2019-02-24 08:25:39 +00:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-04-02 20:50:39 +01:00
|
|
|
"io"
|
2019-02-24 08:25:39 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2020-04-02 20:50:39 +01:00
|
|
|
"os"
|
|
|
|
"strconv"
|
2020-04-24 09:01:53 +01:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2019-02-24 08:25:39 +00:00
|
|
|
)
|
|
|
|
|
2020-04-24 09:01:53 +01:00
|
|
|
type handler func(context *requestContext, w http.ResponseWriter, r *http.Request) *httpError
|
2020-04-02 20:50:39 +01:00
|
|
|
|
2020-04-24 09:01:53 +01:00
|
|
|
type httpError struct {
|
|
|
|
Message string
|
2020-04-02 20:50:39 +01:00
|
|
|
StatusCode int
|
|
|
|
}
|
2019-02-24 08:25:39 +00:00
|
|
|
|
2020-04-24 09:01:53 +01:00
|
|
|
type requestContext struct{}
|
2020-04-02 20:50:39 +01:00
|
|
|
|
2020-04-24 09:01:53 +01:00
|
|
|
// Loop through passed functions and execute them, passing through the current
|
|
|
|
// requestContext, response writer and request reader.
|
|
|
|
func handle(handlers ...handler) http.Handler {
|
2020-04-02 20:50:39 +01:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2020-04-24 09:01:53 +01:00
|
|
|
context := &requestContext{}
|
2019-02-24 08:25:39 +00:00
|
|
|
for _, handler := range handlers {
|
2020-04-02 20:50:39 +01:00
|
|
|
err := handler(context, w, r)
|
2019-02-24 08:25:39 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("%v", err)
|
|
|
|
w.Write([]byte(http.StatusText(err.StatusCode)))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-24 09:01:53 +01:00
|
|
|
// Init initializes the main router and all routes for the application.
|
2019-02-24 08:25:39 +00:00
|
|
|
func Init() *mux.Router {
|
|
|
|
|
|
|
|
r := mux.NewRouter()
|
|
|
|
|
|
|
|
// "Static" paths
|
2020-03-22 00:18:19 +00:00
|
|
|
r.PathPrefix("/javascript/").Handler(http.StripPrefix("/javascript/", http.FileServer(http.Dir("assets/web/javascript"))))
|
|
|
|
r.PathPrefix("/css/").Handler(http.StripPrefix("/css/", http.FileServer(http.Dir("assets/web/css"))))
|
2020-04-05 19:32:35 +01:00
|
|
|
r.PathPrefix("/icons/").Handler(http.StripPrefix("/icons/", http.FileServer(http.Dir("assets/web/icons"))))
|
2019-02-24 08:25:39 +00:00
|
|
|
|
|
|
|
// Paths that require specific handlers
|
2020-04-24 09:01:53 +01:00
|
|
|
r.Handle("/", handle(
|
2020-04-16 23:49:35 +01:00
|
|
|
requiresAuth(),
|
2019-02-24 08:25:39 +00:00
|
|
|
rootHandler(),
|
|
|
|
)).Methods("GET")
|
2019-07-14 04:19:28 +01:00
|
|
|
|
2020-04-16 23:49:35 +01:00
|
|
|
// File & Provider API
|
2020-04-24 09:01:53 +01:00
|
|
|
r.Handle("/api/providers", handle(
|
2020-04-16 23:49:35 +01:00
|
|
|
requiresAuth(),
|
2020-04-24 09:01:53 +01:00
|
|
|
listProviders(),
|
2019-04-02 06:43:57 +01:00
|
|
|
)).Methods("GET")
|
2020-04-24 09:01:53 +01:00
|
|
|
|
|
|
|
r.Handle(`/api/files/{provider:[a-zA-Z0-9]+\/*}`, handle(
|
2020-04-16 23:49:35 +01:00
|
|
|
requiresAuth(),
|
2020-04-24 09:01:53 +01:00
|
|
|
handleProvider(),
|
2020-03-30 20:51:26 +01:00
|
|
|
)).Methods("GET", "POST")
|
2019-07-28 05:43:05 +01:00
|
|
|
|
2020-04-24 09:01:53 +01:00
|
|
|
r.Handle(`/api/files/{provider:[a-zA-Z0-9]+}/{file:.+}`, handle(
|
2020-04-16 23:49:35 +01:00
|
|
|
requiresAuth(),
|
2020-04-24 09:01:53 +01:00
|
|
|
handleProvider(),
|
2020-04-12 22:10:51 +01:00
|
|
|
)).Methods("GET", "POST", "DELETE")
|
2019-02-24 08:25:39 +00:00
|
|
|
|
2020-04-16 23:49:35 +01:00
|
|
|
// Auth API & Endpoints
|
2020-04-24 09:01:53 +01:00
|
|
|
r.Handle(`/api/auth/callback`, handle(
|
2020-04-16 23:49:35 +01:00
|
|
|
callbackAuth(),
|
|
|
|
)).Methods("GET", "POST")
|
|
|
|
|
2019-02-24 08:25:39 +00:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2020-04-02 20:50:39 +01:00
|
|
|
// Handles serving index page.
|
2020-04-24 09:01:53 +01:00
|
|
|
func rootHandler() handler {
|
|
|
|
return func(context *requestContext, w http.ResponseWriter, r *http.Request) *httpError {
|
2020-04-02 20:50:39 +01:00
|
|
|
f, err := os.Open("assets/web/index.html")
|
2019-07-14 04:19:28 +01:00
|
|
|
if err != nil {
|
2020-04-24 09:01:53 +01:00
|
|
|
return &httpError{
|
2020-04-02 20:50:39 +01:00
|
|
|
Message: fmt.Sprintf("error serving index page from assets/web"),
|
2019-07-14 04:19:28 +01:00
|
|
|
StatusCode: http.StatusInternalServerError,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-02 20:50:39 +01:00
|
|
|
defer f.Close()
|
|
|
|
stats, err := f.Stat()
|
2019-07-14 04:19:28 +01:00
|
|
|
if err != nil {
|
2020-04-24 09:01:53 +01:00
|
|
|
return &httpError{
|
2020-04-02 20:50:39 +01:00
|
|
|
Message: fmt.Sprintf("error serving index page from assets/web"),
|
|
|
|
StatusCode: http.StatusInternalServerError,
|
2019-07-14 04:19:28 +01:00
|
|
|
}
|
2020-04-02 20:50:39 +01:00
|
|
|
} else {
|
|
|
|
w.Header().Add("Content-Length", strconv.FormatInt(stats.Size(), 10))
|
2019-07-14 04:19:28 +01:00
|
|
|
}
|
|
|
|
|
2020-04-02 20:50:39 +01:00
|
|
|
_, err = io.Copy(w, f)
|
|
|
|
if err != nil {
|
2020-04-24 09:01:53 +01:00
|
|
|
return &httpError{
|
2020-04-02 20:50:39 +01:00
|
|
|
Message: fmt.Sprintf("error serving index page from assets/web"),
|
|
|
|
StatusCode: http.StatusInternalServerError,
|
2019-02-24 08:25:39 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-02 20:50:39 +01:00
|
|
|
return nil
|
2019-02-24 08:25:39 +00:00
|
|
|
}
|
2020-04-24 09:01:53 +01:00
|
|
|
}
|