The system allows services and repositories to be created in the main server definition and then store them in the context. They can then be retrieved and mapped onto the injection type and accessed in the handlers. The handlers can then use the deps to call services. Each service is initialized with the required repositories so they can be accessed directly.
138 lines
3.4 KiB
Go
138 lines
3.4 KiB
Go
package server
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/a-h/templ/examples/integration-gin/gintemplrenderer"
|
|
"github.com/gin-contrib/cors"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/haydenhargreaves/Potion/internal/app/handlers"
|
|
"github.com/haydenhargreaves/Potion/internal/app/service"
|
|
domain "github.com/haydenhargreaves/Potion/internal/domain/server"
|
|
"github.com/haydenhargreaves/Potion/internal/infrastructure/auth"
|
|
"github.com/haydenhargreaves/Potion/internal/infrastructure/database/repository"
|
|
"github.com/joho/godotenv"
|
|
|
|
_ "github.com/lib/pq"
|
|
)
|
|
|
|
type Server struct {
|
|
port int
|
|
Router *gin.Engine
|
|
config cors.Config
|
|
DB *sql.DB
|
|
}
|
|
|
|
// 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(),
|
|
}
|
|
|
|
// Some stuff for templ rendering
|
|
htmlRenderer := server.Router.HTMLRender
|
|
server.Router.HTMLRender = &gintemplrenderer.HTMLTemplRenderer{FallbackHtmlRenderer: htmlRenderer}
|
|
|
|
// Disable proxy warnings
|
|
server.Router.SetTrustedProxies(nil)
|
|
|
|
// Setup the CORS settings and active them
|
|
server.config.AllowAllOrigins = true
|
|
server.Router.Use(cors.New(server.config))
|
|
|
|
return server
|
|
}
|
|
|
|
func (s *Server) ConfigureAuth() *Server {
|
|
err := godotenv.Load(".env")
|
|
if err != nil {
|
|
panic("Could not load env file")
|
|
}
|
|
|
|
var (
|
|
redirectUrl string = "http://localhost:3000/v1/api/auth/callback"
|
|
clientId string = os.Getenv("GOOGLE_CLIENT_ID")
|
|
clientSecret string = os.Getenv("GOOGLE_CLIENT_SECRET")
|
|
scope []string = []string{
|
|
"https://www.googleapis.com/auth/userinfo.email",
|
|
"https://www.googleapis.com/auth/userinfo.profile",
|
|
}
|
|
)
|
|
|
|
// Setup Google OAuth
|
|
auth.NewGoogleConfig(redirectUrl, clientId, clientSecret, scope)
|
|
|
|
return s
|
|
}
|
|
|
|
func (s *Server) ConnectDatabase() *Server {
|
|
err := godotenv.Load(".env")
|
|
if err != nil {
|
|
panic("Could not load env file")
|
|
}
|
|
|
|
var connUrl string = os.Getenv("DATABASE_URL")
|
|
|
|
db, err := sql.Open("postgres", connUrl)
|
|
if err != nil {
|
|
panic("Could not connect to database: " + err.Error())
|
|
}
|
|
|
|
if err = db.Ping(); err != nil {
|
|
panic("Error pinging database: " + err.Error())
|
|
}
|
|
|
|
s.DB = db
|
|
|
|
return s
|
|
}
|
|
|
|
// 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 {
|
|
// Initialize and inject dependencies
|
|
userRepo := repository.NewUserRepository(s.DB)
|
|
userService := service.NewUserService(userRepo)
|
|
authService := service.NewAuthService(userRepo)
|
|
|
|
deps := &domain.InjectedDependencies{
|
|
UserService: userService,
|
|
AuthService: authService,
|
|
}
|
|
|
|
s.Router.Use(DepedencyInjectionMiddleware(deps))
|
|
|
|
// 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")
|
|
|
|
// Static routes
|
|
router_web.Static("/static", "./web/static")
|
|
|
|
// 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("/login", handlers.LoginPage)
|
|
|
|
// Google oauth
|
|
router_api.GET("/auth/login", handlers.GoogleLogin)
|
|
router_api.GET("/auth/callback", handlers.GoogleCallback)
|
|
|
|
return s
|
|
}
|