source: code/trunk/serve.go@ 78

Last change on this file since 78 was 78, checked in by Izuru Yakumo, 18 months ago

Just a documentation update

Signed-off-by: Izuru Yakumo <yakumo.izuru@…>

File size: 971 bytes
RevLine 
[73]1// Taken from https://github.com/fogleman/serve and repurposed as a library
2package aya
3
4import (
5 "fmt"
6 "log"
7 "net/http"
8)
9
10// ResponseWriter wraps http.ResponseWriter to capture the HTTP status code
11type ResponseWriter struct {
12 http.ResponseWriter
13 StatusCode int
14}
15
16func (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
22type Handler struct {
23 http.Handler
24}
25
26func (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
[78]32// This function is called by the `aya serve` subcommand
33func HttpServe(Dir string, Port int) {
[73]34 handler := &Handler{http.FileServer(http.Dir(Dir))}
35 http.Handle("/", handler)
[78]36 addr := fmt.Sprintf(":%d", Port)
[73]37 log.Printf("Listening on %s\n", addr)
38 log.Fatal(http.ListenAndServe(addr, nil))
39}
Note: See TracBrowser for help on using the repository browser.