forked from isotopsweden/docker-farmer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
125 lines (101 loc) · 3.17 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
package main
import (
"encoding/json"
"flag"
"fmt"
"html/template"
"log"
"net/http"
"os"
"github.com/kardianos/osext"
"github.com/stfturist/docker-farmer/config"
"github.com/stfturist/docker-farmer/handlers"
)
var (
configFlag = flag.String("config", "config.json", "Path to config file")
publicFlag = flag.String("public", "public", "Path to public directory")
)
// path will return right path for file, looks at the
// given file first and then looks in the executable folder.
func realpath(file string) string {
if _, err := os.Stat(file); err == nil {
return file
}
path, _ := osext.ExecutableFolder()
if _, err := os.Stat(path + "/" + file); os.IsNotExist(err) {
return file
}
return path + "/" + file
}
// ipAllowed will determine if the request ip is allowed
// or not.
func ipAllowedMiddleware(c *config.Config, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var ip string
if o1 := r.Header.Get("X-Forwarded-For"); o1 != "" {
ip = o1
} else if o2 := r.Header.Get("x-forwarded-for"); o2 != "" {
ip = o2
} else if o3 := r.Header.Get("X-FORWARDED-FOR"); o3 != "" {
ip = o3
}
if len(c.AllowedIPs) == 0 {
next.ServeHTTP(w, r)
return
}
for _, i := range c.AllowedIPs {
if ip == i {
next.ServeHTTP(w, r)
return
}
}
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(http.StatusText(http.StatusUnauthorized)))
})
}
func main() {
flag.Parse()
// Init config.
config.Init(realpath(*configFlag))
c := config.Get()
if c.Listen[0] == ':' {
c.Listen = "0.0.0.0" + c.Listen
}
// Index route.
http.Handle("/", ipAllowedMiddleware(c, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
links, err := json.Marshal(c.Links)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
var styles string
for key := range c.Style {
styles += fmt.Sprintf("%s { %s }\n", key, c.Style[key])
}
templates := template.Must(template.ParseFiles(realpath(*publicFlag) + "/index.html"))
err = templates.ExecuteTemplate(w, "index.html", map[string]interface{}{
"Config": c,
"Links": template.JS(string(links)),
"Styles": template.CSS(styles),
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})))
routes := map[string]http.Handler{
"/assets/": http.StripPrefix("/assets/", http.FileServer(http.Dir(realpath(*publicFlag)+"/assets"))),
"/api/config": http.HandlerFunc(handlers.ConfigHandler),
"/api/containers": http.HandlerFunc(handlers.ContainersHandler),
"/api/database": http.HandlerFunc(handlers.DatabaseHandler),
"/services/bitbucket": http.HandlerFunc(handlers.BitbucketHandler),
"/services/github": http.HandlerFunc(handlers.GithubHandler),
"/services/gitlab": http.HandlerFunc(handlers.GitlabHandler),
"/services/jira": http.HandlerFunc(handlers.JiraHandler),
"/services/test": http.HandlerFunc(handlers.TestHandler),
}
for path, handler := range routes {
http.Handle(path, ipAllowedMiddleware(c, handler))
}
fmt.Printf("Listening to http://%s\n", c.Listen)
// Listen to port.
log.Fatal(http.ListenAndServe(c.Listen, nil))
}