aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: e47d31104beebcb574084ff904e79bb6f5262cac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2022 Terin Stock.
// SPDX-License-Identifier: MPL-2.0

package main

import (
	"flag"
	"net"
	"net/http"
	"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 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)
}