Skip to content

Instantly share code, notes, and snippets.

@PaulCapestany
Created March 3, 2013 03:19
Show Gist options
  • Select an option

  • Save PaulCapestany/5074332 to your computer and use it in GitHub Desktop.

Select an option

Save PaulCapestany/5074332 to your computer and use it in GitHub Desktop.
My static site/fileserver in Go.
package main
import (
"log"
"net/http"
"os"
)
var Home = os.Getenv("HOME")
var RequestsLogPath = Home + "/requests_log"
var RequestsLog, _ = os.OpenFile(RequestsLogPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0600)
func main() {
log.SetOutput(RequestsLog)
http.HandleFunc("/", handler)
http.ListenAndServe(":8285", nil)
RequestsLog.Close()
}
func handler(w http.ResponseWriter, r *http.Request) {
filename := "index.html"
if len(r.URL.Path) > 1 {
filename = r.URL.Path[1:]
}
filename = Home + "/paulcapestany.com/" + filename
log.Printf("%s %s %s", r.RemoteAddr, r.Method, r.URL)
http.ServeFile(w, r, filename)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment