Move handler out of cmd and into separate pacakge

This commit is contained in:
Sasha Koshka 2024-12-07 02:27:34 -05:00
parent e7e31f0e60
commit 7c9e1a429e
2 changed files with 22 additions and 21 deletions

View File

@ -12,6 +12,7 @@ import "git.tebibyte.media/sashakoshka/go-cli"
import "git.tebibyte.media/sashakoshka/step/providers" import "git.tebibyte.media/sashakoshka/step/providers"
import "git.tebibyte.media/sashakoshka/goutil/container" import "git.tebibyte.media/sashakoshka/goutil/container"
import "git.tebibyte.media/sashakoshka/go-service/daemon" import "git.tebibyte.media/sashakoshka/go-service/daemon"
import stephttp"git.tebibyte.media/sashakoshka/step/http"
import "git.tebibyte.media/sashakoshka/go-service/routines" import "git.tebibyte.media/sashakoshka/go-service/routines"
func main () { func main () {
@ -45,11 +46,11 @@ func main () {
if err != nil { log.Fatal(err) } if err != nil { log.Fatal(err) }
// set up the HTTP handler // set up the HTTP handler
handler := handler { handler := stephttp.Handler {
environment: &environment, Environment: &environment,
directories: flagDirectories.Value == "true", Directories: flagDirectories.Value == "true",
stepExt: ucontainer.NewSet(".step"), StepExt: ucontainer.NewSet(".step"),
index: []string { "index.step", "index.html", "index" }, Index: []string { "index.step", "index.html", "index" },
} }
// set up the HTTP server // set up the HTTP server

View File

@ -1,4 +1,4 @@
package main package http
import "log" import "log"
import "fmt" import "fmt"
@ -11,14 +11,14 @@ import "path/filepath"
import "git.tebibyte.media/sashakoshka/step" import "git.tebibyte.media/sashakoshka/step"
import "git.tebibyte.media/sashakoshka/goutil/container" import "git.tebibyte.media/sashakoshka/goutil/container"
type handler struct { type Handler struct {
environment *step.Environment Environment *step.Environment
directories bool Directories bool
stepExt ucontainer.Set[string] StepExt ucontainer.Set[string]
index []string Index []string
} }
func (this *handler) ServeHTTP (res http.ResponseWriter, req *http.Request) { func (this *Handler) ServeHTTP (res http.ResponseWriter, req *http.Request) {
remoteAddr := req.RemoteAddr remoteAddr := req.RemoteAddr
if addr := req.Header.Get("CF-Connecting-IP"); addr != "" { if addr := req.Header.Get("CF-Connecting-IP"); addr != "" {
remoteAddr = fmt.Sprintf("%s --CF-> %s", addr, req.RemoteAddr) remoteAddr = fmt.Sprintf("%s --CF-> %s", addr, req.RemoteAddr)
@ -26,7 +26,7 @@ func (this *handler) ServeHTTP (res http.ResponseWriter, req *http.Request) {
remoteAddr = fmt.Sprintf("%s --??-> %s", addr, req.RemoteAddr) remoteAddr = fmt.Sprintf("%s --??-> %s", addr, req.RemoteAddr)
} }
log.Println("(i)", req.Method, req.URL, "from", remoteAddr) log.Println("(i)", req.Method, req.URL, "from", remoteAddr)
filesystem := this.environment.GetFS() filesystem := this.Environment.GetFS()
// normalize path // normalize path
pat := req.URL.Path pat := req.URL.Path
@ -45,7 +45,7 @@ func (this *handler) ServeHTTP (res http.ResponseWriter, req *http.Request) {
} }
if info.IsDir() { if info.IsDir() {
// try to find an index // try to find an index
for _, base := range this.index { for _, base := range this.Index {
currentPath := path.Join(pat, base) currentPath := path.Join(pat, base)
info, err := statFile(filesystem, pathToName(currentPath)) info, err := statFile(filesystem, pathToName(currentPath))
if err != nil { continue } if err != nil { continue }
@ -54,7 +54,7 @@ func (this *handler) ServeHTTP (res http.ResponseWriter, req *http.Request) {
return return
} }
if !this.directories { if !this.Directories {
this.serveError(res, req, http.StatusForbidden, req.URL) this.serveError(res, req, http.StatusForbidden, req.URL)
return return
} }
@ -68,16 +68,16 @@ func (this *handler) ServeHTTP (res http.ResponseWriter, req *http.Request) {
this.serveFile(res, req, filesystem, pat) this.serveFile(res, req, filesystem, pat)
} }
func (this *handler) serveFile (res http.ResponseWriter, req *http.Request, filesystem fs.FS, pat string) { func (this *Handler) serveFile (res http.ResponseWriter, req *http.Request, filesystem fs.FS, pat string) {
name := pathToName(pat) name := pathToName(pat)
if !this.stepExt.Has(filepath.Ext(name)) { if !this.StepExt.Has(filepath.Ext(name)) {
// just a normal file // just a normal file
http.ServeFileFS(res, req, this.environment.GetFS(), name) http.ServeFileFS(res, req, this.Environment.GetFS(), name)
return return
} }
// parse // parse
document, err := this.environment.Parse(name) document, err := this.Environment.Parse(name)
if err != nil { if err != nil {
this.serveError(res, req, http.StatusInternalServerError, err) this.serveError(res, req, http.StatusInternalServerError, err)
return return
@ -119,7 +119,7 @@ func (this *handler) serveFile (res http.ResponseWriter, req *http.Request, file
} }
} }
func (this *handler) serveError (res http.ResponseWriter, req *http.Request, status int, message any) { func (this *Handler) serveError (res http.ResponseWriter, req *http.Request, status int, message any) {
res.Header().Add("Content-Type", "text/plain") res.Header().Add("Content-Type", "text/plain")
res.WriteHeader(status) res.WriteHeader(status)
// TODO: allow customization with templates // TODO: allow customization with templates
@ -131,7 +131,7 @@ func (this *handler) serveError (res http.ResponseWriter, req *http.Request, sta
log.Printf("ERR %d %s: %v\n", status, http.StatusText(status), message) log.Printf("ERR %d %s: %v\n", status, http.StatusText(status), message)
} }
func (this *handler) logErr (name string, err error) { func (this *Handler) logErr (name string, err error) {
log.Printf("ERR %s: %v\n", name, err) log.Printf("ERR %s: %v\n", name, err)
} }