add middleware to decline routes, while server is running

This commit is contained in:
knoxfighter 2021-01-27 03:40:28 +01:00
parent 8181744c01
commit 127ceeb094

View File

@ -2,6 +2,7 @@ package api
import (
"github.com/mroote/factorio-server-manager/api/websocket"
"github.com/mroote/factorio-server-manager/factorio"
"net/http"
"github.com/gorilla/mux"
@ -12,10 +13,24 @@ type Route struct {
Method string
Pattern string
HandlerFunc http.HandlerFunc
ServerOff bool // Set to `true' if factorio server has to be turned off to call this
}
type Routes []Route
func ServerOffMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// only run if server is turned off
server := factorio.GetFactorioServer()
if server.GetRunning() {
http.Error(w, "factorio server still running", http.StatusLocked)
} else {
next.ServeHTTP(w, r)
}
return
})
}
func NewRouter() *mux.Router {
r := mux.NewRouter().StrictSlash(true)
@ -27,8 +42,20 @@ func NewRouter() *mux.Router {
// Serves all JSON REST handlers prefixed with /api
s := r.PathPrefix("/api").Subrouter()
s.Use(AuthMiddleware)
// use subrouter for calls, that run only, when server is turned off
so := s.NewRoute().Subrouter()
so.Use(ServerOffMiddleware)
s.NewRoute().Subrouter()
for _, route := range apiRoutes {
s.Methods(route.Method).
var router *mux.Router
if route.ServerOff {
router = so
} else {
router = s
}
router.Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(route.HandlerFunc)
@ -38,7 +65,6 @@ func NewRouter() *mux.Router {
r.Path("/api/login").
Methods("POST").
Name("LoginUser").
//HandlerFunc(LoginUser)
HandlerFunc(LoginUser)
// Route for initializing websocket connection
@ -114,106 +140,127 @@ var apiRoutes = Routes{
"GET",
"/saves/list",
ListSaves,
false,
}, {
"DlSave",
"GET",
"/saves/dl/{save}",
DLSave,
false,
}, {
"UploadSave",
"POST",
"/saves/upload",
UploadSave,
false,
}, {
"RemoveSave",
"GET",
"/saves/rm/{save}",
RemoveSave,
false,
}, {
"CreateSave",
"GET",
"/saves/create/{save}",
CreateSaveHandler,
true,
}, {
"LoadModsFromSave",
"POST",
"/saves/mods",
LoadModsFromSaveHandler,
true,
}, {
"LogTail",
"GET",
"/log/tail",
LogTail,
false,
}, {
"LoadConfig",
"GET",
"/config",
LoadConfig,
false,
}, {
"StartServer",
"POST",
"/server/start",
StartServer,
true,
}, {
"StopServer",
"GET",
"/server/stop",
StopServer,
false,
}, {
"KillServer",
"GET",
"/server/kill",
KillServer,
false,
}, {
"RunningServer",
"GET",
"/server/status",
CheckServer,
false,
}, {
"FactorioVersion",
"GET",
"/server/facVersion",
FactorioVersion,
false,
}, {
"LogoutUser",
"GET",
"/logout",
LogoutUser,
false,
}, {
"StatusUser",
"GET",
"/user/status",
GetCurrentLogin,
false,
}, {
"ListUsers",
"GET",
"/user/list",
ListUsers,
false,
}, {
"AddUser",
"POST",
"/user/add",
AddUser,
false,
}, {
"RemoveUser",
"POST",
"/user/remove",
RemoveUser,
false,
}, {
"ChangePassword",
"POST",
"/user/password",
ChangePassword,
false,
}, {
"GetServerSettings",
"GET",
"/settings",
GetServerSettings,
false,
}, {
"UpdateServerSettings",
"POST",
"/settings/update",
UpdateServerSettings,
false,
},
// Mod Portal Stuff
{
@ -221,36 +268,43 @@ var apiRoutes = Routes{
"GET",
"/mods/portal/list",
ModPortalListModsHandler,
false,
}, {
"ModPortalGetModInfo",
"GET",
"/mods/portal/info/{mod}",
ModPortalModInfoHandler,
false,
}, {
"ModPortalInstallMod",
"POST",
"/mods/portal/install",
ModPortalInstallHandler,
true,
}, {
"ModPortalLogin",
"POST",
"/mods/portal/login",
ModPortalLoginHandler,
false,
}, {
"ModPortalLoginStatus",
"GET",
"/mods/portal/loginstatus",
ModPortalLoginStatusHandler,
false,
}, {
"ModPortalLogout",
"GET",
"/mods/portal/logout",
ModPortalLogoutHandler,
false,
}, {
"ModPortalInstallMultiple",
"POST",
"/mods/portal/install/multiple",
ModPortalInstallMultipleHandler,
true,
},
// Mods Stuff
{
@ -258,36 +312,43 @@ var apiRoutes = Routes{
"GET",
"/mods/list",
ListInstalledModsHandler,
false,
}, {
"ToggleMod",
"POST",
"/mods/toggle",
ModToggleHandler,
true,
}, {
"DeleteMod",
"POST",
"/mods/delete",
ModDeleteHandler,
true,
}, {
"DeleteAllMods",
"POST",
"/mods/delete/all",
ModDeleteAllHandler,
true,
}, {
"UpdateMod",
"POST",
"/mods/update",
ModUpdateHandler,
true,
}, {
"UploadMod",
"POST",
"/mods/upload",
ModUploadHandler,
true,
}, {
"DownloadMods",
"GET",
"/mods/download",
ModDownloadHandler,
false,
},
// Mod Packs
{
@ -295,26 +356,31 @@ var apiRoutes = Routes{
"GET",
"/mods/packs/list",
ModPackListHandler,
false,
}, {
"ModPackCreate",
"POST",
"/mods/packs/create",
ModPackCreateHandler,
false,
}, {
"ModPackDelete",
"POST",
"/mods/packs/{modpack}/delete",
ModPackDeleteHandler,
false,
}, {
"ModPackDownload",
"GET",
"/mods/packs/{modpack}/download",
ModPackDownloadHandler,
false,
}, {
"LoadModPack",
"POST",
"/mods/packs/{modpack}/load",
ModPackLoadHandler,
true,
},
// Mods inside Mod Packs
{
@ -322,40 +388,48 @@ var apiRoutes = Routes{
"GET",
"/mods/packs/{modpack}/list",
ModPackModListHandler,
false,
}, {
"ModPackToggleMod",
"POST",
"/mods/packs/{modpack}/mod/toggle",
ModPackModToggleHandler,
false,
}, {
"ModPackDeleteMod",
"POST",
"/mods/packs/{modpack}/mod/delete",
ModPackModDeleteHandler,
false,
}, {
"ModPackDeleteAllMod",
"POST",
"/mods/packs/{modpack}/mod/delete/all",
ModPackModDeleteAllHandler,
false,
}, {
"ModPackUpdateMod",
"POST",
"/mods/packs/{modpack}/mod/update",
ModPackModUpdateHandler,
false,
}, {
"ModPackUploadMod",
"POST",
"/mods/packs/{modpack}/mod/upload",
ModPackModUploadHandler,
false,
}, {
"ModPackModPortalInstallMod",
"POST",
"/mods/packs/{modpack}/portal/install",
ModPackModPortalInstallHandler,
false,
}, {
"ModPackModPortalInstallMultiple",
"POST",
"/mods/packs/{modpack}/portal/install/multiple",
ModPackModPortalInstallMultipleHandler,
false,
},
}