Move handler out of cmd and into separate pacakge
This commit is contained in:
		
							parent
							
								
									e7e31f0e60
								
							
						
					
					
						commit
						7c9e1a429e
					
				@ -12,6 +12,7 @@ import "git.tebibyte.media/sashakoshka/go-cli"
 | 
			
		||||
import "git.tebibyte.media/sashakoshka/step/providers"
 | 
			
		||||
import "git.tebibyte.media/sashakoshka/goutil/container"
 | 
			
		||||
import "git.tebibyte.media/sashakoshka/go-service/daemon"
 | 
			
		||||
import stephttp"git.tebibyte.media/sashakoshka/step/http"
 | 
			
		||||
import "git.tebibyte.media/sashakoshka/go-service/routines"
 | 
			
		||||
 | 
			
		||||
func main () {
 | 
			
		||||
@ -45,11 +46,11 @@ func main () {
 | 
			
		||||
	if err != nil { log.Fatal(err) }
 | 
			
		||||
 | 
			
		||||
	// set up the HTTP handler
 | 
			
		||||
	handler := handler {
 | 
			
		||||
		environment: &environment,
 | 
			
		||||
		directories: flagDirectories.Value == "true",
 | 
			
		||||
		stepExt:     ucontainer.NewSet(".step"),
 | 
			
		||||
		index:       []string { "index.step", "index.html", "index" },
 | 
			
		||||
	handler := stephttp.Handler {
 | 
			
		||||
		Environment: &environment,
 | 
			
		||||
		Directories: flagDirectories.Value == "true",
 | 
			
		||||
		StepExt:     ucontainer.NewSet(".step"),
 | 
			
		||||
		Index:       []string { "index.step", "index.html", "index" },
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// set up the HTTP server
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,4 @@
 | 
			
		||||
package main
 | 
			
		||||
package http
 | 
			
		||||
 | 
			
		||||
import "log"
 | 
			
		||||
import "fmt"
 | 
			
		||||
@ -11,14 +11,14 @@ import "path/filepath"
 | 
			
		||||
import "git.tebibyte.media/sashakoshka/step"
 | 
			
		||||
import "git.tebibyte.media/sashakoshka/goutil/container"
 | 
			
		||||
 | 
			
		||||
type handler struct {
 | 
			
		||||
	environment *step.Environment
 | 
			
		||||
	directories bool
 | 
			
		||||
	stepExt     ucontainer.Set[string]
 | 
			
		||||
	index       []string
 | 
			
		||||
type Handler struct {
 | 
			
		||||
	Environment *step.Environment
 | 
			
		||||
	Directories bool
 | 
			
		||||
	StepExt     ucontainer.Set[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
 | 
			
		||||
	if addr := req.Header.Get("CF-Connecting-IP"); addr != "" {
 | 
			
		||||
		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)
 | 
			
		||||
	}
 | 
			
		||||
	log.Println("(i)", req.Method, req.URL, "from", remoteAddr)
 | 
			
		||||
	filesystem := this.environment.GetFS()
 | 
			
		||||
	filesystem := this.Environment.GetFS()
 | 
			
		||||
 | 
			
		||||
	// normalize path
 | 
			
		||||
	pat := req.URL.Path
 | 
			
		||||
@ -45,7 +45,7 @@ func (this *handler) ServeHTTP (res http.ResponseWriter, req *http.Request) {
 | 
			
		||||
	}
 | 
			
		||||
	if info.IsDir() {
 | 
			
		||||
		// try to find an index
 | 
			
		||||
		for _, base := range this.index {
 | 
			
		||||
		for _, base := range this.Index {
 | 
			
		||||
			currentPath := path.Join(pat, base)
 | 
			
		||||
			info, err := statFile(filesystem, pathToName(currentPath))
 | 
			
		||||
			if err != nil   { continue }
 | 
			
		||||
@ -54,7 +54,7 @@ func (this *handler) ServeHTTP (res http.ResponseWriter, req *http.Request) {
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if !this.directories {
 | 
			
		||||
		if !this.Directories {
 | 
			
		||||
			this.serveError(res, req, http.StatusForbidden, req.URL)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
@ -68,16 +68,16 @@ func (this *handler) ServeHTTP (res http.ResponseWriter, req *http.Request) {
 | 
			
		||||
	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)
 | 
			
		||||
	if !this.stepExt.Has(filepath.Ext(name)) {
 | 
			
		||||
	if !this.StepExt.Has(filepath.Ext(name)) {
 | 
			
		||||
		// just a normal file
 | 
			
		||||
		http.ServeFileFS(res, req, this.environment.GetFS(), name)
 | 
			
		||||
		http.ServeFileFS(res, req, this.Environment.GetFS(), name)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// parse
 | 
			
		||||
	document, err := this.environment.Parse(name)
 | 
			
		||||
	document, err := this.Environment.Parse(name)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		this.serveError(res, req, http.StatusInternalServerError, err)
 | 
			
		||||
		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.WriteHeader(status)
 | 
			
		||||
	// 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)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *handler) logErr (name string, err error) {
 | 
			
		||||
func (this *Handler) logErr (name string, err error) {
 | 
			
		||||
	log.Printf("ERR %s: %v\n", name, err)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user