// Copyright 2022 Terin Stock. // SPDX-License-Identifier: MPL-2.0 package main import ( "flag" "net" "net/http" "net/http/pprof" "os" "strconv" "time" "github.com/gorilla/mux" "github.com/rs/zerolog" "go.terinstock.com/cgit-httpd/handlers/cgit" "go.terinstock.com/cgit-httpd/handlers/git" "go.terinstock.com/cgit-httpd/manager" "go.terinstock.com/cgit-httpd/server" "sigs.k8s.io/controller-runtime/pkg/manager/signals" ) var configFile string func main() { flag.StringVar(&configFile, "config", "./config.edn", "path to cgit-httpd configuration file") flag.Parse() logger := zerolog.New(os.Stdout).With().Logger() cfg, err := readConfig(configFile) if err != nil { logger.Fatal().Err(err).Send() } logger.Info().Interface("config", cfg).Send() gitMux := mux.NewRouter() cgit.New(cgit.Options{ CGI: cfg.CGit.CGI, ReposRoot: cfg.ReposRoot, AssetsDir: cfg.CGit.AssetsDir, ConfigFile: cfg.CGit.ConfigFile, Logger: logger.With().Str("handler", "cgit").Logger(), }). WithRegister(RegistererFunc(gitMux.Handle)). Build() git.New(git.Options{ CGI: cfg.Git.CGI, ReposRoot: cfg.ReposRoot, ExportAll: cfg.Git.ExportAll, Logger: logger.With().Str("handler", "git").Logger(), }). WithRegister(RegistererFunc(gitMux.Handle)). Build() m := manager.New() m.Add(&server.Server{ Name: "git", Server: &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) m.Add(&server.Server{ Name: "pprof", Server: &http.Server{ Addr: net.JoinHostPort(cfg.Pprof.Host, strconv.Itoa(cfg.Pprof.Port)), Handler: mux, }, }) } if err := m.Start(signals.SetupSignalHandler()); err != nil { logger.Info().Err(err).Msg("manager stopped") } } type RegistererFunc func(string, http.Handler) *mux.Route func (r RegistererFunc) Register(name string, handler http.Handler) { r(name, handler) }