2022-11-28 00:19:47 +00:00
|
|
|
package server
|
2020-09-30 06:35:07 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/spf13/viper"
|
2020-10-01 04:59:20 +00:00
|
|
|
|
|
|
|
"github.com/getsentry/sentry-go"
|
2020-10-03 05:40:33 +00:00
|
|
|
"github.com/go-chi/chi"
|
2020-10-01 04:59:20 +00:00
|
|
|
|
|
|
|
// spooky action at a distance!
|
|
|
|
// this populates the environment
|
|
|
|
// from a ./.env file automatically
|
|
|
|
// for development configuration.
|
|
|
|
// .env contents should be things like
|
|
|
|
// `DBURL=postgres://user:pass@.../`
|
|
|
|
// (without the backticks, of course)
|
|
|
|
_ "github.com/joho/godotenv/autoload"
|
2020-09-30 06:35:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type server struct {
|
2020-10-01 04:59:20 +00:00
|
|
|
appname string
|
|
|
|
version string
|
|
|
|
buildarch string
|
|
|
|
databaseURL string
|
|
|
|
startupTime time.Time
|
|
|
|
port int
|
|
|
|
exitCode int
|
|
|
|
sentryEnabled bool
|
|
|
|
log *zerolog.Logger
|
|
|
|
ctx context.Context
|
|
|
|
cancelFunc context.CancelFunc
|
|
|
|
httpServer *http.Server
|
2020-10-03 05:40:33 +00:00
|
|
|
router *chi.Mux
|
2020-09-30 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 00:19:47 +00:00
|
|
|
func newServer(options ...func(s *server)) *server {
|
2020-09-30 06:35:07 +00:00
|
|
|
n := new(server)
|
|
|
|
n.startupTime = time.Now()
|
|
|
|
n.version = "unknown"
|
|
|
|
for _, opt := range options {
|
|
|
|
opt(n)
|
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2022-11-28 00:19:47 +00:00
|
|
|
// FIXME change this to use uber/fx DI and an Invoke()
|
2020-09-30 06:35:07 +00:00
|
|
|
// this is where we come in from package main.
|
|
|
|
func Run(appname, version, buildarch string) int {
|
2022-11-28 00:19:47 +00:00
|
|
|
s := newServer(func(i *server) {
|
2020-09-30 06:35:07 +00:00
|
|
|
i.appname = appname
|
|
|
|
if version != "" {
|
|
|
|
i.version = version
|
|
|
|
}
|
|
|
|
i.buildarch = buildarch
|
|
|
|
})
|
|
|
|
|
2020-10-01 04:59:20 +00:00
|
|
|
// this does nothing if SENTRY_DSN is unset in env.
|
|
|
|
|
|
|
|
// TODO remove:
|
|
|
|
if s.sentryEnabled {
|
|
|
|
sentry.CaptureMessage("It works!")
|
|
|
|
}
|
|
|
|
|
2020-09-30 06:35:07 +00:00
|
|
|
s.configure()
|
|
|
|
s.setupLogging()
|
|
|
|
|
2020-10-04 05:46:48 +00:00
|
|
|
// logging before sentry, because sentry logs
|
|
|
|
s.enableSentry()
|
|
|
|
|
2020-09-30 06:35:07 +00:00
|
|
|
s.databaseURL = viper.GetString("DBURL")
|
|
|
|
s.port = viper.GetInt("PORT")
|
|
|
|
|
|
|
|
return s.serve()
|
|
|
|
}
|
|
|
|
|
2020-10-01 04:59:20 +00:00
|
|
|
func (s *server) enableSentry() {
|
2020-10-04 05:46:48 +00:00
|
|
|
s.sentryEnabled = false
|
|
|
|
|
|
|
|
if viper.GetString("SENTRY_DSN") == "" {
|
2020-10-01 04:59:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := sentry.Init(sentry.ClientOptions{
|
2020-10-04 05:46:48 +00:00
|
|
|
Dsn: viper.GetString("SENTRY_DSN"),
|
2020-10-01 04:59:20 +00:00
|
|
|
Release: fmt.Sprintf("%s-%s", s.appname, s.version),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal().Err(err).Msg("sentry init failure")
|
2020-10-04 05:46:48 +00:00
|
|
|
return
|
2020-10-01 04:59:20 +00:00
|
|
|
}
|
2020-10-04 05:46:48 +00:00
|
|
|
s.log.Info().Msg("sentry error reporting activated")
|
2020-10-01 04:59:20 +00:00
|
|
|
s.sentryEnabled = true
|
|
|
|
}
|
|
|
|
|
2020-09-30 06:35:07 +00:00
|
|
|
func (s *server) serve() int {
|
|
|
|
s.ctx, s.cancelFunc = context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
// signal watcher
|
|
|
|
go func() {
|
2020-09-30 07:48:56 +00:00
|
|
|
c := make(chan os.Signal, 1)
|
2020-09-30 06:35:07 +00:00
|
|
|
signal.Ignore(syscall.SIGPIPE)
|
|
|
|
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
|
|
|
// block and wait for signal
|
|
|
|
sig := <-c
|
2020-09-30 07:48:56 +00:00
|
|
|
s.log.Info().Msgf("signal received: %+v", sig)
|
2020-09-30 06:35:07 +00:00
|
|
|
if s.cancelFunc != nil {
|
|
|
|
// cancelling the main context will trigger a clean
|
|
|
|
// shutdown.
|
|
|
|
s.cancelFunc()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go s.serveUntilShutdown()
|
|
|
|
|
2020-09-30 07:48:56 +00:00
|
|
|
for range s.ctx.Done() {
|
2020-10-10 15:08:37 +00:00
|
|
|
// aforementioned clean shutdown upon main context
|
|
|
|
// cancellation
|
2020-09-30 06:35:07 +00:00
|
|
|
}
|
2020-09-30 07:48:56 +00:00
|
|
|
s.cleanShutdown()
|
|
|
|
return s.exitCode
|
2020-09-30 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 04:59:20 +00:00
|
|
|
func (s *server) cleanupForExit() {
|
|
|
|
s.log.Info().Msg("cleaning up")
|
|
|
|
// FIXME unimplemented
|
|
|
|
// close database connections or whatever
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) cleanShutdown() {
|
|
|
|
// initiate clean shutdown
|
|
|
|
s.exitCode = 0
|
|
|
|
ctxShutdown, shutdownCancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
if err := s.httpServer.Shutdown(ctxShutdown); err != nil {
|
|
|
|
s.log.Error().
|
|
|
|
Err(err).
|
|
|
|
Msg("server clean shutdown failed")
|
|
|
|
}
|
|
|
|
if shutdownCancel != nil {
|
|
|
|
shutdownCancel()
|
|
|
|
}
|
|
|
|
|
|
|
|
s.cleanupForExit()
|
|
|
|
|
|
|
|
if s.sentryEnabled {
|
|
|
|
sentry.Flush(2 * time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-30 06:35:07 +00:00
|
|
|
func (s *server) uptime() time.Duration {
|
|
|
|
return time.Since(s.startupTime)
|
|
|
|
}
|
|
|
|
|
2020-10-03 05:40:33 +00:00
|
|
|
func (s *server) maintenance() bool {
|
|
|
|
return viper.GetBool("MAINTENANCE_MODE")
|
|
|
|
}
|
|
|
|
|
2020-09-30 06:35:07 +00:00
|
|
|
func (s *server) configure() {
|
|
|
|
viper.SetConfigName(s.appname)
|
|
|
|
viper.SetConfigType("yaml")
|
2020-10-01 04:59:20 +00:00
|
|
|
// path to look for the config file in:
|
|
|
|
viper.AddConfigPath(fmt.Sprintf("/etc/%s", s.appname))
|
|
|
|
// call multiple times to add many search paths:
|
|
|
|
viper.AddConfigPath(fmt.Sprintf("$HOME/.config/%s", s.appname))
|
2020-10-10 15:08:37 +00:00
|
|
|
// viper.SetEnvPrefix(strings.ToUpper(s.appname))
|
2020-09-30 06:35:07 +00:00
|
|
|
viper.AutomaticEnv()
|
|
|
|
|
|
|
|
viper.SetDefault("DEBUG", "false")
|
2020-10-03 05:40:33 +00:00
|
|
|
viper.SetDefault("MAINTENANCE_MODE", "false")
|
2020-09-30 06:35:07 +00:00
|
|
|
viper.SetDefault("PORT", "8080")
|
|
|
|
viper.SetDefault("DBURL", "")
|
2020-10-04 05:46:48 +00:00
|
|
|
viper.SetDefault("SENTRY_DSN", "")
|
|
|
|
viper.SetDefault("METRICS_USERNAME", "")
|
|
|
|
viper.SetDefault("METRICS_PASSWORD", "")
|
2020-09-30 06:35:07 +00:00
|
|
|
|
|
|
|
if err := viper.ReadInConfig(); err != nil {
|
|
|
|
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
|
|
|
// Config file not found; ignore error if desired
|
|
|
|
} else {
|
|
|
|
// Config file was found but another error was produced
|
|
|
|
log.Panic().
|
|
|
|
Err(err).
|
|
|
|
Msg("config file malformed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-03 05:40:33 +00:00
|
|
|
// if viper.GetBool("DEBUG") {
|
2020-09-30 06:35:07 +00:00
|
|
|
// pp.Print(viper.AllSettings())
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) setupLogging() {
|
|
|
|
// always log in UTC
|
|
|
|
zerolog.TimestampFunc = func() time.Time {
|
|
|
|
return time.Now().UTC()
|
|
|
|
}
|
|
|
|
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
|
|
|
|
|
|
|
tty := false
|
|
|
|
if fileInfo, _ := os.Stdout.Stat(); (fileInfo.Mode() & os.ModeCharDevice) != 0 {
|
|
|
|
tty = true
|
|
|
|
}
|
|
|
|
|
|
|
|
var writers []io.Writer
|
|
|
|
|
|
|
|
if tty {
|
2020-09-30 07:48:56 +00:00
|
|
|
// this does cool colorization for console/dev
|
2020-09-30 06:35:07 +00:00
|
|
|
consoleWriter := zerolog.NewConsoleWriter(
|
|
|
|
func(w *zerolog.ConsoleWriter) {
|
|
|
|
// Customize time format
|
|
|
|
w.TimeFormat = time.RFC3339Nano
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
writers = append(writers, consoleWriter)
|
|
|
|
} else {
|
2020-09-30 07:48:56 +00:00
|
|
|
// log json in prod for the machines
|
2020-09-30 06:35:07 +00:00
|
|
|
writers = append(writers, os.Stdout)
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-09-30 07:48:56 +00:00
|
|
|
// this is how you log to a file, if you do that
|
|
|
|
// sort of thing still
|
2020-09-30 06:35:07 +00:00
|
|
|
logfile := viper.GetString("Logfile")
|
|
|
|
if logfile != "" {
|
|
|
|
logfileDir := filepath.Dir(logfile)
|
|
|
|
err := goutil.Mkdirp(logfileDir)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("unable to create log dir")
|
|
|
|
}
|
|
|
|
|
|
|
|
hp.logfh, err = os.OpenFile(logfile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
|
|
|
|
if err != nil {
|
|
|
|
panic("unable to open logfile: " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
writers = append(writers, hp.logfh)
|
|
|
|
*/
|
|
|
|
|
|
|
|
multi := zerolog.MultiLevelWriter(writers...)
|
|
|
|
logger := zerolog.New(multi).With().Timestamp().Logger().With().Caller().Logger()
|
|
|
|
|
2020-09-30 07:48:56 +00:00
|
|
|
s.log = &logger
|
2020-10-10 15:08:37 +00:00
|
|
|
// log.Logger = logger
|
2020-09-30 06:35:07 +00:00
|
|
|
|
|
|
|
if viper.GetBool("debug") {
|
|
|
|
zerolog.SetGlobalLevel(zerolog.DebugLevel)
|
2020-09-30 07:48:56 +00:00
|
|
|
s.log.Debug().Bool("debug", true).Send()
|
2020-09-30 06:35:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s.identify()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) identify() {
|
2020-09-30 07:48:56 +00:00
|
|
|
s.log.Info().
|
2020-09-30 06:35:07 +00:00
|
|
|
Str("appname", s.appname).
|
|
|
|
Str("version", s.version).
|
|
|
|
Str("buildarch", s.buildarch).
|
|
|
|
Msg("starting")
|
|
|
|
}
|