debian-forge-composer/cmd/osbuild-worker-executor/main.go
Michael Vogt 372d9f07dd cmd/osbuild-worker-executor: import verbatim from mvo5/oaas
This commit imports the repository https://github.com/mvo5/oaas
without keeping the history. The history is largely unimportant
and can be looked up in https://github.com/mvo5/oaas/commits/main
if needed.
2024-06-05 18:26:08 +02:00

83 lines
1.8 KiB
Go

package main
import (
"context"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"sync"
"time"
"github.com/sirupsen/logrus"
)
// based on the excellent post
// https://grafana.com/blog/2024/02/09/how-i-write-http-services-in-go-after-13-years/
var logrusNew = logrus.New
func newServer(logger *logrus.Logger, config *Config) http.Handler {
mux := http.NewServeMux()
addRoutes(mux, logger, config)
var handler http.Handler = mux
// todo: consider centralize logginer here?
//handler = loggingMiddleware(handler)
return handler
}
func run(ctx context.Context, args []string, getenv func(string) string) error {
ctx, cancel := signal.NotifyContext(ctx, os.Interrupt)
defer cancel()
logger := logrusNew()
config, err := newConfigFromCmdline(args)
if err != nil {
return err
}
srv := newServer(logger, config)
httpServer := &http.Server{
Addr: net.JoinHostPort(config.Host, config.Port),
Handler: srv,
}
go func() {
logger.Printf("listening on %s\n", httpServer.Addr)
if err := httpServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
fmt.Fprintf(os.Stderr, "error listening and serving: %s\n", err)
}
}()
// todo: this seems kinda complicated, why a waitgroup and not
// do it flat?
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
<-ctx.Done()
shutdownCtx := context.Background()
shutdownCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
if err := httpServer.Shutdown(shutdownCtx); err != nil {
fmt.Fprintf(os.Stderr, "error shutting down http server: %s\n", err)
}
}()
wg.Wait()
// cleanup
if err := os.RemoveAll(config.BuildDirBase); err != nil {
logger.Errorf("cannot cleanup: %v", err)
return err
}
return nil
}
func main() {
ctx := context.Background()
if err := run(ctx, os.Args, os.Getenv); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
}