コードを借りる
Signed-off-by: Izuru Yakumo <yakumo.izuru@chaotic.ninja> git-svn-id: file:///srv/svn/repo/aya/trunk@73 cec141ff-132a-4243-88a5-ce187bd62f94
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
// $TheSupernovaDuo: marisa.chaotic.ninja/aya/cmd/aya, v 1.0 2023-10-03 02:34:17, yakumo_izuru Exp $
|
// $TheSupernovaDuo: marisa.chaotic.ninja/aya/cmd/aya, v0.6.4 2023-10-23 22:26:23, yakumo_izuru Exp $
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -6,7 +6,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -369,14 +368,6 @@ func buildAll(watch bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Serve the public directory over HTTP
|
|
||||||
func serve() {
|
|
||||||
root := http.Dir(PUBDIR)
|
|
||||||
http.Handle("/", http.FileServer(root))
|
|
||||||
log.Printf("Serving the %s directory over http://localhost:8000. Interrupt with ^C.\n", PUBDIR)
|
|
||||||
log.Fatal(http.ListenAndServe(":8000", nil))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize the environment
|
// Initialize the environment
|
||||||
func init() {
|
func init() {
|
||||||
// prepend .aya to $PATH, so plugins will be found before OS commands
|
// prepend .aya to $PATH, so plugins will be found before OS commands
|
||||||
@@ -422,7 +413,7 @@ func main() {
|
|||||||
case "help":
|
case "help":
|
||||||
printUsage()
|
printUsage()
|
||||||
case "serve":
|
case "serve":
|
||||||
serve()
|
aya.HttpServe(PUBDIR)
|
||||||
case "var":
|
case "var":
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
log.Fatal("var: filename expected")
|
log.Fatal("var: filename expected")
|
||||||
|
|||||||
38
serve.go
Normal file
38
serve.go
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
// Taken from https://github.com/fogleman/serve and repurposed as a library
|
||||||
|
package aya
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ResponseWriter wraps http.ResponseWriter to capture the HTTP status code
|
||||||
|
type ResponseWriter struct {
|
||||||
|
http.ResponseWriter
|
||||||
|
StatusCode int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *ResponseWriter) WriteHeader(statusCode int) {
|
||||||
|
w.StatusCode = statusCode
|
||||||
|
w.ResponseWriter.WriteHeader(statusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handler wraps http.Handler to log served files
|
||||||
|
type Handler struct {
|
||||||
|
http.Handler
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
rw := &ResponseWriter{w, 0}
|
||||||
|
h.Handler.ServeHTTP(rw, r)
|
||||||
|
log.Println(r.RemoteAddr, r.Method, rw.StatusCode, r.URL)
|
||||||
|
}
|
||||||
|
|
||||||
|
func HttpServe(Dir string) {
|
||||||
|
handler := &Handler{http.FileServer(http.Dir(Dir))}
|
||||||
|
http.Handle("/", handler)
|
||||||
|
addr := fmt.Sprintf(":%d", 8000)
|
||||||
|
log.Printf("Listening on %s\n", addr)
|
||||||
|
log.Fatal(http.ListenAndServe(addr, nil))
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user