2016-04-18 00:20:06 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-04-29 16:37:21 +02:00
|
|
|
"log"
|
2016-04-18 00:20:06 +02:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Route struct {
|
|
|
|
Name string
|
|
|
|
Method string
|
|
|
|
Pattern string
|
|
|
|
HandlerFunc http.HandlerFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
type Routes []Route
|
|
|
|
|
|
|
|
func NewRouter() *mux.Router {
|
2016-04-19 19:55:21 +02:00
|
|
|
r := mux.NewRouter().StrictSlash(true)
|
2016-04-18 00:20:06 +02:00
|
|
|
|
2016-04-29 16:37:21 +02:00
|
|
|
r.Path("/login").
|
|
|
|
Methods("GET").
|
|
|
|
Name("Login").
|
|
|
|
Handler(http.StripPrefix("/login", http.FileServer(http.Dir("./app/"))))
|
2016-05-06 16:49:01 +02:00
|
|
|
r.Path("/login").
|
|
|
|
Methods("POST").
|
|
|
|
Name("LoginPOST").
|
|
|
|
HandlerFunc(LoginUser)
|
2016-04-29 16:37:21 +02:00
|
|
|
|
2016-04-19 19:55:21 +02:00
|
|
|
// API subrouter
|
2016-04-26 02:38:51 +02:00
|
|
|
// Serves all JSON REST handlers prefixed with /api
|
2016-04-19 19:55:21 +02:00
|
|
|
s := r.PathPrefix("/api").Subrouter()
|
|
|
|
for _, route := range apiRoutes {
|
2016-04-20 03:45:49 +02:00
|
|
|
s.Methods(route.Method).
|
2016-04-18 00:20:06 +02:00
|
|
|
Path(route.Pattern).
|
|
|
|
Name(route.Name).
|
2016-05-06 16:49:01 +02:00
|
|
|
Handler(CheckSession(route.HandlerFunc))
|
2016-04-18 00:20:06 +02:00
|
|
|
}
|
|
|
|
|
2016-04-19 19:55:21 +02:00
|
|
|
// Serves the frontend application from the app directory
|
2016-04-20 03:45:49 +02:00
|
|
|
// Uses basic file server to serve index.html and Javascript application
|
2016-04-21 17:05:28 +02:00
|
|
|
// Routes match the ones defined in React application
|
2016-05-07 22:36:42 +02:00
|
|
|
r.Path("/settings").
|
|
|
|
Methods("GET").
|
|
|
|
Name("Settings").
|
|
|
|
Handler(CheckSession(http.StripPrefix("/settings", http.FileServer(http.Dir("./app/")))))
|
2016-04-21 17:05:28 +02:00
|
|
|
r.Path("/mods").
|
|
|
|
Methods("GET").
|
|
|
|
Name("Mods").
|
2016-04-29 18:53:05 +02:00
|
|
|
Handler(CheckSession(http.StripPrefix("/mods", http.FileServer(http.Dir("./app/")))))
|
2016-04-21 17:05:28 +02:00
|
|
|
r.Path("/saves").
|
|
|
|
Methods("GET").
|
|
|
|
Name("Saves").
|
2016-04-29 18:53:05 +02:00
|
|
|
Handler(CheckSession(http.StripPrefix("/saves", http.FileServer(http.Dir("./app/")))))
|
2016-04-21 17:05:28 +02:00
|
|
|
r.Path("/logs").
|
|
|
|
Methods("GET").
|
|
|
|
Name("Logs").
|
2016-04-29 18:53:05 +02:00
|
|
|
Handler(CheckSession(http.StripPrefix("/logs", http.FileServer(http.Dir("./app/")))))
|
2016-04-25 01:07:00 +02:00
|
|
|
r.Path("/config").
|
|
|
|
Methods("GET").
|
|
|
|
Name("Config").
|
2016-04-29 18:53:05 +02:00
|
|
|
Handler(CheckSession(http.StripPrefix("/config", http.FileServer(http.Dir("./app/")))))
|
|
|
|
r.Path("/server").
|
|
|
|
Methods("GET").
|
|
|
|
Name("Server").
|
|
|
|
Handler(CheckSession(http.StripPrefix("/server", http.FileServer(http.Dir("./app/")))))
|
2016-04-20 03:45:49 +02:00
|
|
|
r.PathPrefix("/").
|
2016-04-19 19:55:21 +02:00
|
|
|
Methods("GET").
|
|
|
|
Name("Index").
|
|
|
|
Handler(http.FileServer(http.Dir("./app/")))
|
|
|
|
|
|
|
|
return r
|
2016-04-18 00:20:06 +02:00
|
|
|
}
|
|
|
|
|
2016-04-29 16:37:21 +02:00
|
|
|
// Middleware returns a http.HandlerFunc which authenticates the users request
|
2016-05-07 22:36:42 +02:00
|
|
|
// Redirects user to login page if no session is found
|
2016-04-29 16:37:21 +02:00
|
|
|
func CheckSession(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if err := Auth.aaa.Authorize(w, r, true); err != nil {
|
|
|
|
log.Printf("Unauthenticated request %s %s %s", r.Method, r.Host, r.RequestURI)
|
|
|
|
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-04-20 03:45:49 +02:00
|
|
|
// Defines all API REST endpoints
|
|
|
|
// All routes are prefixed with /api
|
2016-04-19 19:55:21 +02:00
|
|
|
var apiRoutes = Routes{
|
2016-04-18 00:20:06 +02:00
|
|
|
Route{
|
2016-04-18 02:18:16 +02:00
|
|
|
"ListInstalledMods",
|
|
|
|
"GET",
|
|
|
|
"/mods/list/installed",
|
|
|
|
ListInstalledMods,
|
2016-04-18 00:20:06 +02:00
|
|
|
}, {
|
|
|
|
"ListMods",
|
|
|
|
"GET",
|
|
|
|
"/mods/list",
|
|
|
|
ListMods,
|
|
|
|
}, {
|
|
|
|
"ToggleMod",
|
|
|
|
"GET",
|
|
|
|
"/mods/toggle/{mod}",
|
|
|
|
ToggleMod,
|
2016-04-21 17:05:28 +02:00
|
|
|
}, {
|
|
|
|
"UploadMod",
|
|
|
|
"POST",
|
|
|
|
"/mods/upload",
|
|
|
|
UploadMod,
|
|
|
|
}, {
|
|
|
|
"RemoveMod",
|
|
|
|
"GET",
|
|
|
|
"/mods/rm/{mod}",
|
|
|
|
RemoveMod,
|
|
|
|
}, {
|
|
|
|
"DownloadMod",
|
|
|
|
"GET",
|
|
|
|
"/mods/dl/{mod}",
|
|
|
|
DownloadMod,
|
2016-04-18 02:18:16 +02:00
|
|
|
}, {
|
|
|
|
"ListSaves",
|
|
|
|
"GET",
|
|
|
|
"/saves/list",
|
|
|
|
ListSaves,
|
2016-04-20 03:45:49 +02:00
|
|
|
}, {
|
|
|
|
"DlSave",
|
|
|
|
"GET",
|
|
|
|
"/saves/dl/{save}",
|
|
|
|
DLSave,
|
2016-04-21 17:05:28 +02:00
|
|
|
}, {
|
|
|
|
"UploadSave",
|
|
|
|
"POST",
|
|
|
|
"/saves/upload",
|
|
|
|
UploadSave,
|
|
|
|
}, {
|
|
|
|
"RemoveSave",
|
|
|
|
"GET",
|
|
|
|
"/saves/rm/{save}",
|
|
|
|
RemoveSave,
|
2016-04-25 03:38:06 +02:00
|
|
|
}, {
|
|
|
|
"CreateSave",
|
|
|
|
"GET",
|
|
|
|
"/saves/create/{save}",
|
|
|
|
CreateSaveHandler,
|
2016-04-18 18:39:09 +02:00
|
|
|
}, {
|
2016-04-18 22:01:45 +02:00
|
|
|
"LogTail",
|
2016-04-18 18:39:09 +02:00
|
|
|
"GET",
|
|
|
|
"/log/tail",
|
|
|
|
LogTail,
|
2016-04-24 01:14:13 +02:00
|
|
|
}, {
|
|
|
|
"LoadConfig",
|
|
|
|
"GET",
|
2016-04-25 01:07:00 +02:00
|
|
|
"/config",
|
2016-04-24 01:14:13 +02:00
|
|
|
LoadConfig,
|
2016-04-26 02:38:51 +02:00
|
|
|
}, {
|
|
|
|
"StartServer",
|
|
|
|
"GET",
|
|
|
|
"/server/start",
|
|
|
|
StartServer,
|
|
|
|
}, {
|
|
|
|
"StartServer",
|
|
|
|
"POST",
|
|
|
|
"/server/start",
|
|
|
|
StartServer,
|
|
|
|
}, {
|
|
|
|
"StopServer",
|
|
|
|
"GET",
|
|
|
|
"/server/stop",
|
|
|
|
StopServer,
|
|
|
|
}, {
|
|
|
|
"RunningServer",
|
|
|
|
"GET",
|
|
|
|
"/server/status",
|
2016-04-28 02:12:00 +02:00
|
|
|
CheckServer,
|
2016-04-30 20:35:10 +02:00
|
|
|
}, {
|
|
|
|
"LogoutUser",
|
|
|
|
"GET",
|
|
|
|
"/logout",
|
|
|
|
LogoutUser,
|
|
|
|
}, {
|
2016-05-05 03:05:50 +02:00
|
|
|
"StatusUser",
|
2016-04-30 20:35:10 +02:00
|
|
|
"GET",
|
|
|
|
"/user/status",
|
|
|
|
GetCurrentLogin,
|
2016-05-05 03:05:50 +02:00
|
|
|
}, {
|
|
|
|
"ListUsers",
|
|
|
|
"GET",
|
|
|
|
"/user/list",
|
|
|
|
ListUsers,
|
2016-05-06 16:49:01 +02:00
|
|
|
}, {
|
|
|
|
"AddUser",
|
|
|
|
"POST",
|
|
|
|
"/user/add",
|
|
|
|
AddUser,
|
2016-05-07 22:36:42 +02:00
|
|
|
}, {
|
|
|
|
"RemoveUser",
|
|
|
|
"POST",
|
|
|
|
"/user/remove",
|
|
|
|
RemoveUser,
|
2016-04-18 00:20:06 +02:00
|
|
|
},
|
|
|
|
}
|