saasitone/main.go
2021-12-11 14:00:10 -05:00

58 lines
1.3 KiB
Go

package main
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"time"
"goweb/config"
"goweb/container"
"goweb/controllers"
"github.com/labstack/gommon/log"
)
func main() {
c := container.NewContainer()
// Configure logging
switch c.Config.App.Environment {
case config.EnvProduction:
c.Web.Logger.SetLevel(log.WARN)
default:
c.Web.Logger.SetLevel(log.DEBUG)
}
// Build the router
controllers.BuildRouter(c)
// Start the server
go func() {
srv := http.Server{
Addr: fmt.Sprintf("%s:%d", c.Config.HTTP.Hostname, c.Config.HTTP.Port),
Handler: c.Web,
ReadTimeout: c.Config.HTTP.ReadTimeout,
WriteTimeout: c.Config.HTTP.WriteTimeout,
IdleTimeout: c.Config.HTTP.IdleTimeout,
}
if err := c.Web.StartServer(&srv); err != http.ErrServerClosed {
c.Web.Logger.Fatalf("shutting down the server: v", err)
}
}()
// Wait for interrupt signal to gracefully shutdown the server with a timeout of 10 seconds.
// Use a buffered channel to avoid missing signals as recommended for signal.Notify
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := c.Web.Shutdown(ctx); err != nil {
c.Web.Logger.Fatal(err)
}
}