-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.go
45 lines (37 loc) · 1.14 KB
/
routes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import (
"github.com/gorilla/mux"
"log"
"net/http"
"os"
"path/filepath"
)
// Return a http.ServeMux with all the routes bound to it
func register_routes() *mux.Router {
// r := mux
r := mux.NewRouter()
wapi := r.PathPrefix("/wapi").Subrouter()
wapi.HandleFunc("/", websocketApiHandler)
rapi := r.PathPrefix("/rapi").Subrouter()
rapi.HandleFunc("/plex-login", insertPlexLoginData).Methods("POST")
// TODO: The static router should be done in nginx I think
staticFileHandler := getHTTPFileHandler("dist/")
r.PathPrefix("/static/").Handler(staticFileHandler)
// Server up the index and favicon.ico
r.Handle("/", staticFileHandler)
return r
}
// Registers the directory string as a generic file handler. Checks if the
// directory that we are trying to serve exists relative to the current
// directory we are in.
// TODO: Bubble up error
func getHTTPFileHandler(directory string) http.Handler {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatal(err)
}
// Everything checks out, server up the static page
fs := http.Dir(dir + "/" + directory)
fileHandler := http.FileServer(fs)
return fileHandler
}