58 lines
1.5 KiB
Go
58 lines
1.5 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
|
|
"tomatentum.net/svg-templater/internal/routes"
|
|
"tomatentum.net/svg-templater/pkg/auth"
|
|
"tomatentum.net/svg-templater/pkg/svg"
|
|
)
|
|
|
|
func PrepareHTTP() {
|
|
registerAuthorizedFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Fprintln(w, "You are authorized!")
|
|
})
|
|
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)
|
|
})
|
|
registerAuthorizedFunc("/svg/{id}", func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != "GET" {
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
routes.DownloadSVG(w, r)
|
|
})
|
|
registerAuthorizedFunc("/font/", func(w http.ResponseWriter, r *http.Request) {
|
|
switch r.Method {
|
|
case "GET":
|
|
routes.GetFonts(w, r)
|
|
case "POST":
|
|
routes.AddFont(w, r)
|
|
}
|
|
})
|
|
|
|
http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(svg.Storage.GetPublicDir())))
|
|
}
|
|
|
|
func Start() {
|
|
log.Println("Starting http server on :3000")
|
|
if err := http.ListenAndServe(":3000", nil); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
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))
|
|
}
|