From 71ff41d3875f8a2d60077b6bd29fc7bd6c46751b Mon Sep 17 00:00:00 2001 From: Ron Rise Date: Thu, 22 May 2025 16:56:58 -0400 Subject: [PATCH] Please no changes this time. --- .gitea/workflows/test.yml | 39 ++++++++++++++++++ .gitignore | 2 + cmds/server.go | 45 +++++++++++++++++++++ go.mod | 28 +++++++++++++ go.sum | 48 +++++++++++++++++++++++ http_handlers/errors/badrequest.go | 13 ++++++ http_handlers/errors/notfound.go | 10 +++++ http_handlers/handler.go | 11 ++++++ http_handlers/index/index.go | 40 +++++++++++++++++++ logger/log.go | 63 ++++++++++++++++++++++++++++++ main.go | 34 ++++++++++++++++ 11 files changed, 333 insertions(+) create mode 100644 .gitea/workflows/test.yml create mode 100644 .gitignore create mode 100644 cmds/server.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 http_handlers/errors/badrequest.go create mode 100644 http_handlers/errors/notfound.go create mode 100644 http_handlers/handler.go create mode 100644 http_handlers/index/index.go create mode 100644 logger/log.go create mode 100644 main.go diff --git a/.gitea/workflows/test.yml b/.gitea/workflows/test.yml new file mode 100644 index 0000000..8984514 --- /dev/null +++ b/.gitea/workflows/test.yml @@ -0,0 +1,39 @@ +on: + push: + branches: + - "*" + +name: ๐Ÿงช โœจ Unit Tests Workflow + +jobs: + test-go: + env: + GO_VERSION: '1.24.3' + name: ๐Ÿ” ๐Ÿน Go Tests + runs-on: ubuntu-latest + steps: + + - name: ๐Ÿ›ก๏ธ ๐Ÿ”’ Add Siteworx CA Certificates + run: | + apt update && apt install -yq ca-certificates curl + curl -Ls https://siteworxpro.com/hosted/Siteworx+Root+CA.pem -o /usr/local/share/ca-certificates/sw.crt + update-ca-certificates + + - name: โš™๏ธ ๐Ÿน Set up Go Environment + uses: actions/setup-go@v2 + with: + go-version: ${{ env.GO_VERSION }} + cache: true + + - name: ๐Ÿ“– ๐Ÿ” Checkout Repository Code + uses: actions/checkout@v2 + with: + fetch-depth: 1 + + - name: ๐Ÿ“ฆ ๐Ÿ“ฅ Install Dependencies + run: | + go mod download + + - name: โœ… ๐Ÿ” Run Go Tests + run: | + go test -v ./... -coverprofile=coverage.out \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cd0c06c --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea/ +Go-Template \ No newline at end of file diff --git a/cmds/server.go b/cmds/server.go new file mode 100644 index 0000000..02121d9 --- /dev/null +++ b/cmds/server.go @@ -0,0 +1,45 @@ +package cmds + +import ( + "gitea.siteworxpro.com/Siteworxpro/Go-Template/http_handlers/index" + "gitea.siteworxpro.com/Siteworxpro/Go-Template/logger" + "github.com/labstack/echo/v4" + "github.com/labstack/echo/v4/middleware" + "github.com/spf13/cobra" +) + +func ServerCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "server", + Short: "Start the server", + Long: `Start the http server with the specified port or 8080 default.`, + Run: func(cmd *cobra.Command, args []string) { + l := logger.FromContext(cmd.Context()) + + e := echo.New() + e.HideBanner = true + e.HidePort = true + + e.Use(middleware.Logger()) + e.Use(middleware.Recover()) + e.Use(middleware.CORS()) + e.Use(middleware.Gzip()) + + e.Use(func(next echo.HandlerFunc) echo.HandlerFunc { + return func(c echo.Context) error { + c.Set("logger", l) + return next(c) + } + }) + + index.Register(e.Group("/")) + + l.Info("Starting server on port %s", cmd.Flag("port").Value.String()) + e.Logger.Fatal(e.Start(":" + cmd.Flag("port").Value.String())) + }, + } + + cmd.Flags().StringP("port", "p", "8080", "Port to run the server on") + + return cmd +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..346291f --- /dev/null +++ b/go.mod @@ -0,0 +1,28 @@ +module gitea.siteworxpro.com/Siteworxpro/Go-Template + +go 1.24.3 + +require ( + github.com/labstack/echo/v4 v4.13.4 + github.com/sirupsen/logrus v1.9.3 + github.com/spf13/cobra v1.9.1 + github.com/stretchr/testify v1.10.0 +) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/labstack/gommon v0.4.2 // indirect + github.com/mattn/go-colorable v0.1.14 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/spf13/pflag v1.0.6 // indirect + github.com/valyala/bytebufferpool v1.0.0 // indirect + github.com/valyala/fasttemplate v1.2.2 // indirect + golang.org/x/crypto v0.38.0 // indirect + golang.org/x/net v0.40.0 // indirect + golang.org/x/sys v0.33.0 // indirect + golang.org/x/text v0.25.0 // indirect + golang.org/x/time v0.11.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..3f09143 --- /dev/null +++ b/go.sum @@ -0,0 +1,48 @@ +github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/labstack/echo/v4 v4.13.4 h1:oTZZW+T3s9gAu5L8vmzihV7/lkXGZuITzTQkTEhcXEA= +github.com/labstack/echo/v4 v4.13.4/go.mod h1:g63b33BZ5vZzcIUF8AtRH40DrTlXnx4UMC8rBdndmjQ= +github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0= +github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU= +github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE= +github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8= +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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo= +github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0= +github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o= +github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= +github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo= +github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ= +golang.org/x/crypto v0.38.0 h1:jt+WWG8IZlBnVbomuhg2Mdq0+BBQaHbtqHEFEigjUV8= +golang.org/x/crypto v0.38.0/go.mod h1:MvrbAqul58NNYPKnOra203SB9vpuZW0e+RRZV+Ggqjw= +golang.org/x/net v0.40.0 h1:79Xs7wF06Gbdcg4kdCCIQArK11Z1hr5POQ6+fIYHNuY= +golang.org/x/net v0.40.0/go.mod h1:y0hY0exeL2Pku80/zKK7tpntoX23cqL3Oa6njdgRtds= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw= +golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= +golang.org/x/text v0.25.0 h1:qVyWApTSYLk/drJRO5mDlNYskwQznZmkpV2c8q9zls4= +golang.org/x/text v0.25.0/go.mod h1:WEdwpYrmk1qmdHvhkSTNPm3app7v4rsT8F2UD6+VHIA= +golang.org/x/time v0.11.0 h1:/bpjEDfN9tkoN/ryeYHnv5hcMlc8ncjMcM4XBk5NWV0= +golang.org/x/time v0.11.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/http_handlers/errors/badrequest.go b/http_handlers/errors/badrequest.go new file mode 100644 index 0000000..216e6ab --- /dev/null +++ b/http_handlers/errors/badrequest.go @@ -0,0 +1,13 @@ +package errors + +import ( + "github.com/labstack/echo/v4" + "net/http" +) + +func NewBadRequestError(c echo.Context, fields map[string]string) error { + return c.JSON(http.StatusBadRequest, map[string]interface{}{ + "error": "Bad Request", + "fields": fields, + }) +} diff --git a/http_handlers/errors/notfound.go b/http_handlers/errors/notfound.go new file mode 100644 index 0000000..42a50d0 --- /dev/null +++ b/http_handlers/errors/notfound.go @@ -0,0 +1,10 @@ +package errors + +import ( + "github.com/labstack/echo/v4" + "net/http" +) + +func NewNotFoundError(c echo.Context) error { + return c.JSON(http.StatusNotFound, map[string]string{"error": "Not Found"}) +} diff --git a/http_handlers/handler.go b/http_handlers/handler.go new file mode 100644 index 0000000..d276590 --- /dev/null +++ b/http_handlers/handler.go @@ -0,0 +1,11 @@ +package http_handlers + +import "github.com/labstack/echo/v4" + +type Handler interface { + Get(c echo.Context) error + Post(c echo.Context) error + Put(c echo.Context) error + Delete(c echo.Context) error + Patch(c echo.Context) error +} diff --git a/http_handlers/index/index.go b/http_handlers/index/index.go new file mode 100644 index 0000000..f3fab97 --- /dev/null +++ b/http_handlers/index/index.go @@ -0,0 +1,40 @@ +package index + +import ( + "gitea.siteworxpro.com/Siteworxpro/Go-Template/http_handlers/errors" + "gitea.siteworxpro.com/Siteworxpro/Go-Template/logger" + "github.com/labstack/echo/v4" +) + +func Register(g *echo.Group) { + index := new(index) + + g.GET("", index.Get) + g.POST("", index.Post) + g.PUT("", index.Put) + g.DELETE("", index.Delete) + g.PATCH("", index.Patch) +} + +type index struct{} + +func (i *index) Get(c echo.Context) error { + c.Get("logger").(logger.Interface).Info("Index handler called") + return c.JSON(200, map[string]string{"message": "Hello, World!"}) +} + +func (i *index) Post(c echo.Context) error { + return errors.NewBadRequestError(c, map[string]string{"test": "Test is a required field"}) +} + +func (i *index) Put(c echo.Context) error { + return c.JSON(200, map[string]string{"message": "Hello, World!"}) +} + +func (i *index) Delete(c echo.Context) error { + return c.JSON(200, map[string]string{"message": "Hello, World!"}) +} + +func (i *index) Patch(c echo.Context) error { + return errors.NewNotFoundError(c) +} diff --git a/logger/log.go b/logger/log.go new file mode 100644 index 0000000..c923082 --- /dev/null +++ b/logger/log.go @@ -0,0 +1,63 @@ +package logger + +import ( + "context" + log "github.com/sirupsen/logrus" +) + +const contextKey = "logger" + +type Interface interface { + Info(format string, args ...interface{}) + Error(format string, args ...interface{}) + Debug(format string, args ...interface{}) + Warn(format string, args ...interface{}) + GetLevel() log.Level +} + +type Logger struct { + logger *log.Logger +} + +func FromContext(ctx context.Context) Interface { + logger, ok := ctx.Value(contextKey).(*Logger) + if !ok { + return nil + } + + return logger +} + +func NewLogger(level log.Level) Interface { + l := log.New() + l.SetFormatter(&log.JSONFormatter{}) + l.SetLevel(level) + + return &Logger{ + logger: l, + } +} + +func NewContext(ctx context.Context, logger Interface) context.Context { + return context.WithValue(ctx, contextKey, logger) +} + +func (l *Logger) Info(format string, args ...interface{}) { + l.logger.Infof(format, args...) +} + +func (l *Logger) Error(format string, args ...interface{}) { + l.logger.Errorf(format, args...) +} + +func (l *Logger) Debug(format string, args ...interface{}) { + l.logger.Debugf(format, args...) +} + +func (l *Logger) Warn(format string, args ...interface{}) { + l.logger.Warnf(format, args...) +} + +func (l *Logger) GetLevel() log.Level { + return l.logger.GetLevel() +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..40b3a88 --- /dev/null +++ b/main.go @@ -0,0 +1,34 @@ +package main + +import ( + "context" + "gitea.siteworxpro.com/Siteworxpro/Go-Template/cmds" + "gitea.siteworxpro.com/Siteworxpro/Go-Template/logger" + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" + "os" +) + +const Version = "0.1" + +func main() { + + rootCmd := &cobra.Command{ + Use: "Go-Template", + Short: "Go-Template is a simple template for Go applications", + Long: `Go-Template is a simple template for Go applications. It provides a basic structure for building Go applications with a focus on simplicity and ease of use.`, + Version: Version, + } + + l := logger.NewLogger(log.DebugLevel) + rootCmd.SetContext(logger.NewContext(context.Background(), l)) + + rootCmd.AddCommand(cmds.ServerCommand()) + err := rootCmd.Execute() + + if err != nil { + os.Exit(1) + } + + os.Exit(0) +}