2019-02-24 08:25:39 +00:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2021-05-23 17:31:57 +01:00
|
|
|
"io/fs"
|
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.
|
2021-05-23 17:31:57 +01:00
|
|
|
func Init(sc fs.FS) *mux.Router {
|
2019-02-24 08:25:39 +00:00
|
|
|
|
|
|
|
r := mux.NewRouter()
|
|
|
|
|
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(),
|
2021-05-23 17:31:57 +01:00
|
|
|
)).Methods("GET", "POST", "DELETE")
|
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")
|
|
|
|
|
2021-05-23 17:31:57 +01:00
|
|
|
r.PathPrefix("/").Handler(http.StripPrefix("/", http.FileServer(http.FS(sc))))
|
2019-02-24 08:25:39 +00:00
|
|
|
|
2021-05-23 17:31:57 +01:00
|
|
|
return r
|
2020-04-24 09:01:53 +01:00
|
|
|
}
|