Created
January 21, 2015 14:20
-
-
Save tomnomnom/fbbd0641e2db30616967 to your computer and use it in GitHub Desktop.
lolstore
This file contains 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 ( | |
"fmt" | |
"io" | |
"log" | |
"net/http" | |
"os" | |
) | |
const dataDir = "./data" | |
func storeHandler(w http.ResponseWriter, r *http.Request) { | |
path := dataDir + r.RequestURI | |
switch r.Method { | |
case "GET": | |
http.ServeFile(w, r, path) | |
case "POST": | |
f, _ := os.Create(path) | |
io.Copy(f, r.Body) | |
default: | |
w.WriteHeader(http.StatusMethodNotAllowed) | |
fmt.Fprintf(w, "I'm afraid I can't do that.\n") | |
} | |
} | |
func main() { | |
http.HandleFunc("/", storeHandler) | |
http.ListenAndServe(":8080", nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment