Documentation ¶
Overview ¶
Package tiny provides utilities for quick prototyping HTTP/gRPC/TCP servers.
Index ¶
- func GetSecureRandomString(lengthBytes uint) (string, error)
- func Init(config ...*Config)
- func LoadConfig(files ...string) (loaded bool)
- func StartAndBlock(services ...Service)
- func TimePtrToUnix(t *time.Time) *int64
- func TimeToUnixPtr(t time.Time) *int64
- func ToPtr[V any](v V) *V
- func UnixPtrToTime(u *int64) *time.Time
- func UnixToTimePtr(u int64) *time.Time
- type Config
- type ConsoleLineHandler
- type ConsoleReader
- type Service
- type SignalHandler
- type SignalsListener
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetSecureRandomString ¶
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
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 ¶
TimePtrToUnix returns a pointer to epoch seconds in UTC timezone represented by given pointer to time.Time
func TimeToUnixPtr ¶
TimeToUnixPtr returns a pointer to epoch seconds in UTC timezone represented by given time.Time
func UnixPtrToTime ¶
UnixPtrToTime returns a pointer to time.Time by converting pointer to epoch seconds in UTC timezone
func UnixToTimePtr ¶
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 ¶
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.
Source Files ¶
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. |