// Copyright 2022 Terin Stock. // SPDX-License-Identifier: EUPL-1.2 package main import ( "context" "flag" "log/slog" "net" "net/http" "net/http/pprof" "os" "os/signal" "strconv" "time" "github.com/gorilla/mux" "github.com/thejerf/suture/v4" "github.com/thejerf/sutureslog" "go.terinstock.com/cgit-httpd/handlers/cgit" "go.terinstock.com/cgit-httpd/handlers/git" "go.terinstock.com/cgit-httpd/server" ) var configFile string func main() { flag.StringVar(&configFile, "config", "./config.edn", "path to cgit-httpd configuration file") flag.Parse() logger := slog.New(slog.NewTextHandler(os.Stderr, nil)) slog.SetDefault(logger) cfg, err := readConfig(configFile) if err != nil { logger.Error("could not parse config", "error", err) os.Exit(1) } logger.Info("", "config", cfg) gitMux := mux.NewRouter() git.New(git.Options{ CGI: cfg.Git.CGI, ReposRoot: cfg.ReposRoot, ExportAll: cfg.Git.ExportAll, Logger: logger.With("handler", "git"), }). WithRegister(RegistererFunc(gitMux.Handle)). Build() cgit.New(cgit.Options{ CGI: cfg.CGit.CGI, ReposRoot: cfg.ReposRoot, AssetsDir: cfg.CGit.AssetsDir, ConfigFile: cfg.CGit.ConfigFile, Logger: logger.With("handler", "cgit"), }). WithRegister(RegistererFunc(gitMux.Handle)). Build() supervisor := suture.New("cgit-httpd", suture.Spec{ EventHook: (&sutureslog.Handler{Logger: logger}).MustHook(), }) supervisor.Add(&server.HTTPServer{ NewServer: func() *http.Server { return &http.Server{ Addr: net.JoinHostPort(cfg.HTTP.Host, strconv.Itoa(cfg.HTTP.Port)), Handler: gitMux, MaxHeaderBytes: 1 << 20, IdleTimeout: 90 * time.Second, ReadHeaderTimeout: 32 * time.Second, } }, }) if cfg.Pprof.Port != 0 { mux := http.NewServeMux() mux.HandleFunc("/debug/pprof/", pprof.Index) mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) mux.HandleFunc("/debug/pprof/profile", pprof.Profile) mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol) mux.HandleFunc("/debug/pprof/trace", pprof.Trace) supervisor.Add(&server.HTTPServer{ NewServer: func() *http.Server { return &http.Server{ Addr: net.JoinHostPort(cfg.Pprof.Host, strconv.Itoa(cfg.Pprof.Port)), Handler: mux, } }, }) } ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt) defer cancel() supervisor.Serve(ctx) } type RegistererFunc func(string, http.Handler) *mux.Route func (r RegistererFunc) Register(name string, handler http.Handler) { r(name, handler) }