gpaste/cmd/server/server.go

131 lines
3.2 KiB
Go
Raw Normal View History

2022-01-15 18:07:33 +00:00
package main
2022-01-15 21:19:35 +00:00
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
2022-01-15 21:32:24 +00:00
"strings"
2022-01-15 21:19:35 +00:00
"time"
"git.t-juice.club/torjus/gpaste"
2022-01-20 02:44:33 +00:00
"git.t-juice.club/torjus/gpaste/api"
2022-01-15 21:19:35 +00:00
"github.com/urfave/cli/v2"
2022-01-15 21:32:24 +00:00
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
2022-01-15 21:19:35 +00:00
)
var (
version = "dev"
commit = "none"
date = "unknown"
)
2022-01-15 18:07:33 +00:00
func main() {
2022-01-15 21:19:35 +00:00
cli.VersionFlag = &cli.BoolFlag{Name: "version"}
app := cli.App{
Name: "gpaste-server",
Version: fmt.Sprintf("gpaste-server %s-%s (%s)", version, commit, date),
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Usage: "Path to config-file.",
},
},
Action: ActionServe,
}
app.Run(os.Args)
}
func ActionServe(c *cli.Context) error {
configPath := "gpaste-server.toml"
if c.IsSet("config") {
configPath = c.String("config")
}
f, err := os.Open(configPath)
if err != nil {
return cli.Exit(err, 1)
}
defer f.Close()
cfg, err := gpaste.ServerConfigFromReader(f)
if err != nil {
return cli.Exit(err, 1)
}
2022-01-15 21:32:24 +00:00
// Setup loggers
rootLogger := getRootLogger(cfg.LogLevel)
serverLogger := rootLogger.Named("SERV")
2022-01-19 02:23:54 +00:00
accessLogger := rootLogger.Named("ACCS")
2022-01-15 21:19:35 +00:00
2022-01-15 21:32:24 +00:00
// Setup contexts for clean shutdown
2022-01-15 21:19:35 +00:00
rootCtx, rootCancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer rootCancel()
httpCtx, httpCancel := context.WithCancel(rootCtx)
defer httpCancel()
httpShutdownCtx, httpShutdownCancel := context.WithCancel(context.Background())
defer httpShutdownCancel()
go func() {
2022-01-20 02:44:33 +00:00
srv := api.NewHTTPServer(cfg)
2022-01-15 21:19:35 +00:00
srv.Addr = cfg.ListenAddr
2022-01-16 20:29:42 +00:00
srv.Logger = serverLogger
2022-01-19 02:23:54 +00:00
srv.AccessLogger = accessLogger
2022-01-15 21:19:35 +00:00
// Wait for cancel
go func() {
<-httpCtx.Done()
timeoutCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
srv.Shutdown(timeoutCtx)
}()
2022-01-15 21:32:24 +00:00
serverLogger.Infow("Starting HTTP server.", "addr", cfg.ListenAddr)
2022-01-15 21:19:35 +00:00
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
2022-01-15 21:32:24 +00:00
serverLogger.Errorw("Error during shutdown.", "error", err)
2022-01-15 21:19:35 +00:00
}
2022-01-15 21:32:24 +00:00
serverLogger.Infow("HTTP server shutdown complete.", "addr", cfg.ListenAddr)
2022-01-15 21:19:35 +00:00
httpShutdownCancel()
}()
<-httpShutdownCtx.Done()
return nil
2022-01-15 18:07:33 +00:00
}
2022-01-15 21:32:24 +00:00
func getRootLogger(level string) *zap.SugaredLogger {
logEncoderConfig := zap.NewProductionEncoderConfig()
logEncoderConfig.EncodeCaller = zapcore.ShortCallerEncoder
logEncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
logEncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
logEncoderConfig.EncodeDuration = zapcore.StringDurationEncoder
rootLoggerConfig := &zap.Config{
Level: zap.NewAtomicLevelAt(zap.DebugLevel),
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stdout"},
Encoding: "console",
EncoderConfig: logEncoderConfig,
DisableCaller: true,
}
switch strings.ToUpper(level) {
case "DEBUG":
rootLoggerConfig.DisableCaller = false
rootLoggerConfig.Level = zap.NewAtomicLevelAt(zap.DebugLevel)
case "INFO":
rootLoggerConfig.Level = zap.NewAtomicLevelAt(zap.InfoLevel)
case "WARN", "WARNING":
rootLoggerConfig.Level = zap.NewAtomicLevelAt(zap.WarnLevel)
case "ERR", "ERROR":
rootLoggerConfig.Level = zap.NewAtomicLevelAt(zap.ErrorLevel)
}
rootLogger, err := rootLoggerConfig.Build()
if err != nil {
panic(err)
}
return rootLogger.Sugar()
}