# README
Simple Service Wrapper
Serves as a wrapper for simple service where full systemd integration is not needed.
It provides some basic functionality out of the box
- Start/Stop of the application
- Allows you to do action based on signals from the OS, like reloading configuration or activating/deactivating new functionality
- Allows you to start and stop services if the implement the ClientStart and/or ClientStop interfaces
package main
import (
"fmt"
"os"
"syscall"
"github.com/ilijamt/ssw"
"go.uber.org/zap"
)
func main() {
cfg := ssw.NewConfig()
version := ssw.NewVersion("Test", "Desc", "Ver", "Hash", "Date", "Clean")
svc := ssw.WithLogger(
ssw.New("Test", cfg, version),
zap.NewNop(),
)
svc.HandleStart = func() error {
fmt.Println("Handle start")
return nil
}
svc.HandleStop = func() error {
fmt.Println("Handle stop")
return nil
}
svc.HandleError = func(err error) error {
fmt.Println("Handle error")
return nil
}
handler := func(signal os.Signal) error {
fmt.Printf("We have a %s, we can do whatever we want here", signal)
return nil
}
svc.Handler(handler, syscall.SIGHUP, syscall.SIGUSR1)
// This will start the service and register all handlers and everything
if err := svc.Run(); err != nil {
panic(err)
}
}
# Functions
New creates a new instance of service with provided configuration and details.
NewConfig creates a new config instance from environment.
NewVersion returns a new Version.
WithLogger wraps the service with a logger.
# Constants
ErrServiceAlreadyRanOnce tells us that the service already ran and you cannot run it again, if you need to run it again you need to create a new objecct.
# Structs
Config contains configuration for service.
Service represents a service handling requests, what it does it bootstraps the process and wraps it in a nice package where you don't have to worry about starting/stopping the services.
Version contains all the necessary information about a build.
# Interfaces
Client generic interface.
ClientStart for clients that are able to start.
ClientStop for clients that are able to start, the client should make sure that the stop is blocking and it will only continue after it has successfully shutdown.
# Type aliases
HandlerError is a function that will get called if there is an error, here you can decide if you want to terminate the application or not.
HandlerGeneric is a generic function that will be executed at various points.
HandlerInterrupt is a generic function that is executed on a specific signal.