-
-
Save tanatana/5996fb3d1110ad788f8ceea4f6b7bd62 to your computer and use it in GitHub Desktop.
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/ioutil" | |
"net/http" | |
"net/http/httputil" | |
"os" | |
"time" | |
) | |
func errorResponse(w http.ResponseWriter, err error) { | |
w.WriteHeader(500) | |
fmt.Fprintf(w, err.Error()) | |
fmt.Printf("500 %s %s\n", time.Now(), err.Error()) | |
} | |
func main() { | |
mux := http.NewServeMux() | |
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | |
reqdump, err := httputil.DumpRequest(r, false) | |
if err != nil { | |
errorResponse(w, err) | |
return | |
} | |
fmt.Printf("%s\n", reqdump) | |
buf, err := ioutil.ReadAll(r.Body) | |
defer r.Body.Close() | |
if err != nil { | |
errorResponse(w, err) | |
return | |
} | |
now := time.Now() | |
filepath := fmt.Sprintf("./images/%d.png", now.Unix()) | |
file, err := os.Create(filepath) | |
if err != nil { | |
errorResponse(w, err) | |
return | |
} | |
_, err = file.Write(buf) | |
if err != nil { | |
errorResponse(w, err) | |
return | |
} | |
err = file.Close() | |
if err != nil { | |
errorResponse(w, err) | |
return | |
} | |
fmt.Printf("200 %s\n", time.Now()) | |
fmt.Fprintf(w, filepath) | |
}) | |
fmt.Println("server start on localhost:8080") | |
http.ListenAndServe(":8080", mux) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
images ディレクトリ無いとエラー出ますのでよろしくお願いします.