statping/handlers/api.go

92 lines
2.3 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-06-25 07:09:31 +00:00
"encoding/json"
"github.com/gorilla/mux"
2018-06-30 00:57:05 +00:00
"github.com/hunterlong/statup/core"
"github.com/hunterlong/statup/utils"
2018-06-25 07:09:31 +00:00
"net/http"
2018-06-15 04:30:10 +00:00
)
func ApiIndexHandler(w http.ResponseWriter, r *http.Request) {
if !isAPIAuthorized(r) {
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
2018-06-30 00:57:05 +00:00
json.NewEncoder(w).Encode(core.CoreApp)
2018-06-15 04:30:10 +00:00
}
2018-06-22 06:56:44 +00:00
func ApiCheckinHandler(w http.ResponseWriter, r *http.Request) {
if !isAPIAuthorized(r) {
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
2018-06-22 06:56:44 +00:00
vars := mux.Vars(r)
2018-06-30 00:57:05 +00:00
checkin := core.FindCheckin(vars["api"])
2018-06-22 06:56:44 +00:00
checkin.Receivehit()
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(checkin)
}
2018-06-15 04:30:10 +00:00
func ApiServiceHandler(w http.ResponseWriter, r *http.Request) {
if !isAPIAuthorized(r) {
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
2018-06-15 04:30:10 +00:00
vars := mux.Vars(r)
2018-06-30 00:57:05 +00:00
service := core.SelectService(utils.StringInt(vars["id"]))
2018-06-15 04:30:10 +00:00
json.NewEncoder(w).Encode(service)
}
func ApiServiceUpdateHandler(w http.ResponseWriter, r *http.Request) {
if !isAPIAuthorized(r) {
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
2018-06-15 04:30:10 +00:00
vars := mux.Vars(r)
2018-06-30 00:57:05 +00:00
service := core.SelectService(utils.StringInt(vars["id"]))
var s core.Service
2018-06-15 04:30:10 +00:00
decoder := json.NewDecoder(r.Body)
decoder.Decode(&s)
service.Update(&s)
json.NewEncoder(w).Encode(s)
2018-06-15 04:30:10 +00:00
}
func ApiAllServicesHandler(w http.ResponseWriter, r *http.Request) {
if !isAPIAuthorized(r) {
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
2018-06-30 00:57:05 +00:00
services, _ := core.SelectAllServices()
2018-06-15 04:30:10 +00:00
json.NewEncoder(w).Encode(services)
}
func ApiUserHandler(w http.ResponseWriter, r *http.Request) {
if !isAPIAuthorized(r) {
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
2018-06-15 04:30:10 +00:00
vars := mux.Vars(r)
2018-06-30 00:57:05 +00:00
user, _ := core.SelectUser(utils.StringInt(vars["id"]))
2018-06-15 04:30:10 +00:00
json.NewEncoder(w).Encode(user)
}
func ApiAllUsersHandler(w http.ResponseWriter, r *http.Request) {
if !isAPIAuthorized(r) {
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
2018-06-30 00:57:05 +00:00
users, _ := core.SelectAllUsers()
2018-06-15 04:30:10 +00:00
json.NewEncoder(w).Encode(users)
}
func isAPIAuthorized(r *http.Request) bool {
if IsAuthenticated(r) {
return true
}
if isAuthorized(r) {
return true
}
return false
}