forked from bmorton/deployster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
48 lines (40 loc) · 1.54 KB
/
service.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
package main
import (
"github.com/bmorton/deployster/fleet"
"github.com/rcrowley/go-tigertonic"
"net/http"
)
type DeploysterService struct {
AppVersion string
Listen string
RootMux *tigertonic.TrieServeMux
Mux *tigertonic.TrieServeMux
Server *tigertonic.Server
}
func NewDeploysterService(listen string, version string) *DeploysterService {
service := DeploysterService{Listen: listen, AppVersion: version}
service.RootMux = tigertonic.NewTrieServeMux()
service.Mux = tigertonic.NewTrieServeMux()
service.RootMux.HandleNamespace("/v1", service.Mux)
service.Server = tigertonic.NewServer(service.Listen, tigertonic.ApacheLogged(service.RootMux))
service.ConfigureRoutes()
return &service
}
func (self *DeploysterService) ConfigureRoutes() {
fleetClient := fleet.NewClient("/var/run/fleet.sock")
deploys := DeploysResource{&fleetClient}
units := UnitsResource{&fleetClient}
self.Mux.Handle("GET", "/version", authenticated(tigertonic.Version(self.AppVersion)))
self.Mux.Handle("POST", "/services/{name}/deploys", authenticated(tigertonic.Marshaled(deploys.Create)))
self.Mux.Handle("DELETE", "/services/{name}/deploys/{version}", authenticated(tigertonic.Marshaled(deploys.Destroy)))
self.Mux.Handle("GET", "/services/{name}/units", authenticated(tigertonic.Marshaled(units.Index)))
}
func (self *DeploysterService) ListenAndServe() {
self.Server.ListenAndServe()
}
func authenticated(h http.Handler) tigertonic.FirstHandler {
return tigertonic.HTTPBasicAuth(
map[string]string{username: password},
"Deployster",
h)
}