This repository has been archived by the owner on Mar 30, 2020. It is now read-only.
/
server.go
170 lines (142 loc) · 4.19 KB
/
server.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
165
166
167
168
169
170
package http
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"github.com/mcuadros/dockership/config"
"github.com/mcuadros/dockership/core"
"github.com/gorilla/mux"
"gopkg.in/igm/sockjs-go.v2/sockjs"
)
var configFile string
func Start(version, build string) {
core.Info("Starting HTTP daemon", "version", version, "build", build)
flag.StringVar(&configFile, "config", config.DefaultConfig, "config file")
flag.Parse()
s := &server{serverID: fmt.Sprintf("dockership %s, build %s", version, build)}
s.readConfig(configFile)
s.configure()
s.configStaticAssets()
s.configureAuth()
s.run()
}
type server struct {
serverID string
sockjs *SockJS
mux *mux.Router
oauth *OAuth
config config.Config
}
func (s *server) configure() {
s.sockjs = NewSockJS()
s.mux = mux.NewRouter()
s.sockjs.AddHandler("connect", s.HandleConnect)
s.sockjs.AddHandler("containers", s.HandleContainers)
s.sockjs.AddHandler("status", s.HandleStatus)
s.sockjs.AddHandler("deploy", s.HandleDeploy)
// socket
s.mux.Path("/socket/{any:.*}").Handler(sockjs.NewHandler("/socket", sockjs.DefaultOptions, func(session sockjs.Session) {
s.sockjs.AddSessionAndRead(session)
}))
// logged-user
s.mux.Path("/rest/user").Methods("GET").HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
user, _ := s.oauth.getValidUser(s.oauth.getToken(r))
s.json(w, 200, user)
},
)
s.mux.Path("/rest/projects").Methods("GET").HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
s.json(w, 200, s.config.Projects)
},
)
s.mux.Path("/rest/status").Methods("GET").HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
s.json(w, 200, s.GetStatus(""))
},
)
s.mux.Path("/rest/status/{project}").Methods("GET").HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
s.json(w, 200, s.GetStatus(mux.Vars(r)["project"]))
},
)
s.mux.Path("/rest/deploy/{project}/{environment}").Methods("GET").HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
status := 200
result := s.DoDeploy(ioutil.Discard, vars["project"], vars["environment"], true)
if !result.Done {
status = 500
}
s.json(w, status, result)
},
)
}
func (s *server) configStaticAssets() {
s.mux.Path("/").Methods("GET").HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
content, _ := Asset("static/index.html")
w.Write(content)
},
)
s.mux.Path("/dockership.png").Methods("GET").HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/png")
content, _ := Asset("static/dockership.png")
w.Write(content)
},
)
s.mux.Path("/logo.png").Methods("GET").HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/png")
content, _ := Asset("static/logo.png")
w.Write(content)
},
)
s.mux.Path("/app.js").Methods("GET").HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/javascript")
content, _ := Asset("static/app.js")
w.Write(content)
},
)
s.mux.Path("/app.css").Methods("GET").HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/css")
content, _ := Asset("static/app.css")
w.Write(content)
},
)
}
func (s *server) configureAuth() {
s.oauth = NewOAuth(&s.config)
}
func (s *server) readConfig(configFile string) {
if err := s.config.LoadFile(configFile); err != nil {
panic(err)
}
}
func (s *server) run() {
writer := NewSockJSWriter(s.sockjs, "log")
subs := subscribeWriteToEvents(writer)
defer unsubscribeEvents(subs)
core.Info("HTTP server running", "host:port", s.config.HTTP.Listen)
if err := http.ListenAndServe(s.config.HTTP.Listen, s); err != nil {
panic(err)
}
}
func (s *server) json(w http.ResponseWriter, code int, response interface{}) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
encoder := json.NewEncoder(w)
encoder.Encode(response)
}
func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if s.oauth.Handler(w, r) {
core.Debug("Handling request", "url", r.URL)
w.Header().Set("Server", s.serverID)
s.mux.ServeHTTP(w, r)
}
}