factorio-server-manager/routes.go

76 lines
1.2 KiB
Go
Raw Normal View History

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 {
r := mux.NewRouter().StrictSlash(true)
2016-04-18 00:20:06 +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)
}
// 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("/").
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
var apiRoutes = Routes{
2016-04-18 00:20:06 +02:00
Route{
"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,
}, {
"ListSaves",
"GET",
"/saves/list",
ListSaves,
2016-04-20 03:45:49 +02:00
}, {
"DlSave",
"GET",
"/saves/dl/{save}",
DLSave,
}, {
"LogTail",
"GET",
"/log/tail",
LogTail,
2016-04-18 00:20:06 +02:00
},
}