source: code/trunk/serve.go@ 91

Last change on this file since 91 was 89, checked in by Izuru Yakumo, 7 weeks ago

風神「嵐の日」

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