mirror of
https://github.com/gmemstr/sliproad.git
synced 2024-09-20 00:21:15 +01:00
dad608edbc
Theorectically, this means that the entire binary is now self contained, minus the need for the config file. Ripped out the redundant static file directives and root handler while I was at it.
66 lines
1.4 KiB
Go
66 lines
1.4 KiB
Go
package router
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"io/fs"
|
|
|
|
"github.com/gorilla/mux"
|
|
)
|
|
|
|
type handler func(context *requestContext, w http.ResponseWriter, r *http.Request) *httpError
|
|
|
|
type httpError struct {
|
|
Message string
|
|
StatusCode int
|
|
}
|
|
|
|
type requestContext struct{}
|
|
|
|
// Loop through passed functions and execute them, passing through the current
|
|
// requestContext, response writer and request reader.
|
|
func handle(handlers ...handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
context := &requestContext{}
|
|
for _, handler := range handlers {
|
|
err := handler(context, w, r)
|
|
if err != nil {
|
|
log.Printf("%v", err)
|
|
w.Write([]byte(http.StatusText(err.StatusCode)))
|
|
return
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
// Init initializes the main router and all routes for the application.
|
|
func Init(sc fs.FS) *mux.Router {
|
|
|
|
r := mux.NewRouter()
|
|
|
|
// File & Provider API
|
|
r.Handle("/api/providers", handle(
|
|
requiresAuth(),
|
|
listProviders(),
|
|
)).Methods("GET")
|
|
|
|
r.Handle(`/api/files/{provider:[a-zA-Z0-9]+\/*}`, handle(
|
|
requiresAuth(),
|
|
handleProvider(),
|
|
)).Methods("GET", "POST", "DELETE")
|
|
|
|
r.Handle(`/api/files/{provider:[a-zA-Z0-9]+}/{file:.+}`, handle(
|
|
requiresAuth(),
|
|
handleProvider(),
|
|
)).Methods("GET", "POST", "DELETE")
|
|
|
|
// Auth API & Endpoints
|
|
r.Handle(`/api/auth/callback`, handle(
|
|
callbackAuth(),
|
|
)).Methods("GET", "POST")
|
|
|
|
r.PathPrefix("/").Handler(http.StripPrefix("/", http.FileServer(http.FS(sc))))
|
|
|
|
return r
|
|
}
|