This repository has been archived by the owner on Jun 2, 2023. It is now read-only.
/
main.go
78 lines (68 loc) · 2.09 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
package main
import (
"fmt"
"net/http"
"os"
"strings"
"time"
"github.com/bakito/jenkins-update-center-proxy/pkg/handler"
"github.com/bakito/jenkins-update-center-proxy/version"
"github.com/gorilla/mux"
"go.uber.org/zap"
)
const (
envRepoProxyURL = "REPO_PROXY_URL"
envUseRepoProxyForDownload = "USE_REPO_PROXY_FOR_DOWNLOAD"
envPort = "PORT"
envOfflineDir = "OFFLINE_DIR"
envContextPath = "CONTEXT_PATH"
envInsecureSkipVerify = "TLS_INSECURE_SKIP_VERIFY"
envTimeout = "TIMEOUT"
envDebugOutput = "DEBUG_OUTPUT"
)
func main() {
lc := zap.NewDevelopmentConfig()
if strings.EqualFold(os.Getenv(envDebugOutput), "true") {
lc.Level = zap.NewAtomicLevelAt(zap.DebugLevel)
} else {
lc.Level = zap.NewAtomicLevelAt(zap.InfoLevel)
}
logger, _ := lc.Build()
log := logger.Sugar()
repoProxyURL := os.Getenv(envRepoProxyURL)
if repoProxyURL == "" {
log.Error("env variable %s is required", envRepoProxyURL)
os.Exit(1)
}
port := "8080"
if p, ok := os.LookupEnv(envPort); ok {
port = p
}
contextPath := "/"
if cp, ok := os.LookupEnv(envContextPath); ok {
if !strings.HasPrefix(cp, "/") {
cp = "/" + cp
}
contextPath = strings.TrimSuffix(cp, "/")
}
timeoutString := "1m"
if to, ok := os.LookupEnv(envTimeout); ok {
timeoutString = to
}
timeout, err := time.ParseDuration(timeoutString)
if err != nil {
log.Error("timeout %q from env var %q is invalid", timeoutString, envTimeout)
os.Exit(1)
}
log.With("version", version.Version, "port", port, "contextPath", contextPath, "timeout", timeoutString).Info("Starting server")
useProxyForDownload := strings.EqualFold("true", os.Getenv(envUseRepoProxyForDownload))
insecureSkipVerify := strings.EqualFold("true", os.Getenv(envInsecureSkipVerify))
offlineDir := os.Getenv(envOfflineDir)
r := mux.NewRouter()
h := handler.New(r, contextPath, repoProxyURL, useProxyForDownload, insecureSkipVerify, offlineDir, timeout)
defer h.Close()
http.Handle("/", r)
if err := http.ListenAndServe(fmt.Sprintf(":%s", port), nil); err != nil {
panic(err)
}
}