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"
|
|
|
|
"github.com/gorilla/sessions"
|
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"
|
2018-07-04 09:00:16 +00:00
|
|
|
"time"
|
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
|
2018-07-22 22:17:38 +00:00
|
|
|
)
|
|
|
|
|
2018-12-04 05:57:11 +00:00
|
|
|
// Router returns all of the routes used in Statping
|
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()
|
2018-08-16 20:55:30 +00:00
|
|
|
r := mux.NewRouter()
|
2019-02-01 04:48:18 +00:00
|
|
|
r.Handle("/", cached("60s", "text/html", http.HandlerFunc(indexHandler)))
|
2018-08-16 20:55:30 +00:00
|
|
|
if source.UsingAssets(dir) {
|
|
|
|
indexHandler := http.FileServer(http.Dir(dir + "/assets/"))
|
2018-11-06 07:15:55 +00:00
|
|
|
r.PathPrefix("/css/").Handler(http.StripPrefix("/css/", http.FileServer(http.Dir(dir+"/assets/css"))))
|
|
|
|
r.PathPrefix("/font/").Handler(http.StripPrefix("/font/", http.FileServer(http.Dir(dir+"/assets/font"))))
|
2019-01-15 01:28:00 +00:00
|
|
|
r.PathPrefix("/js/").Handler(http.StripPrefix("/js/", http.FileServer(http.Dir(dir+"/assets/js"))))
|
2018-08-16 20:55:30 +00:00
|
|
|
r.PathPrefix("/robots.txt").Handler(indexHandler)
|
|
|
|
r.PathPrefix("/favicon.ico").Handler(indexHandler)
|
2018-12-04 04:17:29 +00:00
|
|
|
r.PathPrefix("/banner.png").Handler(indexHandler)
|
2018-08-16 20:55:30 +00:00
|
|
|
} else {
|
|
|
|
r.PathPrefix("/css/").Handler(http.StripPrefix("/css/", http.FileServer(source.CssBox.HTTPBox())))
|
2018-11-06 07:15:55 +00:00
|
|
|
r.PathPrefix("/font/").Handler(http.StripPrefix("/font/", http.FileServer(source.FontBox.HTTPBox())))
|
2019-01-15 01:28:00 +00:00
|
|
|
r.PathPrefix("/js/").Handler(http.StripPrefix("/js/", http.FileServer(source.JsBox.HTTPBox())))
|
2018-08-16 20:55:30 +00:00
|
|
|
r.PathPrefix("/robots.txt").Handler(http.FileServer(source.TmplBox.HTTPBox()))
|
|
|
|
r.PathPrefix("/favicon.ico").Handler(http.FileServer(source.TmplBox.HTTPBox()))
|
2018-12-04 04:17:29 +00:00
|
|
|
r.PathPrefix("/banner.png").Handler(http.FileServer(source.TmplBox.HTTPBox()))
|
2018-08-16 20:55:30 +00:00
|
|
|
}
|
2018-09-17 22:13:42 +00:00
|
|
|
r.Handle("/charts.js", http.HandlerFunc(renderServiceChartsHandler))
|
2018-09-16 07:48:34 +00:00
|
|
|
r.Handle("/setup", http.HandlerFunc(setupHandler)).Methods("GET")
|
|
|
|
r.Handle("/setup", http.HandlerFunc(processSetupHandler)).Methods("POST")
|
2019-02-01 21:30:01 +00:00
|
|
|
r.Handle("/dashboard", http.HandlerFunc(dashboardHandler)).Methods("GET")
|
2018-09-16 07:48:34 +00:00
|
|
|
r.Handle("/dashboard", http.HandlerFunc(loginHandler)).Methods("POST")
|
|
|
|
r.Handle("/logout", http.HandlerFunc(logoutHandler))
|
2018-10-04 08:18:55 +00:00
|
|
|
r.Handle("/plugins/download/{name}", http.HandlerFunc(pluginsDownloadHandler))
|
|
|
|
r.Handle("/plugins/{name}/save", http.HandlerFunc(pluginSavedHandler)).Methods("POST")
|
|
|
|
r.Handle("/help", http.HandlerFunc(helpHandler))
|
|
|
|
r.Handle("/logs", http.HandlerFunc(logsHandler))
|
|
|
|
r.Handle("/logs/line", http.HandlerFunc(logsLineHandler))
|
|
|
|
|
|
|
|
// USER Routes
|
2018-09-16 07:48:34 +00:00
|
|
|
r.Handle("/users", http.HandlerFunc(usersHandler)).Methods("GET")
|
|
|
|
r.Handle("/user/{id}", http.HandlerFunc(usersEditHandler)).Methods("GET")
|
2018-10-04 08:18:55 +00:00
|
|
|
|
2018-11-07 05:06:44 +00:00
|
|
|
// MESSAGES Routes
|
|
|
|
r.Handle("/messages", http.HandlerFunc(messagesHandler)).Methods("GET")
|
2018-11-14 03:05:36 +00:00
|
|
|
r.Handle("/message/{id}", http.HandlerFunc(viewMessageHandler)).Methods("GET")
|
2018-11-07 05:06:44 +00:00
|
|
|
|
2018-10-04 08:18:55 +00:00
|
|
|
// SETTINGS Routes
|
2018-09-16 07:48:34 +00:00
|
|
|
r.Handle("/settings", http.HandlerFunc(settingsHandler)).Methods("GET")
|
|
|
|
r.Handle("/settings", http.HandlerFunc(saveSettingsHandler)).Methods("POST")
|
|
|
|
r.Handle("/settings/css", http.HandlerFunc(saveSASSHandler)).Methods("POST")
|
|
|
|
r.Handle("/settings/build", http.HandlerFunc(saveAssetsHandler)).Methods("GET")
|
|
|
|
r.Handle("/settings/delete_assets", http.HandlerFunc(deleteAssetsHandler)).Methods("GET")
|
2018-09-25 04:19:59 +00:00
|
|
|
r.Handle("/settings/export", http.HandlerFunc(exportHandler)).Methods("GET")
|
2018-08-21 03:11:40 +00:00
|
|
|
|
2018-10-04 08:18:55 +00:00
|
|
|
// SERVICE Routes
|
|
|
|
r.Handle("/services", http.HandlerFunc(servicesHandler)).Methods("GET")
|
|
|
|
r.Handle("/service/{id}", http.HandlerFunc(servicesViewHandler)).Methods("GET")
|
2019-01-10 16:47:01 +00:00
|
|
|
r.Handle("/service/{id}/edit", http.HandlerFunc(servicesViewHandler)).Methods("GET")
|
2018-10-04 08:18:55 +00:00
|
|
|
r.Handle("/service/{id}/delete_failures", http.HandlerFunc(servicesDeleteFailuresHandler)).Methods("GET")
|
|
|
|
|
2018-12-31 21:36:58 +00:00
|
|
|
// API GROUPS Routes
|
|
|
|
r.Handle("/api/groups", http.HandlerFunc(apiAllGroupHandler)).Methods("GET")
|
|
|
|
r.Handle("/api/groups", http.HandlerFunc(apiCreateGroupHandler)).Methods("POST")
|
|
|
|
r.Handle("/api/groups/{id}", http.HandlerFunc(apiGroupHandler)).Methods("GET")
|
|
|
|
r.Handle("/api/groups/{id}", http.HandlerFunc(apiGroupDeleteHandler)).Methods("DELETE")
|
|
|
|
|
2018-11-05 22:04:06 +00:00
|
|
|
// API Routes
|
|
|
|
r.Handle("/api", http.HandlerFunc(apiIndexHandler))
|
|
|
|
r.Handle("/api/renew", http.HandlerFunc(apiRenewHandler))
|
2019-02-01 04:48:18 +00:00
|
|
|
r.Handle("/api/clear_cache", http.HandlerFunc(apiClearCacheHandler))
|
2018-11-05 22:04:06 +00:00
|
|
|
|
2018-10-04 08:18:55 +00:00
|
|
|
// API SERVICE Routes
|
2018-09-16 07:48:34 +00:00
|
|
|
r.Handle("/api/services", http.HandlerFunc(apiAllServicesHandler)).Methods("GET")
|
|
|
|
r.Handle("/api/services", http.HandlerFunc(apiCreateServiceHandler)).Methods("POST")
|
|
|
|
r.Handle("/api/services/{id}", http.HandlerFunc(apiServiceHandler)).Methods("GET")
|
2018-12-14 09:19:55 +00:00
|
|
|
r.Handle("/api/reorder", http.HandlerFunc(reorderServiceHandler)).Methods("POST")
|
2018-12-05 20:31:20 +00:00
|
|
|
r.Handle("/api/services/{id}/data", cached("30s", "application/json", http.HandlerFunc(apiServiceDataHandler))).Methods("GET")
|
2019-02-01 04:48:18 +00:00
|
|
|
r.Handle("/api/services/{id}/ping", cached("30s", "application/json", http.HandlerFunc(apiServicePingDataHandler))).Methods("GET")
|
|
|
|
r.Handle("/api/services/{id}/heatmap", cached("30s", "application/json", http.HandlerFunc(apiServiceHeatmapHandler))).Methods("GET")
|
2018-09-16 07:48:34 +00:00
|
|
|
r.Handle("/api/services/{id}", http.HandlerFunc(apiServiceUpdateHandler)).Methods("POST")
|
|
|
|
r.Handle("/api/services/{id}", http.HandlerFunc(apiServiceDeleteHandler)).Methods("DELETE")
|
2019-01-10 16:47:01 +00:00
|
|
|
r.Handle("/api/services/{id}/failures", http.HandlerFunc(apiServiceFailuresHandler)).Methods("GET")
|
|
|
|
r.Handle("/api/services/{id}/hits", http.HandlerFunc(apiServiceHitsHandler)).Methods("GET")
|
2018-08-21 03:11:40 +00:00
|
|
|
|
2018-10-04 08:18:55 +00:00
|
|
|
// API USER Routes
|
2018-09-16 07:48:34 +00:00
|
|
|
r.Handle("/api/users", http.HandlerFunc(apiAllUsersHandler)).Methods("GET")
|
|
|
|
r.Handle("/api/users", http.HandlerFunc(apiCreateUsersHandler)).Methods("POST")
|
|
|
|
r.Handle("/api/users/{id}", http.HandlerFunc(apiUserHandler)).Methods("GET")
|
|
|
|
r.Handle("/api/users/{id}", http.HandlerFunc(apiUserUpdateHandler)).Methods("POST")
|
|
|
|
r.Handle("/api/users/{id}", http.HandlerFunc(apiUserDeleteHandler)).Methods("DELETE")
|
2018-08-21 03:11:40 +00:00
|
|
|
|
2018-11-07 07:53:39 +00:00
|
|
|
// API NOTIFIER Routes
|
2018-11-08 06:23:24 +00:00
|
|
|
r.Handle("/api/notifiers", http.HandlerFunc(apiNotifiersHandler)).Methods("GET")
|
2018-11-03 00:27:29 +00:00
|
|
|
r.Handle("/api/notifier/{notifier}", http.HandlerFunc(apiNotifierGetHandler)).Methods("GET")
|
2018-11-06 09:03:21 +00:00
|
|
|
r.Handle("/api/notifier/{notifier}", http.HandlerFunc(apiNotifierUpdateHandler)).Methods("POST")
|
2018-11-17 17:14:14 +00:00
|
|
|
r.Handle("/api/notifier/{method}/test", http.HandlerFunc(testNotificationHandler)).Methods("POST")
|
2018-11-02 23:19:52 +00:00
|
|
|
|
2018-11-07 07:53:39 +00:00
|
|
|
// API MESSAGES Routes
|
|
|
|
r.Handle("/api/messages", http.HandlerFunc(apiAllMessagesHandler)).Methods("GET")
|
2018-11-14 03:05:36 +00:00
|
|
|
r.Handle("/api/messages", http.HandlerFunc(apiMessageCreateHandler)).Methods("POST")
|
2018-11-07 07:53:39 +00:00
|
|
|
r.Handle("/api/messages/{id}", http.HandlerFunc(apiMessageGetHandler)).Methods("GET")
|
|
|
|
r.Handle("/api/messages/{id}", http.HandlerFunc(apiMessageUpdateHandler)).Methods("POST")
|
|
|
|
r.Handle("/api/messages/{id}", http.HandlerFunc(apiMessageDeleteHandler)).Methods("DELETE")
|
|
|
|
|
2018-11-21 03:39:40 +00:00
|
|
|
// API CHECKIN Routes
|
|
|
|
r.Handle("/api/checkins", http.HandlerFunc(apiAllCheckinsHandler)).Methods("GET")
|
|
|
|
r.Handle("/api/checkin/{api}", http.HandlerFunc(apiCheckinHandler)).Methods("GET")
|
|
|
|
r.Handle("/api/checkin", http.HandlerFunc(checkinCreateHandler)).Methods("POST")
|
|
|
|
r.Handle("/api/checkin/{api}", http.HandlerFunc(checkinDeleteHandler)).Methods("DELETE")
|
|
|
|
r.Handle("/checkin/{api}", http.HandlerFunc(checkinHitHandler))
|
|
|
|
|
|
|
|
// 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())))
|
|
|
|
|
2018-11-07 07:53:39 +00:00
|
|
|
// API Generic Routes
|
2018-09-16 07:48:34 +00:00
|
|
|
r.Handle("/metrics", http.HandlerFunc(prometheusHandler))
|
2018-11-06 01:34:02 +00:00
|
|
|
r.Handle("/health", http.HandlerFunc(healthCheckHandler))
|
2018-09-16 07:48:34 +00:00
|
|
|
r.Handle("/tray", http.HandlerFunc(trayHandler))
|
2018-12-04 23:26:07 +00:00
|
|
|
r.Handle("/.well-known/", http.StripPrefix("/.well-known/", http.FileServer(http.Dir(dir+"/.well-known"))))
|
2019-02-01 04:48:18 +00:00
|
|
|
|
2018-10-04 08:18:55 +00:00
|
|
|
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() {
|
2018-07-04 09:00:16 +00:00
|
|
|
if core.CoreApp != nil {
|
|
|
|
cookie := fmt.Sprintf("%v_%v", core.CoreApp.ApiSecret, time.Now().Nanosecond())
|
2018-10-06 05:56:08 +00:00
|
|
|
sessionStore = sessions.NewCookieStore([]byte(cookie))
|
2018-07-04 09:00:16 +00:00
|
|
|
} else {
|
2018-10-06 05:56:08 +00:00
|
|
|
sessionStore = sessions.NewCookieStore([]byte("secretinfo"))
|
2018-07-04 09:00:16 +00:00
|
|
|
}
|
2018-07-01 03:54:28 +00:00
|
|
|
}
|