weather/cmd/weather/main.go

61 lines
1.3 KiB
Go

package main
import (
"context"
"flag"
"go/weather/internal"
"go/weather/internal/version"
"go/weather/internal/web"
"go/weather/pkg/logger"
"log"
"os"
"os/signal"
"time"
"go.uber.org/zap"
)
func main() {
var wait time.Duration
var configFile string
flag.DurationVar(&wait, "graceful-timeout", time.Second*15, "the duration for which the server gracefully wait for existing connections to finish - e.g. 15s or 1m")
flag.StringVar(&configFile, "filename", "config.hcl", "configuration filename")
flag.Parse()
//logger
loggerLevel := zap.NewAtomicLevel()
defaultLogger := logger.NewLogger("weather", "weather.log", loggerLevel)
defer defaultLogger.Sync()
//configuration parsing
config := internal.ParseConfiguration(defaultLogger.Sugar(), configFile)
//http
addr := web.NewListenAddr("0.0.0.0", 8080)
defaultLogger.Sugar().Infof("Weather server is listening on %s", addr)
server := web.New(defaultLogger, addr, version.String()).
WithTLSConfigure().
WithHandler(config).
WithHTTPLogging().
WithErrorLogging()
go func() {
if err := server.Serve(); err != nil {
defaultLogger.Sugar().Info(err)
}
}()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
ctx, cancel := context.WithTimeout(context.Background(), wait)
defer cancel()
server.Shutdown(ctx)
log.Println("shutting down")
os.Exit(0)
}