2016-04-18 00:20:06 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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-19 19:55:21 +02:00
|
|
|
// API subrouter
|
|
|
|
// Serves all REST handlers prefixed with /api
|
|
|
|
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).
|
|
|
|
Handler(route.HandlerFunc)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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-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-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-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-18 00:20:06 +02:00
|
|
|
},
|
|
|
|
}
|