36 lines
848 B
Go
36 lines
848 B
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
|
|
"tomatentum.net/svg-templater/internal/routes"
|
|
"tomatentum.net/svg-templater/pkg/auth"
|
|
)
|
|
|
|
func PrepareHTTP() {
|
|
registerAuthorized("/", func(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Fprintln(w, "You are authorized!")
|
|
})
|
|
registerAuthorized("/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)
|
|
})
|
|
}
|
|
|
|
func Start() {
|
|
log.Println("Starting http server on :3000")
|
|
if err := http.ListenAndServe(":3000", nil); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func registerAuthorized(path string, f func(w http.ResponseWriter, r *http.Request)) {
|
|
http.HandleFunc(path, auth.AuthMiddleware(http.HandlerFunc(f)))
|
|
log.Println("Registered authorized handler for", path)
|
|
}
|