Created
March 3, 2013 03:19
-
-
Save PaulCapestany/5074332 to your computer and use it in GitHub Desktop.
My static site/fileserver in Go.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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