Skip to content

Instantly share code, notes, and snippets.

@tanatana
Last active September 20, 2018 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanatana/5996fb3d1110ad788f8ceea4f6b7bd62 to your computer and use it in GitHub Desktop.
Save tanatana/5996fb3d1110ad788f8ceea4f6b7bd62 to your computer and use it in GitHub Desktop.
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)
}
@tanatana
Copy link
Author

images ディレクトリ無いとエラー出ますのでよろしくお願いします.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment