Skip to content

Instantly share code, notes, and snippets.

@tanatana tanatana/uploader.go Secret
Last active Sep 20, 2018

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

commented Sep 20, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.