forked from manifoldco/torus-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.go
78 lines (61 loc) · 2.32 KB
/
routes.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
package routes
import (
"encoding/json"
"net/http"
"github.com/go-zoo/bone"
"github.com/manifoldco/torus-cli/apitypes"
"github.com/manifoldco/torus-cli/config"
"github.com/manifoldco/torus-cli/daemon/db"
"github.com/manifoldco/torus-cli/daemon/logic"
"github.com/manifoldco/torus-cli/daemon/observer"
"github.com/manifoldco/torus-cli/daemon/registry"
"github.com/manifoldco/torus-cli/daemon/session"
)
// NewRouteMux returns a *bone.Mux responsible for handling the cli to daemon
// http api.
func NewRouteMux(c *config.Config, s session.Session, db *db.DB,
t *http.Transport, o *observer.Observer, client *registry.Client, lEngine *logic.Engine) *bone.Mux {
mux := bone.New()
mux.Get("/observe", o)
mux.PostFunc("/signup", signupRoute(client, s, db))
mux.PostFunc("/login", loginRoute(lEngine))
mux.PostFunc("/logout", logoutRoute(lEngine))
mux.GetFunc("/session", sessionRoute(s))
mux.GetFunc("/self", selfRoute(s))
mux.PatchFunc("/self", updateSelfRoute(client, s, lEngine))
mux.PostFunc("/machines", machinesCreateRoute(client, s, lEngine, o))
mux.PostFunc("/keypairs/generate", keypairsGenerateRoute(lEngine, o))
mux.PostFunc("/keypairs/revoke", keypairsRevokeRoute(lEngine, o))
mux.GetFunc("/credentials", credentialsGetRoute(lEngine, o))
mux.PostFunc("/credentials", credentialsPostRoute(lEngine, o))
mux.PostFunc("/org-invites/:id/approve",
orgInvitesApproveRoute(lEngine, o))
mux.GetFunc("/worklog", worklogListRoute(lEngine, o))
mux.GetFunc("/worklog/:id", worklogGetRoute(lEngine, o))
mux.PostFunc("/worklog/:id", worklogResolveRoute(lEngine, o))
mux.GetFunc("/version", func(w http.ResponseWriter, r *http.Request) {
enc := json.NewEncoder(w)
err := enc.Encode(&apitypes.Version{Version: c.Version})
if err != nil {
encodeResponseErr(w, err)
}
})
return mux
}
// if encoding has errored, our struct is either bad, or our writer
// is broken. Try writing an error back to the client, but ignore any
// problems (ie the writer is broken).
func encodeResponseErr(w http.ResponseWriter, err interface{}) {
enc := json.NewEncoder(w)
rErr, ok := err.(*apitypes.Error)
if ok {
w.WriteHeader(rErr.StatusCode)
enc.Encode(rErr)
} else {
w.WriteHeader(http.StatusInternalServerError)
enc.Encode(&errorMsg{
Type: apitypes.InternalServerError,
Error: []string{"Internal server error"},
})
}
}