-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
63 lines (55 loc) · 1.42 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
package main
import (
"crypto/tls"
"flag"
"fmt"
"log"
"net/http"
"time"
)
func main() {
help := flag.Bool("help", false, "Show usage")
url := flag.String("url", "", "GitLab API url including version string ie. https://gitlab.com/api/v3/")
token := flag.String("token", "", "GitLab API token")
verify := flag.Bool("verify-ssl", true, "GitLab API verify SSL")
httpAddr := flag.String("addr", ":7070", "HTTP server address")
interval := flag.Uint("interval", 5, "Interval between updates")
flag.Parse()
if *help == true || *url == "" || *token == "" || *httpAddr == "" {
flag.Usage()
return
}
g := &GitLab{
Url: *url,
Token: *token,
Client: &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: !*verify,
},
},
},
}
c := NewComposerRepository(g)
go func() {
for {
log.Println("Fetching data...")
if err := c.Update(); err != nil {
log.Println(err)
}
time.Sleep(time.Duration(*interval) * time.Minute)
}
}()
http.HandleFunc("/packages.json", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
x, err := c.Content()
if err != nil {
http.Error(w, fmt.Sprintf("%v", err), 500)
}
http.ServeContent(w, r, "packages.json", c.ModifiedTime(), x)
})
log.Printf("Server Listening on %v", *httpAddr)
if err := http.ListenAndServe(*httpAddr, nil); err != nil {
log.Fatal(err)
}
}