statping/handlers/api.go

195 lines
4.8 KiB
Go
Raw Normal View History

2018-12-04 05:57:11 +00:00
// Statping
2018-08-16 06:22:20 +00:00
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
2020-03-09 18:17:55 +00:00
// https://github.com/statping/statping
2018-08-16 06:22:20 +00:00
//
// The licenses for most software and other practical works are designed
// to take away your freedom to share and change the works. By contrast,
// the GNU General Public License is intended to guarantee your freedom to
// share and change all versions of a program--to make sure it remains free
// software for all its users.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
2018-06-30 00:57:05 +00:00
package handlers
2018-06-15 04:30:10 +00:00
import (
2020-01-25 03:28:40 +00:00
"encoding/json"
2018-11-13 19:28:21 +00:00
"errors"
2018-11-25 03:56:09 +00:00
"fmt"
2020-03-09 18:17:55 +00:00
"github.com/statping/statping/types/checkins"
"github.com/statping/statping/types/core"
"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"`
Error string `json:"error,omitempty"`
Id int64 `json:"id,omitempty"`
Output interface{} `json:"output,omitempty"`
}
2020-01-20 05:02:15 +00:00
func apiIndexHandler(r *http.Request) interface{} {
coreClone := *core.App
2020-02-08 04:21:25 +00:00
_, err := getJwtToken(r)
if err == nil {
coreClone.LoggedIn = true
coreClone.IsAdmin = IsAdmin(r)
2020-02-08 04:21:25 +00:00
}
2020-03-04 10:29:00 +00:00
return coreClone
2018-06-15 04:30:10 +00:00
}
func apiRenewHandler(w http.ResponseWriter, r *http.Request) {
2018-07-17 09:18:20 +00:00
var err error
2020-03-16 06:51:15 +00:00
core.App.ApiKey = utils.NewSHA256Hash()
core.App.ApiSecret = utils.NewSHA256Hash()
2020-03-04 10:29:00 +00:00
err = core.App.Update()
2018-07-17 09:18:20 +00:00
if 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
}
2020-01-25 03:28:40 +00:00
func apiCoreHandler(w http.ResponseWriter, r *http.Request) {
var c *core.Core
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&c)
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
}
if c.Timezone != app.Timezone {
app.Timezone = c.Timezone
}
2020-03-04 10:29:00 +00:00
app.UseCdn = null.NewNullBool(c.UseCdn.Bool)
err = app.Update()
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
}
func apiCacheHandler(w http.ResponseWriter, r *http.Request) {
var cacheList []cacheJson
2020-02-19 04:07:22 +00:00
for k, v := range CacheStorage.List() {
2020-02-01 03:53:00 +00:00
cacheList = append(cacheList, cacheJson{
URL: k,
2020-02-04 06:37:32 +00:00
Expiration: time.Unix(0, v.Expiration).UTC(),
2020-02-01 03:53:00 +00:00
Size: len(v.Content),
})
}
returnJson(cacheList, w, r)
}
func apiClearCacheHandler(w http.ResponseWriter, r *http.Request) {
2020-02-19 04:07:22 +00:00
CacheStorage.StopRoutine()
CacheStorage = NewStorage()
2020-02-01 03:53:00 +00:00
output := apiResponse{
Status: "success",
}
returnJson(output, w, r)
}
2020-03-10 05:24:35 +00:00
func sendErrorJson(err error, w http.ResponseWriter, r *http.Request, statusCode ...int) {
log.Warnln(fmt.Errorf("sending error response for %v: %v", r.URL.String(), err.Error()))
output := apiResponse{
2018-11-17 17:14:14 +00:00
Status: "error",
Error: err.Error(),
}
2020-03-10 05:24:35 +00:00
returnJson(output, w, r, statusCode...)
}
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 = "missing"
}
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) {
2018-11-13 19:28:21 +00:00
output := apiResponse{
Status: "error",
Error: errors.New("not authorized").Error(),
2018-11-08 10:50:06 +00:00
}
2020-01-13 02:12:50 +00:00
w.Header().Set("Content-Type", "application/json")
2018-11-08 10:50:06 +00:00
w.WriteHeader(http.StatusUnauthorized)
returnJson(output, w, r)
2018-11-08 10:50:06 +00:00
}