statping/handlers/api.go

432 lines
11 KiB
Go
Raw Normal View History

2018-08-16 06:22:20 +00:00
// Statup
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
// https://github.com/hunterlong/statup
//
// 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 (
2018-06-25 07:09:31 +00:00
"encoding/json"
2018-11-13 19:28:21 +00:00
"errors"
2018-11-03 00:27:29 +00:00
"fmt"
2018-06-25 07:09:31 +00:00
"github.com/gorilla/mux"
2018-06-30 00:57:05 +00:00
"github.com/hunterlong/statup/core"
2018-11-03 00:27:29 +00:00
"github.com/hunterlong/statup/core/notifier"
2018-07-14 02:37:39 +00:00
"github.com/hunterlong/statup/types"
2018-06-30 00:57:05 +00:00
"github.com/hunterlong/statup/utils"
2018-06-25 07:09:31 +00:00
"net/http"
"os"
"time"
2018-06-15 04:30:10 +00:00
)
2018-10-06 05:56:08 +00:00
type apiResponse struct {
2018-09-12 21:29:47 +00:00
Status string `json:"status"`
2018-11-13 19:28:21 +00:00
Object string `json:"type,omitempty"`
Id int64 `json:"id,omitempty"`
Method string `json:"method,omitempty"`
Error string `json:"error,omitempty"`
}
func apiIndexHandler(w http.ResponseWriter, r *http.Request) {
if !isAPIAuthorized(r) {
2018-11-08 10:50:06 +00:00
sendUnauthorizedJson(w, r)
return
}
2018-09-12 21:29:47 +00:00
w.Header().Set("Content-Type", "application/json")
2018-11-14 03:55:12 +00:00
json.NewEncoder(w).Encode(core.CoreApp)
2018-06-15 04:30:10 +00:00
}
func apiRenewHandler(w http.ResponseWriter, r *http.Request) {
2018-07-17 09:18:20 +00:00
if !isAPIAuthorized(r) {
2018-11-08 10:50:06 +00:00
sendUnauthorizedJson(w, r)
2018-07-17 09:18:20 +00:00
return
}
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
}
http.Redirect(w, r, "/settings", http.StatusSeeOther)
}
func apiCheckinHandler(w http.ResponseWriter, r *http.Request) {
if !isAPIAuthorized(r) {
2018-11-08 10:50:06 +00:00
sendUnauthorizedJson(w, r)
return
}
2018-06-22 06:56:44 +00:00
vars := mux.Vars(r)
2018-10-04 08:18:55 +00:00
checkin := core.SelectCheckin(vars["api"])
2018-07-14 02:37:39 +00:00
//checkin.Receivehit()
2018-06-22 06:56:44 +00:00
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(checkin)
}
func apiServiceDataHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
service := core.SelectService(utils.StringInt(vars["id"]))
if service == nil {
2018-11-13 19:28:21 +00:00
sendErrorJson(errors.New("service data not found"), w, r)
return
}
fields := parseGet(r)
grouping := fields.Get("group")
startField := utils.StringInt(fields.Get("start"))
endField := utils.StringInt(fields.Get("end"))
2018-11-02 23:19:52 +00:00
if startField == 0 || endField == 0 {
startField = 0
endField = 99999999999
}
obj := core.GraphDataRaw(service, time.Unix(startField, 0).UTC(), time.Unix(endField, 0).UTC(), grouping, "latency")
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(obj)
}
func apiServicePingDataHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
service := core.SelectService(utils.StringInt(vars["id"]))
if service == nil {
2018-11-13 19:28:21 +00:00
sendErrorJson(errors.New("service not found"), w, r)
return
}
fields := parseGet(r)
grouping := fields.Get("group")
startField := utils.StringInt(fields.Get("start"))
endField := utils.StringInt(fields.Get("end"))
obj := core.GraphDataRaw(service, time.Unix(startField, 0), time.Unix(endField, 0), grouping, "ping_time")
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(obj)
}
func apiServiceHandler(w http.ResponseWriter, r *http.Request) {
if !isAPIAuthorized(r) {
2018-11-08 10:50:06 +00:00
sendUnauthorizedJson(w, r)
return
}
2018-06-15 04:30:10 +00:00
vars := mux.Vars(r)
servicer := core.SelectServicer(utils.StringInt(vars["id"]))
if servicer == nil {
2018-11-13 19:28:21 +00:00
sendErrorJson(errors.New("service not found"), w, r)
return
}
service := servicer.Select()
2018-09-12 21:29:47 +00:00
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(service)
}
func apiCreateServiceHandler(w http.ResponseWriter, r *http.Request) {
if !isAPIAuthorized(r) {
2018-11-08 10:50:06 +00:00
sendUnauthorizedJson(w, r)
return
}
var service *types.Service
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&service)
if err != nil {
2018-11-13 19:28:21 +00:00
sendErrorJson(err, w, r)
return
}
newService := core.ReturnService(service)
_, err = newService.Create(true)
if err != nil {
2018-11-13 19:28:21 +00:00
sendErrorJson(err, w, r)
return
}
2018-09-12 21:29:47 +00:00
w.Header().Set("Content-Type", "application/json")
2018-06-15 04:30:10 +00:00
json.NewEncoder(w).Encode(service)
}
func apiServiceUpdateHandler(w http.ResponseWriter, r *http.Request) {
if !isAPIAuthorized(r) {
2018-11-08 10:50:06 +00:00
sendUnauthorizedJson(w, r)
return
}
2018-06-15 04:30:10 +00:00
vars := mux.Vars(r)
2018-11-16 02:52:51 +00:00
srv := core.SelectServicer(utils.StringInt(vars["id"]))
if srv.Select() == nil {
2018-11-13 19:28:21 +00:00
sendErrorJson(errors.New("service not found"), w, r)
return
}
2018-11-16 02:52:51 +00:00
var updatedService *core.Service
2018-06-15 04:30:10 +00:00
decoder := json.NewDecoder(r.Body)
decoder.Decode(&updatedService)
2018-11-16 02:52:51 +00:00
updatedService.Id = srv.Select().Id
err := updatedService.Update(true)
if err != nil {
2018-11-13 19:28:21 +00:00
sendErrorJson(err, w, r)
return
}
2018-11-16 02:52:51 +00:00
go updatedService.Check(true)
2018-09-12 21:29:47 +00:00
w.Header().Set("Content-Type", "application/json")
2018-11-16 02:52:51 +00:00
json.NewEncoder(w).Encode(updatedService)
}
func apiServiceDeleteHandler(w http.ResponseWriter, r *http.Request) {
if !isAPIAuthorized(r) {
2018-11-08 10:50:06 +00:00
sendUnauthorizedJson(w, r)
return
}
vars := mux.Vars(r)
service := core.SelectService(utils.StringInt(vars["id"]))
if service == nil {
2018-11-13 19:28:21 +00:00
sendErrorJson(errors.New("service not found"), w, r)
return
}
err := service.Delete()
if err != nil {
2018-11-13 19:28:21 +00:00
sendErrorJson(err, w, r)
return
}
2018-10-06 05:56:08 +00:00
output := apiResponse{
Object: "service",
Method: "delete",
Id: service.Id,
Status: "success",
}
2018-09-12 21:29:47 +00:00
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(output)
2018-06-15 04:30:10 +00:00
}
func apiAllServicesHandler(w http.ResponseWriter, r *http.Request) {
if !isAPIAuthorized(r) {
2018-11-08 10:50:06 +00:00
sendUnauthorizedJson(w, r)
return
}
2018-11-09 05:29:01 +00:00
services := core.Services()
2018-09-12 21:29:47 +00:00
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(services)
2018-06-15 04:30:10 +00:00
}
2018-11-03 00:27:29 +00:00
func apiNotifierGetHandler(w http.ResponseWriter, r *http.Request) {
if !isAPIAuthorized(r) {
2018-11-08 10:50:06 +00:00
sendUnauthorizedJson(w, r)
2018-11-03 00:27:29 +00:00
return
}
vars := mux.Vars(r)
2018-11-06 09:03:21 +00:00
_, notifierObj, err := notifier.SelectNotifier(vars["notifier"])
2018-11-03 00:27:29 +00:00
if err != nil {
2018-11-13 19:28:21 +00:00
sendErrorJson(err, w, r)
2018-11-03 00:27:29 +00:00
return
}
2018-11-05 22:04:06 +00:00
w.Header().Set("Content-Type", "application/json")
2018-11-06 12:12:17 +00:00
json.NewEncoder(w).Encode(notifierObj)
2018-11-06 09:03:21 +00:00
}
func apiNotifierUpdateHandler(w http.ResponseWriter, r *http.Request) {
if !isAPIAuthorized(r) {
2018-11-08 10:50:06 +00:00
sendUnauthorizedJson(w, r)
2018-11-06 09:03:21 +00:00
return
}
vars := mux.Vars(r)
var notification *notifier.Notification
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&notification)
if err != nil {
sendErrorJson(err, w, r)
return
}
2018-11-06 12:12:17 +00:00
notifer, not, err := notifier.SelectNotifier(vars["notifier"])
2018-11-06 09:03:21 +00:00
if err != nil {
2018-11-13 19:28:21 +00:00
sendErrorJson(err, w, r)
2018-11-06 09:03:21 +00:00
return
}
2018-11-06 12:12:17 +00:00
notifer.Var1 = notification.Var1
notifer.Var2 = notification.Var2
notifer.Host = notification.Host
notifer.Port = notification.Port
notifer.Password = notification.Password
notifer.Username = notification.Username
notifer.ApiKey = notification.ApiKey
notifer.ApiSecret = notification.ApiSecret
notifer.Enabled = types.NewNullBool(notification.Enabled.Bool)
2018-11-06 09:03:21 +00:00
2018-11-06 12:12:17 +00:00
_, err = notifier.Update(not, notifer)
2018-11-06 09:03:21 +00:00
if err != nil {
2018-11-13 19:28:21 +00:00
sendErrorJson(err, w, r)
return
2018-11-06 09:03:21 +00:00
}
2018-11-06 12:12:17 +00:00
notifier.OnSave(notifer.Method)
2018-11-06 09:03:21 +00:00
w.Header().Set("Content-Type", "application/json")
2018-11-06 12:12:17 +00:00
json.NewEncoder(w).Encode(notifer)
2018-11-03 00:27:29 +00:00
}
func apiAllMessagesHandler(w http.ResponseWriter, r *http.Request) {
if !isAPIAuthorized(r) {
2018-11-08 10:50:06 +00:00
sendUnauthorizedJson(w, r)
return
}
messages, err := core.SelectMessages()
if err != nil {
2018-11-13 19:28:21 +00:00
sendErrorJson(err, w, r)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(messages)
}
func apiMessageCreateHandler(w http.ResponseWriter, r *http.Request) {
if !isAPIAuthorized(r) {
sendUnauthorizedJson(w, r)
return
}
var message *types.Message
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&message)
if err != nil {
sendErrorJson(err, w, r)
return
}
msg := core.ReturnMessage(message)
_, err = msg.Create()
if err != nil {
sendErrorJson(err, w, r)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(msg)
}
func apiMessageGetHandler(w http.ResponseWriter, r *http.Request) {
if !isAPIAuthorized(r) {
2018-11-08 10:50:06 +00:00
sendUnauthorizedJson(w, r)
return
}
vars := mux.Vars(r)
message, err := core.SelectMessage(utils.StringInt(vars["id"]))
if err != nil {
2018-11-13 19:28:21 +00:00
sendErrorJson(err, w, r)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(message)
}
func apiMessageDeleteHandler(w http.ResponseWriter, r *http.Request) {
if !isAPIAuthorized(r) {
2018-11-08 10:50:06 +00:00
sendUnauthorizedJson(w, r)
return
}
vars := mux.Vars(r)
message, err := core.SelectMessage(utils.StringInt(vars["id"]))
if err != nil {
2018-11-13 19:28:21 +00:00
sendErrorJson(err, w, r)
return
}
err = message.Delete()
if err != nil {
2018-11-13 19:28:21 +00:00
sendErrorJson(err, w, r)
return
}
output := apiResponse{
Object: "message",
Method: "delete",
Id: message.Id,
Status: "success",
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(output)
}
func apiMessageUpdateHandler(w http.ResponseWriter, r *http.Request) {
if !isAPIAuthorized(r) {
2018-11-08 10:50:06 +00:00
sendUnauthorizedJson(w, r)
return
}
vars := mux.Vars(r)
message, err := core.SelectMessage(utils.StringInt(vars["id"]))
if err != nil {
2018-11-13 19:28:21 +00:00
sendErrorJson(fmt.Errorf("message #%v was not found", vars["id"]), w, r)
return
}
var messageBody *types.Message
decoder := json.NewDecoder(r.Body)
err = decoder.Decode(&messageBody)
if err != nil {
2018-11-13 19:28:21 +00:00
sendErrorJson(err, w, r)
return
}
messageBody.Id = message.Id
message = core.ReturnMessage(messageBody)
_, err = message.Update()
if err != nil {
2018-11-13 19:28:21 +00:00
sendErrorJson(err, w, r)
return
}
output := apiResponse{
Object: "message",
Method: "update",
Id: message.Id,
Status: "success",
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(output)
}
2018-11-08 06:23:24 +00:00
func apiNotifiersHandler(w http.ResponseWriter, r *http.Request) {
2018-11-08 10:50:06 +00:00
if !isAPIAuthorized(r) {
sendUnauthorizedJson(w, r)
2018-11-08 06:23:24 +00:00
return
}
var notifiers []*notifier.Notification
for _, n := range core.CoreApp.Notifications {
notif := n.(notifier.Notifier)
notifiers = append(notifiers, notif.Select())
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(notifiers)
}
2018-11-13 19:28:21 +00:00
func sendErrorJson(err error, w http.ResponseWriter, r *http.Request) {
output := apiResponse{
Status: "error",
Error: err.Error(),
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(output)
}
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
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusUnauthorized)
2018-11-13 19:28:21 +00:00
json.NewEncoder(w).Encode(output)
2018-11-08 10:50:06 +00:00
}
func isAPIAuthorized(r *http.Request) bool {
if os.Getenv("GO_ENV") == "test" {
return true
}
if IsAuthenticated(r) {
return true
}
if isAuthorized(r) {
return true
}
return false
}