From 8b8447213fa397d06dec80d432fa99337d25fab3 Mon Sep 17 00:00:00 2001 From: "Felipe M." Date: Wed, 20 Nov 2024 23:21:03 +0100 Subject: [PATCH] Imported from hako --- README.md | 69 ++++++++++++++++++++++++++++++++++++++ database/database.go | 39 ++++++++++++++++++++++ go.mod | 21 ++++++++++++ go.sum | 49 +++++++++++++++++++++++++++ model/server.go | 9 +++++ model/service.go | 9 +++++ model/template.go | 5 +++ server/http/server.go | 1 + service/service.go | 77 +++++++++++++++++++++++++++++++++++++++++++ template/engine.go | 36 ++++++++++++++++++++ 10 files changed, 315 insertions(+) create mode 100644 database/database.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 model/server.go create mode 100644 model/service.go create mode 100644 model/template.go create mode 100644 server/http/server.go create mode 100644 service/service.go create mode 100644 template/engine.go diff --git a/README.md b/README.md index 281bd9e..470f864 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,71 @@ # gotoolkit +A set of basic tools to develop Go applications. + +## Database + +A basic database engine to manage the connection to a database. + +```go +db, err := database.New("postgres://user:password@localhost:5432/dbname") +if err != nil { + // ... +} +``` + +## Service & Servers + +A basic way to expose servers within one service. + +A service is the main point of the application, and it can expose multiple servers. +Using the provided interfaces you can create a new _service_ with multiple _servers_. + +A simple example with one single HTTP server: + +```go + +import "git.nakama.town/fmartingr/gotoolkit/model" + +type httpServer struct {} + +func (s *httpServer) IsEnabled() bool { + return true +} + +func (s *httpServer) Start(_ context.Context) error { + return s.http.ListenAndServe() +} + +func (s *httpServer) Stop(ctx context.Context) error { + return s.http.Shutdown(ctx) +} + +func newHttpServer() (servers.Server, error) { + httpServer := &httpServer{} + // http server logic + return httpServer, nil +} + +func main() { + httpServer, _ := newHttpServer() + svc := service.New([]model.Server{server}) + svc.Start(context.Background()) + svc.WaitStop() +} +``` + +## Template + +A basic template engine to render html templates. + +```go +//go:embed templates/*.html +var Templates embed.FS +// ... +engine, _ := template.NewEngine(Templates) +result, _ := engine.Render("template.html", struct { + Message string +}{ + Message: "nometokens" +}) +``` diff --git a/database/database.go b/database/database.go new file mode 100644 index 0000000..86586b2 --- /dev/null +++ b/database/database.go @@ -0,0 +1,39 @@ +package database + +import ( + "database/sql" + "fmt" + "net/url" + "strings" + + _ "modernc.org/sqlite" +) + +type Engine struct { + db *sql.DB +} + +func connect(dbURL string) (*sql.DB, error) { + dbU, err := url.Parse(dbURL) + if err != nil { + return nil, fmt.Errorf("failed to parse database URL: %w", err) + } + + return sql.Open(dbU.Scheme, strings.TrimPrefix(dbURL, dbU.Scheme+":")) +} + +func New(databaseURI string) (*Engine, error) { + db, err := connect(databaseURI) + if err != nil { + return nil, fmt.Errorf("failed to connect to database: %w", err) + } + + // TODO: try for several seconds before giving up + if err := db.Ping(); err != nil { + return nil, fmt.Errorf("failed to ping database: %w", err) + } + + return &Engine{ + db: db, + }, nil +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..a0ea3cd --- /dev/null +++ b/go.mod @@ -0,0 +1,21 @@ +module git.nakama.town/fmartingr/gotoolkit + +go 1.23.3 + +require modernc.org/sqlite v1.34.1 + +require ( + github.com/dustin/go-humanize v1.0.1 // indirect + github.com/google/uuid v1.6.0 // indirect + github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/ncruces/go-strftime v0.1.9 // indirect + github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect + golang.org/x/sys v0.22.0 // indirect + modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6 // indirect + modernc.org/libc v1.55.3 // indirect + modernc.org/mathutil v1.6.0 // indirect + modernc.org/memory v1.8.0 // indirect + modernc.org/strutil v1.2.0 // indirect + modernc.org/token v1.1.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..a3a46c5 --- /dev/null +++ b/go.sum @@ -0,0 +1,49 @@ +github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= +github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= +github.com/google/pprof v0.0.0-20240409012703-83162a5b38cd h1:gbpYu9NMq8jhDVbvlGkMFWCjLFlqqEZjEmObmhUy6Vo= +github.com/google/pprof v0.0.0-20240409012703-83162a5b38cd/go.mod h1:kf6iHlnVGwgKolg33glAes7Yg/8iWP8ukqeldJSO7jw= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k= +github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4= +github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= +golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic= +golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= +golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/tools v0.19.0 h1:tfGCXNR1OsFG+sVdLAitlpjAvD/I6dHDKnYrpEZUHkw= +golang.org/x/tools v0.19.0/go.mod h1:qoJWxmGSIBmAeriMx19ogtrEPrGtDbPK634QFIcLAhc= +modernc.org/cc/v4 v4.21.4 h1:3Be/Rdo1fpr8GrQ7IVw9OHtplU4gWbb+wNgeoBMmGLQ= +modernc.org/cc/v4 v4.21.4/go.mod h1:HM7VJTZbUCR3rV8EYBi9wxnJ0ZBRiGE5OeGXNA0IsLQ= +modernc.org/ccgo/v4 v4.19.2 h1:lwQZgvboKD0jBwdaeVCTouxhxAyN6iawF3STraAal8Y= +modernc.org/ccgo/v4 v4.19.2/go.mod h1:ysS3mxiMV38XGRTTcgo0DQTeTmAO4oCmJl1nX9VFI3s= +modernc.org/fileutil v1.3.0 h1:gQ5SIzK3H9kdfai/5x41oQiKValumqNTDXMvKo62HvE= +modernc.org/fileutil v1.3.0/go.mod h1:XatxS8fZi3pS8/hKG2GH/ArUogfxjpEKs3Ku3aK4JyQ= +modernc.org/gc/v2 v2.4.1 h1:9cNzOqPyMJBvrUipmynX0ZohMhcxPtMccYgGOJdOiBw= +modernc.org/gc/v2 v2.4.1/go.mod h1:wzN5dK1AzVGoH6XOzc3YZ+ey/jPgYHLuVckd62P0GYU= +modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6 h1:5D53IMaUuA5InSeMu9eJtlQXS2NxAhyWQvkKEgXZhHI= +modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6/go.mod h1:Qz0X07sNOR1jWYCrJMEnbW/X55x206Q7Vt4mz6/wHp4= +modernc.org/libc v1.55.3 h1:AzcW1mhlPNrRtjS5sS+eW2ISCgSOLLNyFzRh/V3Qj/U= +modernc.org/libc v1.55.3/go.mod h1:qFXepLhz+JjFThQ4kzwzOjA/y/artDeg+pcYnY+Q83w= +modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4= +modernc.org/mathutil v1.6.0/go.mod h1:Ui5Q9q1TR2gFm0AQRqQUaBWFLAhQpCwNcuhBOSedWPo= +modernc.org/memory v1.8.0 h1:IqGTL6eFMaDZZhEWwcREgeMXYwmW83LYW8cROZYkg+E= +modernc.org/memory v1.8.0/go.mod h1:XPZ936zp5OMKGWPqbD3JShgd/ZoQ7899TUuQqxY+peU= +modernc.org/opt v0.1.3 h1:3XOZf2yznlhC+ibLltsDGzABUGVx8J6pnFMS3E4dcq4= +modernc.org/opt v0.1.3/go.mod h1:WdSiB5evDcignE70guQKxYUl14mgWtbClRi5wmkkTX0= +modernc.org/sortutil v1.2.0 h1:jQiD3PfS2REGJNzNCMMaLSp/wdMNieTbKX920Cqdgqc= +modernc.org/sortutil v1.2.0/go.mod h1:TKU2s7kJMf1AE84OoiGppNHJwvB753OYfNl2WRb++Ss= +modernc.org/sqlite v1.34.1 h1:u3Yi6M0N8t9yKRDwhXcyp1eS5/ErhPTBggxWFuR6Hfk= +modernc.org/sqlite v1.34.1/go.mod h1:pXV2xHxhzXZsgT/RtTFAPY6JJDEvOTcTdwADQCCWD4k= +modernc.org/strutil v1.2.0 h1:agBi9dp1I+eOnxXeiZawM8F4LawKv4NzGWSaLfyeNZA= +modernc.org/strutil v1.2.0/go.mod h1:/mdcBmfOibveCTBxUl5B5l6W+TTH1FXPLHZE6bTosX0= +modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y= +modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM= diff --git a/model/server.go b/model/server.go new file mode 100644 index 0000000..b928b86 --- /dev/null +++ b/model/server.go @@ -0,0 +1,9 @@ +package model + +import "context" + +type Server interface { + IsEnabled() bool + Start(context.Context) error + Stop(context.Context) error +} diff --git a/model/service.go b/model/service.go new file mode 100644 index 0000000..325c67b --- /dev/null +++ b/model/service.go @@ -0,0 +1,9 @@ +package model + +import "context" + +type Service interface { + Start(context.Context) error + Stop(context.Context) error + WaitStop(context.Context) error +} diff --git a/model/template.go b/model/template.go new file mode 100644 index 0000000..6369575 --- /dev/null +++ b/model/template.go @@ -0,0 +1,5 @@ +package model + +type TemplateEngine interface { + Render(name string, data any) ([]byte, error) +} diff --git a/server/http/server.go b/server/http/server.go new file mode 100644 index 0000000..d02cfda --- /dev/null +++ b/server/http/server.go @@ -0,0 +1 @@ +package http diff --git a/service/service.go b/service/service.go new file mode 100644 index 0000000..28ab831 --- /dev/null +++ b/service/service.go @@ -0,0 +1,77 @@ +package service + +import ( + "context" + "errors" + "fmt" + "log/slog" + "net/http" + "os" + "os/signal" + "syscall" + "time" + + "git.nakama.town/fmartingr/gotoolkit/model" +) + +type Service struct { + servers []model.Server + cancel context.CancelFunc +} + +func (s *Service) Start(ctx context.Context) error { + ctx, cancel := context.WithCancel(ctx) + s.cancel = cancel + + for _, server := range s.servers { + if server.IsEnabled() { + go func() { + if err := server.Start(ctx); err != nil && !errors.Is(err, http.ErrServerClosed) { + slog.Error("error starting server", slog.String("err", err.Error())) + } + }() + } + } + + return nil +} + +func (s *Service) WaitStop(ctx context.Context) error { + signals := make(chan os.Signal, 1) + signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM) + + sig := <-signals + slog.Debug("signal %s received, shutting down", slog.String("signal", fmt.Sprintf("%v", sig))) + + if err := s.Stop(ctx); err != nil { + return err + } + + return nil +} + +func (s *Service) Stop(ctx context.Context) error { + s.cancel() + + shuwdownContext, cancel := context.WithTimeout(ctx, 10*time.Second) + defer cancel() + + for _, server := range s.servers { + if server.IsEnabled() { + if err := server.Stop(shuwdownContext); err != nil && !errors.Is(err, http.ErrServerClosed) { + slog.Error("error shutting down http server", slog.String("err", err.Error())) + return err + } + } + } + + return nil +} + +func NewService(servers []model.Server) (*Service, error) { + server := &Service{ + servers: servers, + } + + return server, nil +} diff --git a/template/engine.go b/template/engine.go new file mode 100644 index 0000000..53414d3 --- /dev/null +++ b/template/engine.go @@ -0,0 +1,36 @@ +package template + +import ( + "bytes" + "embed" + "fmt" + "html/template" +) + +// Engine is a template engine +type Engine struct { + templates *template.Template +} + +// Render renders the template with the given name and data +func (e *Engine) Render(name string, data any) ([]byte, error) { + var buf bytes.Buffer + + if err := e.templates.ExecuteTemplate(&buf, name, data); err != nil { + return nil, fmt.Errorf("failed to execute template: %w", err) + } + + return buf.Bytes(), nil +} + +// NewTemplateEngine creates a new template engine from the given templates +func NewEngine(templates embed.FS) (*Engine, error) { + tmpls, err := template.ParseFS(templates, "**/*.html") + if err != nil { + return nil, fmt.Errorf("failed to parse templates: %w", err) + } + + return &Engine{ + templates: tmpls, + }, nil +}