/[aya]/serve.go
ViewVC logotype

Contents of /serve.go

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1 - (show annotations)
Mon Sep 30 00:42:05 2024 UTC (6 weeks, 4 days ago) by yakumo_izuru
Branch: MAIN
CVS Tags: HEAD
Error occurred while calculating annotation data.
Mirrored from https://git.chaotic.ninja/git/yakumo_izuru/aya

1 // Taken from https://github.com/fogleman/serve and repurposed as a library
2 package aya
3
4 import (
5 "fmt"
6 "log"
7 "net/http"
8 )
9
10 // ResponseWriter wraps http.ResponseWriter to capture the HTTP status code
11 type ResponseWriter struct {
12 http.ResponseWriter
13 StatusCode int
14 }
15
16 func (w *ResponseWriter) WriteHeader(statusCode int) {
17 w.StatusCode = statusCode
18 w.ResponseWriter.WriteHeader(statusCode)
19 }
20
21 // Handler wraps http.Handler to log served files
22 type Handler struct {
23 http.Handler
24 }
25
26 func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
27 rw := &ResponseWriter{w, 0}
28 h.Handler.ServeHTTP(rw, r)
29 log.Println(r.RemoteAddr, r.Method, rw.StatusCode, r.URL)
30 }
31
32 // This function is called by the `aya serve` subcommand
33 func HttpServe(Dir string, Port int) {
34 handler := &Handler{http.FileServer(http.Dir(Dir))}
35 http.Handle("/", handler)
36 addr := fmt.Sprintf("127.0.0.1:%d", Port)
37 log.Printf("[aya.HttpServe] Listening on %s\n", addr)
38 log.Fatal(http.ListenAndServe(addr, nil))
39 }

nishi@chaotic.ninja
ViewVC Help
Powered by ViewVC 1.3.0-dev