add: server shutdown, tests
This commit is contained in:
@@ -4,12 +4,12 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
|
||||
"GoMembership/internal/config"
|
||||
"GoMembership/internal/controllers"
|
||||
"GoMembership/internal/database"
|
||||
"GoMembership/internal/repositories"
|
||||
|
||||
// "GoMembership/internal/middlewares"
|
||||
@@ -20,15 +20,10 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
var shutdownChannel = make(chan struct{})
|
||||
|
||||
// Run initializes the server configuration, sets up services and controllers, and starts the HTTP server.
|
||||
func Run() {
|
||||
config.LoadConfig()
|
||||
logger.Info.Printf("Config loaded: %#v", config.CFG)
|
||||
|
||||
err := database.InitDB(config.DB.Path)
|
||||
if err != nil {
|
||||
logger.Error.Fatalf("Couldn't init database: %v", err)
|
||||
}
|
||||
|
||||
emailService := services.NewEmailService(config.SMTP.Host, config.SMTP.Port, config.SMTP.User, config.SMTP.Password)
|
||||
var consentRepo repositories.ConsentRepositoryInterface = &repositories.ConsentRepository{}
|
||||
@@ -63,7 +58,23 @@ func Run() {
|
||||
// accountRouter.Use(middlewares.AuthMiddleware)
|
||||
|
||||
logger.Info.Println("Starting server on :8080")
|
||||
if err := http.ListenAndServe(":8080", router); err != nil {
|
||||
logger.Error.Fatalf("could not start server: %v", err)
|
||||
}
|
||||
go func() {
|
||||
if err := http.ListenAndServe(":8080", router); err != nil && err != http.ErrServerClosed {
|
||||
logger.Error.Fatalf("could not start server: %v", err)
|
||||
}
|
||||
}()
|
||||
// Wait for the shutdown signal
|
||||
<-shutdownChannel
|
||||
}
|
||||
|
||||
func Shutdown(ctx context.Context) {
|
||||
// Signal the server to stop
|
||||
close(shutdownChannel)
|
||||
|
||||
// Optionally wait for a timeout or other cleanup operations
|
||||
// ctx can be used to manage shutdown timeout or cleanup tasks
|
||||
// select {
|
||||
// case <-ctx.Done():
|
||||
// // handle context cancellation if needed
|
||||
// }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user