statping/handlers/routes.go

171 lines
8.9 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
import (
2018-07-04 09:00:16 +00:00
"fmt"
2018-06-30 00:57:05 +00:00
"github.com/gorilla/mux"
2018-12-04 04:17:29 +00:00
"github.com/hunterlong/statping/core"
"github.com/hunterlong/statping/source"
"github.com/hunterlong/statping/utils"
2018-06-30 00:57:05 +00:00
"net/http"
2020-01-03 06:10:25 +00:00
"os"
2018-06-30 00:57:05 +00:00
)
2018-07-22 22:17:38 +00:00
var (
2018-08-16 20:55:30 +00:00
router *mux.Router
log = utils.Log.WithField("type", "handlers")
2018-07-22 22:17:38 +00:00
)
2019-02-06 18:51:30 +00:00
// Router returns all of the routes used in Statping.
// Server will use static assets if the 'assets' directory is found in the root directory.
2018-06-30 00:57:05 +00:00
func Router() *mux.Router {
2018-08-16 20:55:30 +00:00
dir := utils.Directory
2018-11-05 22:04:06 +00:00
CacheStorage = NewStorage()
r := mux.NewRouter().StrictSlash(true)
2020-01-13 07:11:53 +00:00
2020-01-03 06:10:25 +00:00
if os.Getenv("AUTH_USERNAME") != "" && os.Getenv("AUTH_PASSWORD") != "" {
authUser = os.Getenv("AUTH_USERNAME")
authPass = os.Getenv("AUTH_PASSWORD")
r.Use(basicAuthHandler)
}
2020-01-13 08:20:23 +00:00
if os.Getenv("BASE_PATH") != "" {
basePath = "/" + os.Getenv("BASE_PATH") + "/"
r = r.PathPrefix("/" + os.Getenv("BASE_PATH")).Subrouter()
r.Handle("", http.HandlerFunc(indexHandler))
} else {
r.Handle("/", http.HandlerFunc(indexHandler))
2020-01-13 07:11:53 +00:00
}
r.Use(sendLog)
2018-08-16 20:55:30 +00:00
if source.UsingAssets(dir) {
indexHandler := http.FileServer(http.Dir(dir + "/assets/"))
2020-01-26 21:01:43 +00:00
r.PathPrefix("/css/").Handler(Gzip(http.StripPrefix(basePath+"css/", http.FileServer(http.Dir(dir+"/assets/css")))))
2020-01-13 08:20:23 +00:00
r.PathPrefix("/font/").Handler(http.StripPrefix(basePath+"font/", http.FileServer(http.Dir(dir+"/assets/font"))))
2020-01-26 21:01:43 +00:00
r.PathPrefix("/js/").Handler(Gzip(http.StripPrefix(basePath+"js/", http.FileServer(http.Dir(dir+"/assets/js")))))
2020-01-13 08:20:23 +00:00
r.PathPrefix("/robots.txt").Handler(http.StripPrefix(basePath, indexHandler))
r.PathPrefix("/favicon.ico").Handler(http.StripPrefix(basePath, indexHandler))
r.PathPrefix("/banner.png").Handler(http.StripPrefix(basePath, indexHandler))
2018-08-16 20:55:30 +00:00
} else {
2020-01-26 21:01:43 +00:00
//r.PathPrefix("/").Handler(http.StripPrefix(basePath+"/", http.FileServer(source.TmplBox.HTTPBox())))
r.PathPrefix("/css/").Handler(Gzip(http.FileServer(source.TmplBox.HTTPBox())))
r.PathPrefix("/font/").Handler(http.FileServer(source.TmplBox.HTTPBox()))
r.PathPrefix("/js/").Handler(Gzip(http.FileServer(source.TmplBox.HTTPBox())))
2020-01-13 08:20:23 +00:00
r.PathPrefix("/robots.txt").Handler(http.StripPrefix(basePath, http.FileServer(source.TmplBox.HTTPBox())))
r.PathPrefix("/favicon.ico").Handler(http.StripPrefix(basePath, http.FileServer(source.TmplBox.HTTPBox())))
r.PathPrefix("/banner.png").Handler(http.StripPrefix(basePath, http.FileServer(source.TmplBox.HTTPBox())))
2018-08-16 20:55:30 +00:00
}
2019-03-22 05:34:52 +00:00
2018-11-05 22:04:06 +00:00
// API Routes
2020-01-20 05:02:15 +00:00
r.Handle("/api", scoped(apiIndexHandler))
2020-01-16 09:29:49 +00:00
r.Handle("/api/login", http.HandlerFunc(apiLoginHandler)).Methods("POST")
2020-01-26 21:01:43 +00:00
r.Handle("/api/setup", http.HandlerFunc(processSetupHandler)).Methods("POST")
2020-01-16 09:29:49 +00:00
r.Handle("/api/logout", http.HandlerFunc(logoutHandler))
2019-03-05 20:13:25 +00:00
r.Handle("/api/renew", authenticated(apiRenewHandler, false))
2020-02-01 03:53:00 +00:00
r.Handle("/api/cache", authenticated(apiCacheHandler, false)).Methods("GET")
2019-03-05 20:13:25 +00:00
r.Handle("/api/clear_cache", authenticated(apiClearCacheHandler, false))
2020-01-25 03:28:40 +00:00
r.Handle("/api/core", authenticated(apiCoreHandler, false)).Methods("POST")
2020-02-01 03:53:00 +00:00
r.Handle("/api/logs", authenticated(logsHandler, false)).Methods("GET")
r.Handle("/api/logs/last", authenticated(logsLineHandler, false)).Methods("GET")
2018-11-05 22:04:06 +00:00
2020-02-01 03:53:00 +00:00
// API INTEGRATIONS Routes
2020-01-03 06:10:25 +00:00
r.Handle("/api/integrations", authenticated(apiAllIntegrationsHandler, false)).Methods("GET")
2020-02-01 03:53:00 +00:00
r.Handle("/api/integrations/{name}", authenticated(apiIntegrationViewHandler, false)).Methods("GET")
2020-01-03 06:10:25 +00:00
r.Handle("/api/integrations/{name}", authenticated(apiIntegrationHandler, false)).Methods("POST")
2020-01-13 02:12:50 +00:00
// API GROUPS Routes
2020-01-20 05:02:15 +00:00
r.Handle("/api/groups", scoped(apiAllGroupHandler)).Methods("GET")
2020-01-13 02:12:50 +00:00
r.Handle("/api/groups", authenticated(apiCreateGroupHandler, false)).Methods("POST")
r.Handle("/api/groups/{id}", readOnly(apiGroupHandler, false)).Methods("GET")
r.Handle("/api/groups/{id}", authenticated(apiGroupUpdateHandler, false)).Methods("POST")
r.Handle("/api/groups/{id}", authenticated(apiGroupDeleteHandler, false)).Methods("DELETE")
r.Handle("/api/reorder/groups", authenticated(apiGroupReorderHandler, false)).Methods("POST")
2018-10-04 08:18:55 +00:00
// API SERVICE Routes
2020-01-20 05:02:15 +00:00
r.Handle("/api/services", scoped(apiAllServicesHandler)).Methods("GET")
2019-03-05 20:13:25 +00:00
r.Handle("/api/services", authenticated(apiCreateServiceHandler, false)).Methods("POST")
2020-01-30 04:34:47 +00:00
r.Handle("/api/services_test", authenticated(apiTestServiceHandler, false)).Methods("POST")
2020-01-20 05:02:15 +00:00
r.Handle("/api/services/{id}", scoped(apiServiceHandler)).Methods("GET")
r.Handle("/api/reorder/services", authenticated(reorderServiceHandler, false)).Methods("POST")
r.Handle("/api/services/{id}/running", authenticated(apiServiceRunningHandler, false)).Methods("POST")
r.Handle("/api/services/{id}/data", cached("30s", "application/json", apiServiceDataHandler)).Methods("GET")
r.Handle("/api/services/{id}/ping", cached("30s", "application/json", apiServicePingDataHandler)).Methods("GET")
r.Handle("/api/services/{id}/heatmap", cached("30s", "application/json", apiServiceHeatmapHandler)).Methods("GET")
2019-03-05 20:13:25 +00:00
r.Handle("/api/services/{id}", authenticated(apiServiceUpdateHandler, false)).Methods("POST")
r.Handle("/api/services/{id}", authenticated(apiServiceDeleteHandler, false)).Methods("DELETE")
2020-01-20 05:02:15 +00:00
r.Handle("/api/services/{id}/failures", scoped(apiServiceFailuresHandler)).Methods("GET")
2019-03-05 20:13:25 +00:00
r.Handle("/api/services/{id}/failures", authenticated(servicesDeleteFailuresHandler, false)).Methods("DELETE")
2020-01-20 05:02:15 +00:00
r.Handle("/api/services/{id}/hits", scoped(apiServiceHitsHandler)).Methods("GET")
2019-06-24 22:21:38 +00:00
// API INCIDENTS Routes
r.Handle("/api/incidents", readOnly(apiAllIncidentsHandler, false)).Methods("GET")
2020-02-01 03:53:00 +00:00
r.Handle("/api/incidents", authenticated(apiCreateIncidentHandler, false)).Methods("POST")
r.Handle("/api/incidents/:id", authenticated(apiIncidentUpdateHandler, false)).Methods("POST")
r.Handle("/api/incidents/:id", authenticated(apiDeleteIncidentHandler, false)).Methods("DELETE")
2019-06-24 22:21:38 +00:00
2018-10-04 08:18:55 +00:00
// API USER Routes
2019-03-05 20:13:25 +00:00
r.Handle("/api/users", authenticated(apiAllUsersHandler, false)).Methods("GET")
r.Handle("/api/users", authenticated(apiCreateUsersHandler, false)).Methods("POST")
r.Handle("/api/users/{id}", authenticated(apiUserHandler, false)).Methods("GET")
r.Handle("/api/users/{id}", authenticated(apiUserUpdateHandler, false)).Methods("POST")
r.Handle("/api/users/{id}", authenticated(apiUserDeleteHandler, false)).Methods("DELETE")
// API NOTIFIER Routes
2019-03-05 20:13:25 +00:00
r.Handle("/api/notifiers", authenticated(apiNotifiersHandler, false)).Methods("GET")
r.Handle("/api/notifier/{notifier}", authenticated(apiNotifierGetHandler, false)).Methods("GET")
r.Handle("/api/notifier/{notifier}", authenticated(apiNotifierUpdateHandler, false)).Methods("POST")
r.Handle("/api/notifier/{method}/test", authenticated(testNotificationHandler, false)).Methods("POST")
2018-11-02 23:19:52 +00:00
// API MESSAGES Routes
2020-01-20 05:02:15 +00:00
r.Handle("/api/messages", scoped(apiAllMessagesHandler)).Methods("GET")
2019-03-05 20:13:25 +00:00
r.Handle("/api/messages", authenticated(apiMessageCreateHandler, false)).Methods("POST")
r.Handle("/api/messages/{id}", readOnly(apiMessageGetHandler, false)).Methods("GET")
r.Handle("/api/messages/{id}", authenticated(apiMessageUpdateHandler, false)).Methods("POST")
r.Handle("/api/messages/{id}", authenticated(apiMessageDeleteHandler, false)).Methods("DELETE")
// API CHECKIN Routes
2019-03-05 20:13:25 +00:00
r.Handle("/api/checkins", authenticated(apiAllCheckinsHandler, false)).Methods("GET")
r.Handle("/api/checkin/{api}", authenticated(apiCheckinHandler, false)).Methods("GET")
r.Handle("/api/checkin", authenticated(checkinCreateHandler, false)).Methods("POST")
r.Handle("/api/checkin/{api}", authenticated(checkinDeleteHandler, false)).Methods("DELETE")
r.Handle("/checkin/{api}", http.HandlerFunc(checkinHitHandler))
2020-01-26 21:01:43 +00:00
//r.PathPrefix("/").Handler(http.HandlerFunc(indexHandler))
// Static Files Routes
2018-11-16 02:52:51 +00:00
r.PathPrefix("/files/postman.json").Handler(http.StripPrefix("/files/", http.FileServer(source.TmplBox.HTTPBox())))
r.PathPrefix("/files/swagger.json").Handler(http.StripPrefix("/files/", http.FileServer(source.TmplBox.HTTPBox())))
r.PathPrefix("/files/grafana.json").Handler(http.StripPrefix("/files/", http.FileServer(source.TmplBox.HTTPBox())))
// API Generic Routes
2019-03-05 20:13:25 +00:00
r.Handle("/metrics", readOnly(prometheusHandler, false))
r.Handle("/health", http.HandlerFunc(healthCheckHandler))
r.Handle("/.well-known/", http.StripPrefix("/.well-known/", http.FileServer(http.Dir(dir+"/.well-known"))))
r.NotFoundHandler = http.HandlerFunc(error404Handler)
2018-08-16 20:55:30 +00:00
return r
}
2018-10-06 05:56:08 +00:00
func resetRouter() {
2018-08-16 20:55:30 +00:00
router = Router()
httpServer.Handler = router
}
func resetCookies() {
2020-01-16 09:29:49 +00:00
jwtKey = fmt.Sprintf("%v_%v", core.CoreApp.ApiSecret, utils.Now().Nanosecond())
}