forked from purpleworks/fleet-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
68 lines (56 loc) · 1.65 KB
/
app.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
package main
import (
"flag"
"fmt"
"github.com/codegangsta/negroni"
"github.com/gorilla/mux"
"gopkg.in/unrolled/render.v1"
"os"
)
var (
renderer *render.Render
fleetClient FleetClient
tempDir string
etcdPeer string
etcdPeerFlag = flag.String("etcd-peer", "172.17.42.1", "ETCD_PEER")
)
func init() {
// parse command argument
flag.Parse()
if v := os.Getenv("ETCD_PEER"); v != "" {
etcdPeer = v
} else {
etcdPeer = *etcdPeerFlag
}
// init global variables
renderer = render.New(render.Options{})
fleetClient = NewClientCLIWithPeer(fmt.Sprintf("%s", etcdPeer))
tempDir = "./tmp"
if _, err := os.Stat(tempDir); os.IsNotExist(err) {
os.Mkdir(tempDir, 0755)
}
}
func main() {
r := mux.NewRouter().StrictSlash(false)
api := r.PathPrefix("/api/v1").Subrouter()
// routing machines collection
machines := api.Path("/machines").Subrouter()
machines.Methods("GET").HandlerFunc(machineAllHandler)
// routing units collection
units := api.Path("/units").Subrouter()
units.Methods("GET").HandlerFunc(statusAllHandler)
units.Methods("POST").HandlerFunc(submitUnitHandler)
api.Path("/units/upload").Methods("POST").HandlerFunc(uploadUnitHandler)
// routing units singular
unit := api.PathPrefix("/units/{id}").Subrouter()
unit.Methods("GET").HandlerFunc(statusHandler)
unit.Methods("DELETE").HandlerFunc(destroyHandler)
unit.Path("/start").Methods("POST").HandlerFunc(startHandler)
unit.Path("/stop").Methods("POST").HandlerFunc(stopHandler)
unit.Path("/load").Methods("POST").HandlerFunc(loadHandler)
// routing websocket
r.Path("/ws/journal/{id}").HandlerFunc(wsHandler)
n := negroni.Classic()
n.UseHandler(r)
n.Run(":3000")
}