backend-service/internal/server/server.go
ats-tech25 488be7b8ef feat(project): Initialize project structure and core components
- Add initial project scaffolding with Go module and project structure
- Create server and CLI entry points for application
- Implement Makefile with development and build commands
- Add `.env.example` with comprehensive configuration template
- Set up README.md with project documentation and setup instructions
- Configure basic dependencies for server, database, and CLI tools
- Establish internal package structure for models, services, and handlers
- Add initial configuration and environment management
- Prepare for HTTP server, CLI, and database integration
2025-11-05 15:06:07 +00:00

167 lines
3.8 KiB
Go

package server
import (
"fmt"
"log"
"attune-heart-therapy/internal/config"
"attune-heart-therapy/internal/database"
"github.com/gin-gonic/gin"
)
type Server struct {
config *config.Config
db *database.DB
router *gin.Engine
}
func New(cfg *config.Config) *Server {
// Set Gin mode based on environment
gin.SetMode(gin.ReleaseMode)
router := gin.New()
// Add basic middleware
router.Use(gin.Logger())
router.Use(gin.Recovery())
return &Server{
config: cfg,
router: router,
}
}
// Initialize sets up the database connection and runs migrations
func (s *Server) Initialize() error {
// Initialize database connection
db, err := database.New(s.config)
if err != nil {
return fmt.Errorf("failed to initialize database: %w", err)
}
s.db = db
// Run database migrations
if err := s.db.Migrate(); err != nil {
return fmt.Errorf("failed to run database migrations: %w", err)
}
// Seed database with initial data
if err := s.db.Seed(); err != nil {
return fmt.Errorf("failed to seed database: %w", err)
}
log.Println("Server initialization completed successfully")
return nil
}
func (s *Server) Start() error {
// Initialize database and run migrations
if err := s.Initialize(); err != nil {
return err
}
// Setup routes
s.setupRoutes()
// Start server
addr := fmt.Sprintf("%s:%s", s.config.Server.Host, s.config.Server.Port)
log.Printf("Starting server on %s", addr)
return s.router.Run(addr)
}
// Shutdown gracefully shuts down the server
func (s *Server) Shutdown() error {
if s.db != nil {
log.Println("Closing database connection...")
return s.db.Close()
}
return nil
}
func (s *Server) setupRoutes() {
// Health check endpoint
s.router.GET("/health", s.healthCheck)
// API v1 routes group
v1 := s.router.Group("/api/v1")
{
// Auth routes (will be implemented in later tasks)
auth := v1.Group("/auth")
{
auth.POST("/register", func(c *gin.Context) {
c.JSON(501, gin.H{"message": "Not implemented yet"})
})
auth.POST("/login", func(c *gin.Context) {
c.JSON(501, gin.H{"message": "Not implemented yet"})
})
}
// Booking routes (will be implemented in later tasks)
bookings := v1.Group("/bookings")
{
bookings.GET("/", func(c *gin.Context) {
c.JSON(501, gin.H{"message": "Not implemented yet"})
})
bookings.POST("/", func(c *gin.Context) {
c.JSON(501, gin.H{"message": "Not implemented yet"})
})
}
// Schedule routes (will be implemented in later tasks)
schedules := v1.Group("/schedules")
{
schedules.GET("/", func(c *gin.Context) {
c.JSON(501, gin.H{"message": "Not implemented yet"})
})
}
// Payment routes (will be implemented in later tasks)
payments := v1.Group("/payments")
{
payments.POST("/intent", func(c *gin.Context) {
c.JSON(501, gin.H{"message": "Not implemented yet"})
})
payments.POST("/confirm", func(c *gin.Context) {
c.JSON(501, gin.H{"message": "Not implemented yet"})
})
payments.POST("/webhook", func(c *gin.Context) {
c.JSON(501, gin.H{"message": "Not implemented yet"})
})
}
// Admin routes (will be implemented in later tasks)
admin := v1.Group("/admin")
{
admin.GET("/dashboard", func(c *gin.Context) {
c.JSON(501, gin.H{"message": "Not implemented yet"})
})
}
}
}
// healthCheck handles the health check endpoint
func (s *Server) healthCheck(c *gin.Context) {
response := gin.H{
"status": "ok",
"message": "Video Conference Booking System API",
}
// Check database connectivity
if s.db != nil {
if err := s.db.Health(); err != nil {
response["status"] = "error"
response["database"] = "disconnected"
response["error"] = err.Error()
c.JSON(500, response)
return
}
response["database"] = "connected"
} else {
response["database"] = "not initialized"
}
c.JSON(200, response)
}