This repository has been archived by the owner on Apr 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
89 lines (74 loc) · 2.46 KB
/
main.go
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
81
82
83
84
85
86
87
88
89
//go:generate pkger
package main
import (
"flag"
"io"
"net/http"
"os"
"github.com/markbates/pkger"
log "github.com/sirupsen/logrus"
"github.com/saitho/static-git-file-server/config"
"github.com/saitho/static-git-file-server/git"
"github.com/saitho/static-git-file-server/webserver"
)
func initLoggers(cfg *config.Config) {
file, err := os.OpenFile("logs.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Panic(err)
}
var logLevel log.Level
switch cfg.LogLevel {
case "info":
logLevel = log.InfoLevel
case "warning":
logLevel = log.WarnLevel
case "error":
logLevel = log.ErrorLevel
case "panic":
logLevel = log.PanicLevel
default:
logLevel = log.DebugLevel
}
log.SetLevel(logLevel)
mw := io.MultiWriter(os.Stdout, file)
log.SetOutput(mw)
formatter := new(log.TextFormatter)
formatter.ForceColors = true
log.SetFormatter(formatter)
}
func configureRouteRedirects(server *webserver.Webserver, cfg *config.Config) {
// Redirect single-repo path to new multi-repo path
server.AddHandler(`^/webhook/github`, func(resp *webserver.Response, req *webserver.Request) {
http.Redirect(resp, req.Request, "/"+cfg.Git.Repositories[0].Slug+"/webhook/github", http.StatusPermanentRedirect)
})
server.AddHandler(`^/(branch|tag)/(.*)$`, func(resp *webserver.Response, req *webserver.Request) {
http.Redirect(resp, req.Request, "/"+cfg.Git.Repositories[0].Slug+"/"+req.Params[0]+"/"+req.Params[1], http.StatusPermanentRedirect)
})
server.AddHandler(`^/(.+)/(branch|tag)/?$`, func(resp *webserver.Response, req *webserver.Request) {
http.Redirect(resp, req.Request, "/", http.StatusPermanentRedirect)
})
}
func main() {
_ = pkger.Include("/tmpl")
port := flag.String("p", "80", "port to serve on")
configPath := flag.String("c", "config.yml", "path to config file")
flag.Parse()
cfg, err := config.LoadConfig(*configPath)
if err != nil {
panic(err)
}
initLoggers(cfg)
client := &git.Client{Cfg: cfg}
server := webserver.Webserver{
Port: *port,
ConfigPath: *configPath,
}
server.AddHandler(`(?U)^/?$`, webserver.IndexHandler(client))
server.AddHandler(`(?U)^/(.*)/webhook/github`, webserver.GitHubWebHookEndpoint(client))
if cfg.Display.Tags.VirtualTags.EnableSemverMajor {
server.AddHandler(`(?U)^/(.*)/tag/(v?\d+)(/?|/-/(.*))$`, webserver.ResolveVirtualMajorTag(client))
}
server.AddHandler(`(?U)^/(.*)/(branch|tag)/(.*)(/?|/-/(.*))$`, webserver.FileHandler(client))
configureRouteRedirects(&server, cfg)
server.Run()
}