-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.go
164 lines (141 loc) · 4.87 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"context"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"time"
"github.com/subutai-io/agent/log"
"github.com/jasonlvhit/gocron"
"github.com/subutai-io/cdn/apt"
"github.com/subutai-io/cdn/auth"
"github.com/subutai-io/cdn/config"
"github.com/subutai-io/cdn/db"
"github.com/subutai-io/cdn/raw"
"github.com/subutai-io/cdn/template"
"github.com/subutai-io/cdn/upload"
)
var version = "6.3.0"
var (
srv *http.Server
testMode bool = false
stop chan bool
)
func RunTask() {
gocron.Every(6).Hours().Do(apt.GenerateReleaseFile)
<-gocron.Start()
}
func main() {
defer db.Close()
// defer torrent.Close()
// go torrent.SeedLocal()
go RunTask()
if len(config.CDN.Node) > 0 {
target := url.URL{Scheme: "https", Host: config.CDN.Node}
proxy := httputil.NewSingleHostReverseProxy(&target)
targetQuery := target.RawQuery
proxy.Director = func(req *http.Request) {
req.URL.Scheme = target.Scheme
req.URL.Host = target.Host
req.Host = config.CDN.Node
req.URL.Path = singleJoiningSlash(target.Path, req.URL.Path)
if targetQuery == "" || req.URL.RawQuery == "" {
req.URL.RawQuery = targetQuery + req.URL.RawQuery
} else {
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
}
if _, ok := req.Header["User-Agent"]; !ok {
// explicitly disable User-Agent so it's not set to default value
req.Header.Set("User-Agent", "")
}
}
log.Check(log.ErrorLevel, "Starting to listen :"+config.Network.Port, http.ListenAndServe(":"+config.Network.Port, proxy))
return
}
log.Info("Server has started. " + "Listening at " + "127.0.0.1:8080")
http.HandleFunc("/kurjun/rest/file/get", raw.Download)
http.HandleFunc("/kurjun/rest/file/info", raw.Info)
http.HandleFunc("/kurjun/rest/raw/get", raw.Download)
http.HandleFunc("/kurjun/rest/template/get", template.Download)
http.HandleFunc("/kurjun/rest/apt/", apt.Download)
http.HandleFunc("/kurjun/rest/apt/info", apt.Info)
http.HandleFunc("/kurjun/rest/apt/list", apt.List)
http.HandleFunc("/kurjun/rest/apt/delete", apt.Delete)
http.HandleFunc("/kurjun/rest/apt/upload", apt.Upload)
http.HandleFunc("/kurjun/rest/apt/download", apt.Download)
http.HandleFunc("/kurjun/rest/apt/generate", apt.Generate)
http.HandleFunc("/kurjun/rest/raw/", raw.Download)
http.HandleFunc("/kurjun/rest/raw/info", raw.Info)
http.HandleFunc("/kurjun/rest/raw/list", raw.List)
http.HandleFunc("/kurjun/rest/raw/delete", raw.Delete)
http.HandleFunc("/kurjun/rest/raw/upload", raw.Upload)
http.HandleFunc("/kurjun/rest/raw/download", raw.Download)
http.HandleFunc("/kurjun/rest/template/", template.Download)
http.HandleFunc("/kurjun/rest/template/tag", template.Tag)
http.HandleFunc("/kurjun/rest/template/info", template.Info)
http.HandleFunc("/kurjun/rest/template/list", template.List)
http.HandleFunc("/kurjun/rest/template/update", template.ModifyConfig)
http.HandleFunc("/kurjun/rest/template/delete", template.Delete)
http.HandleFunc("/kurjun/rest/template/upload", template.Upload)
http.HandleFunc("/kurjun/rest/template/download", template.Download)
// http.HandleFunc("/kurjun/rest/template/torrent", template.Torrent)
http.HandleFunc("/kurjun/rest/auth/key", auth.Key)
http.HandleFunc("/kurjun/rest/auth/keys", auth.Keys)
http.HandleFunc("/kurjun/rest/auth/sign", auth.Sign)
http.HandleFunc("/kurjun/rest/auth/owner", auth.Owner)
http.HandleFunc("/kurjun/rest/auth/token", auth.Token)
http.HandleFunc("/kurjun/rest/auth/register", auth.Register)
http.HandleFunc("/kurjun/rest/auth/validate", auth.Validate)
http.HandleFunc("/kurjun/rest/healthcheck", healthcheck)
http.HandleFunc("/kurjun/rest/share", upload.Share)
http.HandleFunc("/kurjun/rest/quota", upload.Quota)
http.HandleFunc("/kurjun/rest/about", about)
if testMode {
http.HandleFunc("/kurjun/rest/shutdown", shutdown)
}
srv = &http.Server{
Addr: ":" + config.Network.Port,
Handler: nil,
}
srv.ListenAndServe()
}
func shutdown(w http.ResponseWriter, r *http.Request) {
log.Info("Shutting down the server")
stop <- true
}
func about(w http.ResponseWriter, r *http.Request) {
if strings.Split(r.RemoteAddr, ":")[0] == "127.0.0.1" {
_, err := w.Write([]byte(version))
log.Check(log.DebugLevel, "Writing Kurjun version", err)
} else {
w.WriteHeader(http.StatusUnauthorized)
}
}
func singleJoiningSlash(a, b string) string {
aslash := strings.HasSuffix(a, "/")
bslash := strings.HasPrefix(b, "/")
switch {
case aslash && bslash:
return a + b[1:]
case !aslash && !bslash:
return a + "/" + b
}
return a + b
}
func healthcheck(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}
func runMain() {
// start the stop channel
stop = make(chan bool)
// put the service in "testMode"
testMode = true
// run the main entry point
go main()
// watch for the stop channel
<-stop
// stop the graceful server
ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
srv.Shutdown(ctx)
}