-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
109 lines (90 loc) · 2.63 KB
/
handlers.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
package main
import (
"encoding/json"
"net/http"
"rebootx-on-prem/http-server-go/resources/dashboard"
"rebootx-on-prem/http-server-go/resources/runnable"
"rebootx-on-prem/http-server-go/utils"
"github.com/gorilla/mux"
"openapi"
)
func getDashboardsHandler(service dashboard.Service) func(w http.ResponseWriter, r *http.Request) {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
encoder := json.NewEncoder(w)
params := openapi.NewListDashboardsQueryParams()
query := r.URL.Query()
limitAsString := query.Get("limit")
limit := utils.ParseInt(&limitAsString)
if limit != nil {
params.SetLimit(*limit)
}
offsetAsString := query.Get("offset")
offset := utils.ParseInt(&offsetAsString)
if offset != nil {
params.SetOffset(*offset)
}
params.SetQ(query.Get("q"))
res, err := service.List(params)
if err != nil {
w.WriteHeader(err.HTTPStatus)
encoder.Encode(openapi.NewErrorRes(err.Error()))
return
}
encoder.Encode(res)
})
}
func getRunnablesHandler(service runnable.Service) func(w http.ResponseWriter, r *http.Request) {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
encoder := json.NewEncoder(w)
params := openapi.NewListRunnablesQueryParams()
query := r.URL.Query()
limitAsString := query.Get("limit")
limit := utils.ParseInt(&limitAsString)
if limit != nil {
params.SetLimit(*limit)
}
offsetAsString := query.Get("offset")
offset := utils.ParseInt(&offsetAsString)
if offset != nil {
params.SetOffset(*offset)
}
params.SetQ(query.Get("q"))
res, err := service.List(params)
if err != nil {
w.WriteHeader(err.HTTPStatus)
encoder.Encode(openapi.NewErrorRes(err.Error()))
return
}
encoder.Encode(res)
})
}
func postRunnableRebootHandler(service runnable.Service) func(w http.ResponseWriter, r *http.Request) {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
encoder := json.NewEncoder(w)
vars := mux.Vars(r)
id := vars["id"]
res, err := service.Reboot(id)
if err != nil {
w.WriteHeader(err.HTTPStatus)
encoder.Encode(openapi.NewErrorRes(err.Error()))
return
}
w.WriteHeader(http.StatusCreated)
encoder.Encode(res)
})
}
func postRunnableStopHandler(service runnable.Service) func(w http.ResponseWriter, r *http.Request) {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
encoder := json.NewEncoder(w)
vars := mux.Vars(r)
id := vars["id"]
res, err := service.Stop(id)
if err != nil {
w.WriteHeader(err.HTTPStatus)
encoder.Encode(openapi.NewErrorRes(err.Error()))
return
}
w.WriteHeader(http.StatusCreated)
encoder.Encode(res)
})
}