This repository has been archived by the owner on May 30, 2022. It is now read-only.
/
app.go
148 lines (122 loc) · 4.75 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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package web
import (
"embed"
"fmt"
"net/http"
"os"
"time"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
"github.com/trento-project/trento/internal/consul"
"github.com/trento-project/trento/web/services"
"github.com/trento-project/trento/web/services/ara"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
_ "github.com/trento-project/trento/docs" // docs is generated by Swag CLI, you have to import it.
)
//go:embed frontend/assets
var assetsFS embed.FS
//go:embed templates
var templatesFS embed.FS
const araAddrDefault = "127.0.0.1:8000"
type App struct {
host string
port int
Dependencies
}
type Dependencies struct {
consul consul.Client
engine *gin.Engine
store cookie.Store
checksService services.ChecksService
subscriptionsService services.SubscriptionsService
hostsService services.HostsService
sapSystemsService services.SAPSystemsService
}
func DefaultDependencies() Dependencies {
consulClient, _ := consul.DefaultClient()
engine := gin.Default()
store := cookie.NewStore([]byte("secret"))
mode := os.Getenv(gin.EnvGinMode)
araService := ara.NewAraService(araAddrDefault)
checksService := services.NewChecksService(araService)
subscriptionsService := services.NewSubscriptionsService(consulClient)
hostsService := services.NewHostsService(consulClient)
sapSystemsService := services.NewSAPSystemsService(consulClient)
gin.SetMode(mode)
return Dependencies{
consulClient, engine, store, checksService,
subscriptionsService, hostsService, sapSystemsService}
}
func (d *Dependencies) SetAraAddr(araAddr string) {
araService := ara.NewAraService(araAddr)
d.checksService = services.NewChecksService(araService)
}
// shortcut to use default dependencies
func NewApp(host string, port int) (*App, error) {
return NewAppWithDeps(host, port, DefaultDependencies())
}
// @title Trento API
// @version 1.0
// @description Trento API
// @contact.name Trento Project
// @contact.url https://www.trento-project.io
// @contact.email trento-project@suse.com
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host localhost:8080
// @BasePath /
// @schemes http
func NewAppWithDeps(host string, port int, deps Dependencies) (*App, error) {
app := &App{
Dependencies: deps,
host: host,
port: port,
}
InitAlerts()
engine := deps.engine
engine.HTMLRender = NewLayoutRender(templatesFS, "templates/*.tmpl")
engine.Use(ErrorHandler)
engine.Use(sessions.Sessions("session", deps.store))
engine.StaticFS("/static", http.FS(assetsFS))
engine.GET("/", HomeHandler)
engine.GET("/about", NewAboutHandler(deps.subscriptionsService))
engine.GET("/hosts", NewHostListHandler(deps.consul))
engine.GET("/hosts/:name", NewHostHandler(deps.consul, deps.subscriptionsService))
engine.GET("/catalog", NewChecksCatalogHandler(deps.checksService))
engine.GET("/clusters", NewClusterListHandler(deps.consul, deps.checksService))
engine.GET("/clusters/:id", NewClusterHandler(deps.consul, deps.checksService))
engine.POST("/clusters/:id/settings", NewSaveClusterSettingsHandler(deps.consul))
engine.GET("/sapsystems", NewSAPSystemListHandler(deps.consul, deps.hostsService, deps.sapSystemsService))
engine.GET("/sapsystems/:sid", NewSAPResourceHandler(deps.hostsService, deps.sapSystemsService))
engine.GET("/databases", NewHanaDatabaseListHandler(deps.consul, deps.hostsService, deps.sapSystemsService))
engine.GET("/databases/:sid", NewSAPResourceHandler(deps.hostsService, deps.sapSystemsService))
engine.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
apiGroup := engine.Group("/api")
{
apiGroup.GET("/ping", ApiPingHandler)
apiGroup.GET("/tags", ApiListTag(deps.consul))
apiGroup.POST("/hosts/:name/tags", ApiHostCreateTagHandler(deps.consul))
apiGroup.DELETE("/hosts/:name/tags/:tag", ApiHostDeleteTagHandler(deps.consul))
apiGroup.POST("/clusters/:id/tags", ApiClusterCreateTagHandler(deps.consul))
apiGroup.DELETE("/clusters/:id/tags/:tag", ApiClusterDeleteTagHandler(deps.consul))
apiGroup.GET("/clusters/:cluster_id/results", ApiClusterCheckResultsHandler(deps.consul, deps.checksService))
apiGroup.POST("/sapsystems/:sid/tags", ApiSAPSystemCreateTagHandler(deps.consul))
apiGroup.DELETE("/sapsystems/:sid/tags/:tag", ApiSAPSystemDeleteTagHandler(deps.consul))
}
return app, nil
}
func (a *App) Start() error {
s := &http.Server{
Addr: fmt.Sprintf("%s:%d", a.host, a.port),
Handler: a,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
return s.ListenAndServe()
}
func (a *App) ServeHTTP(w http.ResponseWriter, req *http.Request) {
a.engine.ServeHTTP(w, req)
}