-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implment the incident frontend, service api and update readme
- Loading branch information
Showing
11 changed files
with
355 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,15 @@ | ||
# Statuspage | ||
> Framework used to create a personalized statuspage. | ||
> Self hosted status page written in golang! | ||
 | ||
|
||
This is a small status page project with Postgres as the backing datastore. | ||
There should be no need to change the go code to customize the page. | ||
We use environment variables for configuration, this also includes the logo. | ||
|
||
## Todo | ||
|
||
* API authorization | ||
* API payload validation | ||
* Incident API | ||
* Customize page with env variables |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
version: '2' | ||
|
||
services: | ||
redis: | ||
image: redis:latest | ||
postgres: | ||
image: postgres:9.5 | ||
ports: | ||
- '127.0.0.1:6379:6379' | ||
- '127.0.0.1:5432:5432' | ||
environment: | ||
- POSTGRES_USER=statuspage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,65 @@ | ||
package src | ||
|
||
import ( | ||
"github.com/go-redis/redis" | ||
"time" | ||
"github.com/go-pg/pg" | ||
) | ||
|
||
type Incident struct { | ||
Id string | ||
Id int64 | ||
Time time.Time | ||
Title string | ||
Resolved time.Time | ||
Updates []*IncidentUpdate | ||
} | ||
|
||
type IncidentUpdate struct { | ||
Id string | ||
Incident string | ||
Id int64 | ||
Time time.Time | ||
IncidentId int64 | ||
Status string | ||
Message string | ||
} | ||
|
||
type Incidents struct { | ||
db redis.Client | ||
db pg.DB | ||
} | ||
|
||
func (i *Incidents) Initialize(db redis.Client) { | ||
func (i *Incidents) Initialize(db pg.DB) { | ||
i.db = db | ||
} | ||
|
||
func (i *Incidents) CreateIncident(incident Incident) error { | ||
return nil | ||
func (i *Incidents) InsertIncident(incident Incident) error { | ||
if incident.Time.IsZero() { | ||
now := time.Now() | ||
incident.Time = now | ||
} | ||
err := i.db.Insert(&incident) | ||
return err | ||
} | ||
|
||
func (i *Incidents) InsertIncidentUpdate(incident int64, update IncidentUpdate) error { | ||
update.IncidentId = incident | ||
|
||
if update.Time.IsZero() { | ||
now := time.Now() | ||
update.Time = now | ||
} | ||
|
||
err := i.db.Insert(&update) | ||
return err | ||
} | ||
|
||
func (i *Incidents) CreateIncidentUpdate(incident string, update IncidentUpdate) error { | ||
return nil | ||
func (i *Incidents) GetLatestIncidents() ([]Incident, error) { | ||
to := time.Now() | ||
from := to.Add(-14 * 24 * time.Hour).Truncate(24 * time.Hour) | ||
|
||
var incidents []Incident | ||
|
||
err := i.db.Model(&incidents). | ||
Column("incident.*", "Updates"). | ||
Where("time > ?", from). | ||
Where("time < ?", to). | ||
Select() | ||
|
||
return incidents, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package routes | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/eirsyl/statuspage/src" | ||
"net/http" | ||
"strconv" | ||
) | ||
|
||
func ServiceList(c *gin.Context) { | ||
services, _ := c.Keys["services"].(src.Services) | ||
|
||
s, err := services.GetServices(true) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
c.JSON(http.StatusOK, s) | ||
} | ||
|
||
func ServicePost(c *gin.Context) { | ||
var service src.Service | ||
err := c.BindJSON(&service) | ||
if err != nil { | ||
c.AbortWithStatus(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
services, _ := c.Keys["services"].(src.Services) | ||
|
||
err = services.InsertService(service) | ||
if err != nil { | ||
c.AbortWithStatus(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusCreated, service) | ||
} | ||
|
||
func ServiceGet(c *gin.Context) { | ||
serviceId := c.Param("id") | ||
id, err := strconv.Atoi(serviceId) | ||
if err != nil { | ||
c.AbortWithStatus(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
services, _ := c.Keys["services"].(src.Services) | ||
|
||
s, err := services.GetService(int64(id)) | ||
if err != nil { | ||
c.AbortWithStatus(http.StatusNotFound) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, s) | ||
} | ||
|
||
func ServicePatch(c *gin.Context) { | ||
serviceId := c.Param("id") | ||
id, err := strconv.Atoi(serviceId) | ||
if err != nil { | ||
c.AbortWithStatus(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
var service src.Service | ||
err = c.BindJSON(&service) | ||
if err != nil { | ||
c.AbortWithStatus(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
services, _ := c.Keys["services"].(src.Services) | ||
|
||
err = services.UpdateService(int64(id), service) | ||
if err != nil { | ||
c.AbortWithStatus(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
c.JSON(http.StatusCreated, service) | ||
} | ||
|
||
func ServiceDelete(c *gin.Context) { | ||
serviceId := c.Param("id") | ||
id, err := strconv.Atoi(serviceId) | ||
if err != nil { | ||
c.AbortWithStatus(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
services, _ := c.Keys["services"].(src.Services) | ||
|
||
err = services.DeleteService(int64(id)) | ||
if err != nil { | ||
c.AbortWithStatus(http.StatusNotFound) | ||
return | ||
} | ||
|
||
c.AbortWithStatus(http.StatusNoContent) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package routes | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/eirsyl/statuspage/src" | ||
"net/http" | ||
) | ||
|
||
func Dashboard(c *gin.Context) { | ||
services, _ := c.Keys["services"].(src.Services) | ||
incidents, _ := c.Keys["incidents"].(src.Incidents) | ||
|
||
res, err := services.GetServices(true) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
inc, err := incidents.GetLatestIncidents() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
c.HTML(http.StatusOK, "index.tmpl", gin.H{ | ||
"owner": "Abakus", | ||
"services": src.AggregateServices(res), | ||
"mostCriticalStatus": src.MostCriticalStatus(res), | ||
"incidents": src.AggregateIncidents(inc), | ||
}) | ||
} |
Oops, something went wrong.