Files
Go-Template/cmds/server.go

46 lines
1.1 KiB
Go

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
}