Initial commit.
This commit is contained in:
commit
63f43e568c
23 changed files with 1199 additions and 0 deletions
57
main.go
Normal file
57
main.go
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"time"
|
||||
|
||||
"goweb/config"
|
||||
"goweb/container"
|
||||
"goweb/router"
|
||||
|
||||
"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
|
||||
router.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.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)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue