statping/handlers/api.go

203 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
//
2018-12-04 04:17:29 +00:00
// https://github.com/hunterlong/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"
2018-12-04 04:17:29 +00:00
"github.com/hunterlong/statping/core"
"github.com/hunterlong/statping/core/notifier"
2020-02-25 07:41:28 +00:00
"github.com/hunterlong/statping/database"
2018-12-04 04:17:29 +00:00
"github.com/hunterlong/statping/types"
"github.com/hunterlong/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{} {
2019-01-09 04:20:43 +00:00
coreClone := *core.CoreApp
2020-02-08 04:21:25 +00:00
var loggedIn bool
_, err := getJwtToken(r)
if err == nil {
loggedIn = true
}
coreClone.LoggedIn = loggedIn
2020-01-20 05:02:15 +00:00
return *coreClone.ToCore()
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
core.CoreApp.ApiKey = utils.NewSHA1Hash(40)
core.CoreApp.ApiSecret = utils.NewSHA1Hash(40)
core.CoreApp, err = core.UpdateCore(core.CoreApp)
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
}
app := core.CoreApp
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
}
app.UseCdn = types.NewNullBool(c.UseCdn.Bool)
core.CoreApp, err = core.UpdateCore(app)
returnJson(core.CoreApp, w, r)
}
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)
}
2018-11-17 17:14:14 +00:00
func sendErrorJson(err error, w http.ResponseWriter, r *http.Request) {
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(),
}
returnJson(output, 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-02-25 07:41:28 +00:00
case types.Servicer:
2018-11-17 17:14:14 +00:00
objName = "service"
2020-02-25 07:41:28 +00:00
objId = v.Model().Id
2018-11-17 17:14:14 +00:00
case *notifier.Notification:
objName = "notifier"
objId = v.Id
case *core.Core, *types.Core:
objName = "core"
case *types.User:
objName = "user"
objId = v.Id
case *core.User:
objName = "user"
objId = v.Id
2018-12-31 21:36:58 +00:00
case *types.Group:
objName = "group"
objId = v.Id
2020-02-25 07:41:28 +00:00
case database.Grouper:
2018-12-31 21:36:58 +00:00
objName = "group"
2020-02-25 07:41:28 +00:00
objId = v.Model().Id
case *core.Checkin:
objName = "checkin"
objId = v.Id
case *core.CheckinHit:
objName = "checkin_hit"
objId = v.Id
2018-11-17 17:14:14 +00:00
case *types.Message:
objName = "message"
objId = v.Id
case *core.Message:
objName = "message"
objId = v.Id
case *types.Checkin:
objName = "checkin"
objId = v.Id
2019-06-24 22:21:38 +00:00
case *core.Incident:
objName = "incident"
objId = v.Id
case *core.IncidentUpdate:
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
}