tiny

package module
v1.0.68 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 19, 2023 License: MIT Imports: 16 Imported by: 3

README

tiny

tiny is a Go library for rapid prototyping backend applications. It's basically a wrapper around popular Go libraries. It provides a common interface for starting various network servers, such as TCP, HTTP or gRPC and handling connections to databases, such as Postgres, sqlite or redis. Dependencies containing references to CGO are intentionally avoided and Pure-Go implementations are selected instead.

Install

go get github.com/mkorman9/tiny

Example

package main

import (
	"github.com/gofiber/fiber/v2"
	"github.com/gookit/config/v2"
	"github.com/mkorman9/tiny"
	"github.com/mkorman9/tiny/tinyhttp"
	"github.com/mkorman9/tiny/tinytcp"
)

func main() {
	tiny.Init()

	httpServer := tinyhttp.NewServer(
		config.String("http.address", "0.0.0.0:8080"),
	)
	httpServer.Get("/", func(c *fiber.Ctx) error {
		return c.Status(fiber.StatusOK).
			JSON(fiber.Map{
			    "message": "Hello world!",
			})
	})

	tcpServer := tinytcp.NewServer(
		config.String("tcp.address", "0.0.0.0:7000"),
	)
	tcpServer.ForkingStrategy(tinytcp.GoroutinePerConnection(
		func(socket *tinytcp.Socket) {
			socket.Write([]byte("Hello world!"))
			socket.Close()
		},
	))
	
	tiny.StartAndBlock(httpServer, tcpServer)
}

Documentation

Overview

Package tiny provides utilities for quick prototyping HTTP/gRPC/TCP servers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetSecureRandomString

func GetSecureRandomString(lengthBytes uint) (string, error)

GetSecureRandomString generates a cryptographically secure string of given length. Result string is encoded to hex.

func Init added in v1.0.19

func Init(config ...*Config)

Init initializes global logger and loads configuration from env variables and specified files.

func LoadConfig added in v1.0.1

func LoadConfig(files ...string) (loaded bool)

LoadConfig loads configuration from environment variables and optionally from the specified list of files. YAML, JSON and HCL file formats are supported. Configuration is stored into global config.Config instance.

func StartAndBlock

func StartAndBlock(services ...Service)

StartAndBlock starts all passed services in their designated goroutines and then blocks the current thread. Thread is unblocked when the process receives SIGINT or SIGTERM signals or one of the Start() functions returns an error. When exiting, StartAndBlock gracefully stops all the services by calling their Stop() functions and waiting for them to exit.

func TimePtrToUnix

func TimePtrToUnix(t *time.Time) *int64

TimePtrToUnix returns a pointer to epoch seconds in UTC timezone represented by given pointer to time.Time

func TimeToUnixPtr

func TimeToUnixPtr(t time.Time) *int64

TimeToUnixPtr returns a pointer to epoch seconds in UTC timezone represented by given time.Time

func ToPtr

func ToPtr[V any](v V) *V

ToPtr converts any value to a pointer

func UnixPtrToTime

func UnixPtrToTime(u *int64) *time.Time

UnixPtrToTime returns a pointer to time.Time by converting pointer to epoch seconds in UTC timezone

func UnixToTimePtr

func UnixToTimePtr(u int64) *time.Time

UnixToTimePtr returns a pointer to time.Time by converting epoch seconds in UTC timezone

Types

type Config added in v1.0.65

type Config struct {
	// ConfigFiles specifies a list of files that should be loaded during initialization.
	ConfigFiles []string

	// Log specifies an optional configuration for the global logger.
	Log *tinylog.Config
}

Config hold a configuration for Init(). It allows the end-user to customize core functionalities, such as global logger or locations of config files.

type ConsoleLineHandler added in v1.0.20

type ConsoleLineHandler = func(line string)

ConsoleLineHandler specifies a handler function for ConsoleReader.

type ConsoleReader added in v1.0.20

type ConsoleReader struct {
	// contains filtered or unexported fields
}

ConsoleReader is a Service that actively reads os.Stdin and passes read lines to the underlying handler.

func NewConsoleReader added in v1.0.20

func NewConsoleReader(handler ConsoleLineHandler) *ConsoleReader

NewConsoleReader creates new ConsoleReader.

func (*ConsoleReader) Prompt added in v1.0.21

func (c *ConsoleReader) Prompt(prompt string)

Prompt sets and enables printing defined prompt before line reading.

func (*ConsoleReader) Start added in v1.0.20

func (c *ConsoleReader) Start() error

Start implements the interface of Service.

func (*ConsoleReader) Stop added in v1.0.20

func (c *ConsoleReader) Stop()

Stop implements the interface of Service.

type Service

type Service interface {
	// Start is expected to start execution of the service and block.
	// If the execution cannot be started, or it fails abruptly, it should return a non-nil error.
	Start() error

	// Stop is expected to stop the running service gracefully and unblock the thread used by Start function.
	Stop()
}

Service represents concurrent job, that is expected to run in background for the whole lifetime of the process. Typical implementations of Service include network servers, such as HTTP or gRPC servers.

type SignalHandler

type SignalHandler func(signal os.Signal)

SignalHandler represents a handler function for OS signals.

type SignalsListener

type SignalsListener struct {
	// contains filtered or unexported fields
}

SignalsListener is a listener of OS signals that implements the Service interface.

func NewSignalsListener

func NewSignalsListener(handler SignalHandler, signals ...os.Signal) *SignalsListener

NewSignalsListener creates new SignalsListener.

func (*SignalsListener) Start

func (s *SignalsListener) Start() error

Start implements the interface of Service.

func (*SignalsListener) Stop

func (s *SignalsListener) Stop()

Stop implements the interface of Service.

Directories

Path Synopsis
Package tinygrpc provides gRPC server implementation.
Package tinygrpc provides gRPC server implementation.
Package tinyhttp provides HTTP server implementation.
Package tinyhttp provides HTTP server implementation.
httpauth
Package httpauth provides authorization middleware for HTTP.
Package httpauth provides authorization middleware for HTTP.
requests
Package requests provides HTTP client implementation.
Package requests provides HTTP client implementation.
Package tinylog provides an automated configuration of the global zerolog.Logger.
Package tinylog provides an automated configuration of the global zerolog.Logger.
Package tinypostgres provides utilities for handling Postgres.
Package tinypostgres provides utilities for handling Postgres.
Package tinyredis provides utilities for handling Redis.
Package tinyredis provides utilities for handling Redis.
Package tinysqlite provides utilities for handling sqlite.
Package tinysqlite provides utilities for handling sqlite.
Package tinytcp provides TCP server implementation.
Package tinytcp provides TCP server implementation.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL