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