This repository has been archived by the owner on Oct 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
77 lines (64 loc) · 2.39 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
package main
import (
"flag"
"os"
"strings"
"github.com/getlantern/autoupdate-server/instrument"
"github.com/getlantern/autoupdate-server/otel"
"github.com/getlantern/autoupdate-server/server"
"github.com/getlantern/golog"
)
const (
localPatchesDirectory = "./patches/"
)
var (
flagRateLimit = flag.Int("r", 0, "Rate limit. How many updates are allowed to process per second. Defaults to no limit.")
flagPrivateKey = flag.String("k", "", "Path to private key.")
flagLocalAddr = flag.String("l", ":9999", "Local bind address.")
flagPublicAddr = flag.String("p", "http://127.0.0.1:9999/", "Public address.")
flagGithubOrganization = flag.String("o", "getlantern", "Github organization. For back compatibility to old clients hitting /update endpoint.")
flagGithubProject = flag.String("n", "lantern", "Github project name. For back compatibility to old clients hitting /update endpoint.")
flagRepos = flag.String("repos", "lantern:getlantern/lantern", "Comma separated mapping of update path to Github repos mapping. The format looks like this 'app1:owner1/repo1,app2:owner2/repo2'")
flagHelp = flag.Bool("h", false, "Shows help.")
)
var (
log = golog.LoggerFor("autoupdate-server")
)
func main() {
// Parsing flags
flag.Parse()
if *flagHelp || *flagPrivateKey == "" {
flag.Usage()
os.Exit(0)
}
server.SetPrivateKey(*flagPrivateKey)
tp, stop := otel.BuildTracerProvider(&otel.Opts{
Endpoint: "api.honeycomb.io:443",
Headers: map[string]string{
"x-honeycomb-team": os.Getenv("HONEYCOMB_API_KEY"),
},
})
defer stop()
otelHandler, stopTracing := instrument.NewOTELMiddleware(tp)
defer stopTracing()
updateServer := server.NewUpdateServer(*flagPublicAddr, *flagLocalAddr, localPatchesDirectory, *flagRateLimit)
for _, mapping := range strings.Split(*flagRepos, ",") {
fatal := func() { log.Fatalf("expect repo string in 'app:owner/repo' format, got '%s'", mapping) }
pair := strings.Split(mapping, ":")
if len(pair) != 2 {
fatal()
}
app := pair[0]
parts := strings.Split(pair[1], "/")
if len(parts) != 2 {
fatal()
}
repo := parts[1]
updateServer.HandleRepo(app, parts[0], repo, otelHandler)
}
// back compatibility
updateServer.HandleRepo("", *flagGithubOrganization, *flagGithubProject, otelHandler)
if err := updateServer.ListenAndServe(); err != nil {
log.Fatalf("ListenAndServe: ", err)
}
}