2021-12-03 03:11:01 -08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"goweb/config"
|
|
|
|
"goweb/container"
|
2021-12-05 17:22:45 -08:00
|
|
|
"goweb/controllers"
|
2021-12-03 03:11:01 -08:00
|
|
|
|
|
|
|
"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
|
2021-12-05 17:22:45 -08:00
|
|
|
controllers.BuildRouter(c)
|
2021-12-03 03:11:01 -08:00
|
|
|
|
|
|
|
// 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.Fatal("shutting down the server")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|