(FIX): Server and routers setup.

This commit is contained in:
Hayden Hargreaves 2025-06-13 16:44:23 -07:00
parent 0b29602cb8
commit a88d807e40
5 changed files with 101 additions and 1 deletions

View File

@ -0,0 +1,9 @@
package main
import "github.com/haydenhargreaves/Potion/internal/app/server"
const PORT = 3000
func main() {
server.Init(PORT).Setup().Start()
}

35
go.mod
View File

@ -2,4 +2,37 @@ module github.com/haydenhargreaves/Potion
go 1.24.3
require github.com/a-h/templ v0.3.898 // indirect
require (
github.com/a-h/templ v0.3.898
github.com/gin-gonic/gin v1.10.1
)
require (
github.com/bytedance/sonic v1.13.2 // indirect
github.com/bytedance/sonic/loader v0.2.4 // indirect
github.com/cloudwego/base64x v0.1.5 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
github.com/gin-contrib/cors v1.7.5 // indirect
github.com/gin-contrib/sse v1.0.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.26.0 // indirect
github.com/goccy/go-json v0.10.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.15.0 // indirect
golang.org/x/crypto v0.37.0 // indirect
golang.org/x/net v0.39.0 // indirect
golang.org/x/sys v0.32.0 // indirect
golang.org/x/text v0.24.0 // indirect
google.golang.org/protobuf v1.36.6 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

View File

@ -0,0 +1,2 @@
package handlers

View File

@ -0,0 +1 @@
package handlers

View File

@ -0,0 +1,55 @@
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
}