statping/handlers/api.go

185 lines
4.2 KiB
Go
Raw Normal View History

2018-06-30 00:57:05 +00:00
package handlers
2018-06-15 04:30:10 +00:00
import (
2018-11-25 03:56:09 +00:00
"fmt"
2020-03-09 18:17:55 +00:00
"github.com/statping/statping/types/checkins"
2020-09-14 23:00:35 +00:00
"github.com/statping/statping/types/configs"
2020-03-09 18:17:55 +00:00
"github.com/statping/statping/types/core"
2020-04-17 03:21:17 +00:00
"github.com/statping/statping/types/errors"
2020-03-09 18:17:55 +00:00
"github.com/statping/statping/types/groups"
"github.com/statping/statping/types/incidents"
"github.com/statping/statping/types/messages"
2020-03-14 03:13:20 +00:00
"github.com/statping/statping/types/notifications"
2020-03-09 18:17:55 +00:00
"github.com/statping/statping/types/null"
"github.com/statping/statping/types/services"
"github.com/statping/statping/types/users"
"github.com/statping/statping/utils"
2018-06-25 07:09:31 +00:00
"net/http"
2020-02-04 06:37:32 +00:00
"time"
2018-06-15 04:30:10 +00:00
)
2018-10-06 05:56:08 +00:00
type apiResponse struct {
2018-11-17 17:14:14 +00:00
Status string `json:"status"`
Object string `json:"type,omitempty"`
Method string `json:"method,omitempty"`
2020-04-17 03:21:17 +00:00
Error error `json:"error,omitempty"`
2018-11-17 17:14:14 +00:00
Id int64 `json:"id,omitempty"`
Output interface{} `json:"output,omitempty"`
}
2020-01-20 05:02:15 +00:00
func apiIndexHandler(r *http.Request) interface{} {
return core.App
2018-06-15 04:30:10 +00:00
}
func apiRenewHandler(w http.ResponseWriter, r *http.Request) {
2020-08-26 10:48:05 +00:00
newApi := utils.Params.GetString("API_SECRET")
if newApi == "" {
newApi = utils.NewSHA256Hash()
}
core.App.ApiSecret = newApi
if err := core.App.Update(); err != nil {
2018-11-13 19:28:21 +00:00
sendErrorJson(err, w, r)
return
2018-07-17 09:18:20 +00:00
}
2020-02-19 04:07:22 +00:00
output := apiResponse{
Status: "success",
}
returnJson(output, w, r)
2018-07-17 09:18:20 +00:00
}
func apiUpdateOAuthHandler(w http.ResponseWriter, r *http.Request) {
2020-06-26 04:51:51 +00:00
var oauth core.OAuth
if err := DecodeJSON(r, &oauth); err != nil {
sendErrorJson(err, w, r)
return
}
2020-06-26 00:15:59 +00:00
2020-06-26 04:51:51 +00:00
core.App.OAuth = oauth
if err := core.App.Update(); err != nil {
2020-06-26 00:15:59 +00:00
sendErrorJson(err, w, r)
return
}
2020-06-26 04:51:51 +00:00
sendJsonAction(core.App.OAuth, "update", w, r)
}
2020-04-20 03:02:34 +00:00
func apiOAuthHandler(r *http.Request) interface{} {
app := core.App
return app.OAuth
}
2020-01-25 03:28:40 +00:00
func apiCoreHandler(w http.ResponseWriter, r *http.Request) {
var c *core.Core
2020-04-17 03:21:17 +00:00
err := DecodeJSON(r, &c)
2020-01-25 03:28:40 +00:00
if err != nil {
sendErrorJson(err, w, r)
return
}
2020-03-04 10:29:00 +00:00
app := core.App
2020-01-25 03:28:40 +00:00
if c.Name != "" {
app.Name = c.Name
}
if c.Description != app.Description {
app.Description = c.Description
}
if c.Style != app.Style {
app.Style = c.Style
}
if c.Footer.String != app.Footer.String {
app.Footer = c.Footer
}
if c.Domain != app.Domain {
app.Domain = c.Domain
}
2020-09-14 23:00:35 +00:00
if c.Language != app.Language {
app.Language = c.Language
}
utils.Params.Set("LANGUAGE", app.Language)
2020-03-04 10:29:00 +00:00
app.UseCdn = null.NewNullBool(c.UseCdn.Bool)
2020-04-11 22:18:43 +00:00
app.AllowReports = null.NewNullBool(c.AllowReports.Bool)
2020-09-14 23:00:35 +00:00
if err := app.Update(); err != nil {
sendErrorJson(err, w, r)
return
}
if err := configs.Save(); err != nil {
sendErrorJson(err, w, r)
return
}
2020-03-04 10:29:00 +00:00
returnJson(core.App, w, r)
2020-01-25 03:28:40 +00:00
}
2020-02-01 03:53:00 +00:00
type cacheJson struct {
2020-02-04 06:37:32 +00:00
URL string `json:"url"`
Expiration time.Time `json:"expiration"`
Size int `json:"size"`
2020-02-01 03:53:00 +00:00
}
2020-04-17 03:21:17 +00:00
func sendErrorJson(err error, w http.ResponseWriter, r *http.Request) {
errCode := 0
e, ok := err.(errors.Error)
if ok {
errCode = e.Status()
}
log.WithField("url", r.URL.String()).
WithField("method", r.Method).
2020-04-17 03:21:17 +00:00
WithField("code", errCode).
Errorln(fmt.Errorf("sending error response for %s: %s", r.URL.String(), err.Error()))
2020-04-17 03:21:17 +00:00
returnJson(err, w, r)
}
2018-11-17 17:14:14 +00:00
func sendJsonAction(obj interface{}, method string, w http.ResponseWriter, r *http.Request) {
var objName string
var objId int64
switch v := obj.(type) {
2020-03-04 10:29:00 +00:00
case *services.Service:
2018-11-17 17:14:14 +00:00
objName = "service"
2020-03-04 10:29:00 +00:00
objId = v.Id
2020-03-14 03:13:20 +00:00
case *notifications.Notification:
2018-11-17 17:14:14 +00:00
objName = "notifier"
objId = v.Id
2020-03-04 10:29:00 +00:00
case *core.Core:
2018-11-17 17:14:14 +00:00
objName = "core"
2020-03-04 10:29:00 +00:00
case *users.User:
2018-11-17 17:14:14 +00:00
objName = "user"
objId = v.Id
2020-03-04 10:29:00 +00:00
case *groups.Group:
2018-12-31 21:36:58 +00:00
objName = "group"
objId = v.Id
2020-03-04 10:29:00 +00:00
case *checkins.Checkin:
objName = "checkin"
objId = v.Id
2020-03-04 10:29:00 +00:00
case *checkins.CheckinHit:
objName = "checkin_hit"
objId = v.Id
2020-03-04 10:29:00 +00:00
case *messages.Message:
2018-11-17 17:14:14 +00:00
objName = "message"
objId = v.Id
2020-03-04 10:29:00 +00:00
case *incidents.Incident:
2019-06-24 22:21:38 +00:00
objName = "incident"
objId = v.Id
2020-03-04 10:29:00 +00:00
case *incidents.IncidentUpdate:
2019-06-24 22:21:38 +00:00
objName = "incident_update"
objId = v.Id
2018-11-17 17:14:14 +00:00
default:
objName = fmt.Sprintf("%T", v)
}
output := apiResponse{
2018-11-17 17:14:14 +00:00
Object: objName,
Method: method,
Id: objId,
Status: "success",
2018-11-17 17:14:14 +00:00
Output: obj,
}
returnJson(output, w, r)
}
2018-11-08 10:50:06 +00:00
func sendUnauthorizedJson(w http.ResponseWriter, r *http.Request) {
2020-01-13 02:12:50 +00:00
w.Header().Set("Content-Type", "application/json")
2020-04-17 03:21:17 +00:00
returnJson(errors.NotAuthenticated, w, r)
2018-11-08 10:50:06 +00:00
}