feat(template): add Fileserver for download
Some checks failed
build / Go-Build (push) Failing after 12m37s
Some checks failed
build / Go-Build (push) Failing after 12m37s
This commit is contained in:
@@ -4,22 +4,26 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
|
||||
"tomatentum.net/svg-templater/internal/routes"
|
||||
"tomatentum.net/svg-templater/pkg/auth"
|
||||
"tomatentum.net/svg-templater/pkg/svg"
|
||||
)
|
||||
|
||||
func PrepareHTTP() {
|
||||
registerAuthorized("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
registerAuthorizedFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintln(w, "You are authorized!")
|
||||
})
|
||||
registerAuthorized("/svg/", func(w http.ResponseWriter, r *http.Request) {
|
||||
registerAuthorizedFunc("/svg/", func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != "POST" {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
routes.CreateSVG(w, *r)
|
||||
})
|
||||
|
||||
registerAuthorized("/public/", http.StripPrefix("/public/", http.FileServer(svg.Storage.GetPublicDir())))
|
||||
}
|
||||
|
||||
func Start() {
|
||||
@@ -29,7 +33,15 @@ func Start() {
|
||||
}
|
||||
}
|
||||
|
||||
func registerAuthorized(path string, f func(w http.ResponseWriter, r *http.Request)) {
|
||||
http.HandleFunc(path, auth.AuthMiddleware(http.HandlerFunc(f)))
|
||||
func registerAuthorized(path string, handler http.Handler) {
|
||||
http.HandleFunc(path, auth.AuthMiddleware(handler))
|
||||
log.Println("Registered authorized handler for", path)
|
||||
}
|
||||
|
||||
func registerAuthorizedFunc(path string, f func(w http.ResponseWriter, r *http.Request)) {
|
||||
registerAuthorized(path, http.HandlerFunc(f))
|
||||
}
|
||||
|
||||
func GetPublicPath(path string) string {
|
||||
return filepath.Join("public", path)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user