From 38a025c0aa0bf76cde3e3cf08b63c2dd9d2ac127 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Geisendo=CC=88rfer?= Date: Sun, 17 Mar 2013 15:44:28 +0100 Subject: [PATCH] Some initial http code --- src/cmd/tusd/main.go | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/src/cmd/tusd/main.go b/src/cmd/tusd/main.go index 002a5c7..3d3e28f 100644 --- a/src/cmd/tusd/main.go +++ b/src/cmd/tusd/main.go @@ -1,5 +1,46 @@ package main +import ( + "fmt" + "net/http" + "log" +) + func main() { - println("hello world") + http.HandleFunc("/", route) + err := http.ListenAndServe(":1080", nil) + if err != nil { + log.Fatal("ListenAndServe: ", err) + } +} + +func route(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Server", "tusd") + + if r.Method == "POST" && r.URL.Path == "/files" { + createFile(w, r) + return + } + + reply(w, http.StatusNotFound, "No matching route") +} + +func reply(w http.ResponseWriter, code int, message string) { + w.WriteHeader(code) + fmt.Fprintf(w, "%d - %s: %s\n", code, http.StatusText(code), message) +} + +func createFile(w http.ResponseWriter, r *http.Request) { + contentRange := r.Header.Get("Content-Range") + if contentRange == "" { + reply(w, http.StatusBadRequest, "Content-Range header is required") + return + } + + contentType := r.Header.Get("Content-Type") + if contentType == "" { + contentType = "application/octet-stream" + } + + log.Printf("contentType: %s", contentType) }