This repository has been archived by the owner on Apr 14, 2019. It is now read-only.
/
home-routes.go
77 lines (65 loc) · 1.75 KB
/
home-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
package main
import (
"net/http"
"strings"
"github.com/go-chi/chi"
"github.com/go-chi/jwtauth"
"github.com/go-chi/render"
"github.com/stefanprodan/syros/models"
)
func (s *HttpServer) homeRoutes() chi.Router {
r := chi.NewRouter()
// JWT protected
r.Group(func(r chi.Router) {
r.Use(jwtauth.Verifier(s.TokenAuth))
r.Use(jwtauth.Authenticator)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
dockerStats, err := s.Repository.EnvironmentHostContainerSum()
if err != nil {
render.Status(r, http.StatusInternalServerError)
render.PlainText(w, r, err.Error())
return
}
vsphere, err := s.Repository.AllVSphere()
if err != nil {
render.Status(r, http.StatusInternalServerError)
render.PlainText(w, r, err.Error())
return
}
hs := make([]models.VSphereHost, 0)
for _, d := range vsphere.Hosts {
d.Name = strings.Split(d.Name, ".")[0]
if d.VMs > 0 {
hs = append(hs, d)
}
}
data := struct {
DockerStats []models.EnvironmentStats `json:"docker"`
VSphereHosts []models.VSphereHost `json:"vsphere"`
}{
DockerStats: dockerStats,
VSphereHosts: hs,
}
render.JSON(w, r, data)
})
r.Get("/syrosservices", func(w http.ResponseWriter, r *http.Request) {
services, err := s.Repository.AllSyrosServices()
if err != nil {
render.Status(r, http.StatusInternalServerError)
render.PlainText(w, r, err.Error())
return
}
render.JSON(w, r, services)
})
})
r.Get("/environments", func(w http.ResponseWriter, r *http.Request) {
environments, err := s.Repository.AllEnvironments()
if err != nil {
render.Status(r, http.StatusInternalServerError)
render.PlainText(w, r, err.Error())
return
}
render.JSON(w, r, environments)
})
return r
}