Files
poseidon/main.go
2021-04-30 08:25:40 +02:00

51 lines
1.2 KiB
Go

package main
import (
"context"
"fmt"
"gitlab.hpi.de/codeocean/codemoon/poseidon/api"
"gitlab.hpi.de/codeocean/codemoon/poseidon/config"
"gitlab.hpi.de/codeocean/codemoon/poseidon/logging"
"net/http"
"os"
"os/signal"
"time"
)
var log = logging.GetLogger("main")
func main() {
config.InitConfig()
logging.InitializeLogging(config.Config.Logger.Level)
server := &http.Server{
Addr: fmt.Sprintf("%s:%d", config.Config.Server.Address, config.Config.Server.Port),
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
Handler: api.NewRouter(),
}
log.WithField("address", server.Addr).Info("Starting server")
go func() {
if err := server.ListenAndServe(); err != nil {
if err == http.ErrServerClosed {
log.WithError(err).Info("Server closed")
} else {
log.WithError(err).Fatal("Error during listening and serving")
}
}
}()
// wait for SIGINT
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt)
<-signals
log.Info("Received SIGINT, shutting down ...")
// shutdown the server but wait up to 15 seconds to close remaining connections
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
_ = server.Shutdown(ctx)
}