-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
104 lines (81 loc) · 2.31 KB
/
main.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
package main
import (
"github.com/eirsyl/statuspage/src"
"github.com/eirsyl/statuspage/src/routes"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/go-pg/pg"
"log"
"net/http"
"os"
"runtime"
)
func main() {
ConfigRuntime()
gin.SetMode(gin.ReleaseMode)
router := gin.Default()
router.Use(State())
binding.Validator.RegisterValidation("incidentstatus", src.IncidentStatus)
binding.Validator.RegisterValidation("servicestatus", src.ServiceStatus)
router.Static("/static", "./static")
router.LoadHTMLGlob("templates/*")
router.GET("/", routes.Dashboard)
api := router.Group("/api")
api.Use(Auth())
{
api.GET("/services", routes.ServiceList)
api.POST("/services", routes.ServicePost)
api.GET("/services/:id", routes.ServiceGet)
api.PATCH("/services/:id", routes.ServicePatch)
api.DELETE("/services/:id", routes.ServiceDelete)
api.GET("/incidents", routes.IncidentList)
api.POST("/incidents", routes.IncidentPost)
api.GET("/incidents/:id", routes.IncidentGet)
api.DELETE("/incidents/:id", routes.IncidentDelete)
api.GET("/incidents/:id/updates", routes.IncidentUpdateList)
api.POST("/incidents/:id/updates", routes.IncidentUpdatePost)
api.GET("/incidents/:id/updates/:updateId", routes.IncidentUpdateGet)
api.DELETE("/incidents/:id/updates/:updateId", routes.IncidentUpdateDelete)
}
router.Run()
}
func ConfigRuntime() {
nuCPU := runtime.NumCPU()
runtime.GOMAXPROCS(nuCPU)
log.Printf("Running with %d CPUs\n", nuCPU)
}
func State() gin.HandlerFunc {
pgAddr := os.Getenv("POSTGRES_ADDRESS")
pgUser := os.Getenv("POSTGRES_USER")
pgPassword := os.Getenv("POSTGRES_PASSWORD")
pgDB := os.Getenv("POSTGRES_DB")
db := pg.Connect(&pg.Options{
Addr: pgAddr,
User: pgUser,
Password: pgPassword,
Database: pgDB,
})
if err := src.CreateSchema(db); err != nil {
panic(err)
}
services := src.Services{}
services.Initialize(*db)
incidents := src.Incidents{}
incidents.Initialize(*db)
return func(c *gin.Context) {
c.Set("services", services)
c.Set("incidents", incidents)
c.Next()
}
}
func Auth() gin.HandlerFunc {
return func(c *gin.Context) {
token := c.GetHeader("Authorization")
validToken := os.Getenv("API_TOKEN")
if !(len(validToken) > 0 && token == validToken) {
c.AbortWithStatus(http.StatusUnauthorized)
return
}
c.Next()
}
}