2025-06-13 16:44:23 -07:00

56 lines
1.2 KiB
Go

package server
import (
"fmt"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
type Server struct {
port int
Router *gin.Engine
config cors.Config
}
// Init initializes the server with the provided port. CORS settings are defined here.
// A pointer to a server object is returned which allows for method chaining.
func Init(port int) *Server {
// TODO: Set this to release in prod
gin.SetMode(gin.DebugMode)
server := &Server{
Router: gin.Default(),
port: port,
config: cors.DefaultConfig(),
}
// Setup the CORS settings and active them
server.config.AllowAllOrigins = true
server.Router.Use(cors.New(server.config))
return server
}
// Start starts the server on the port provided when the server was initialized
func (s *Server) Start() {
s.Router.Run(fmt.Sprintf(":%d", s.port))
}
func (s *Server) Setup() *Server {
// Wrap all routes with a version
router_v1 := s.Router.Group("/v1")
// Domain specific routers
router_web := router_v1.Group("/web")
router_api := router_v1.Group("/api")
// API router endpoints
router_api.GET("/", func(ctx *gin.Context) { ctx.JSON(200, gin.H{"message": "Server is active."}) })
// WEB router endpoints
router_web.GET("/", nil)
return s
}