Created
May 11, 2017 19:31
-
-
Save wader/ba95dc9ab0779f8d990ce5f4f9c3795d to your computer and use it in GitHub Desktop.
This file contains hidden or 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 ( | |
| "crypto/rand" | |
| "encoding/hex" | |
| "encoding/json" | |
| "fmt" | |
| "html" | |
| "log" | |
| "net/http" | |
| "os" | |
| ) | |
| type Upload struct { | |
| IV string `json:"iv"` | |
| Chipher string `json:"cipher"` | |
| Data string `json:"data"` | |
| } | |
| func main() { | |
| http.HandleFunc("/foo", func(w http.ResponseWriter, r *http.Request) { | |
| upload := &Upload{} | |
| uploadDecoder := json.NewDecoder(r.Body) | |
| if err := uploadDecoder.Decode(upload); err != nil { | |
| http.Error(w, "failed to parse", http.StatusBadRequest) | |
| return | |
| } | |
| fmt.Printf("%#v\n", upload) | |
| randBytes := make([]byte, 32) | |
| rand.Read(randBytes) | |
| randString := hex.EncodeToString(randBytes) | |
| fmt.Printf("%v\n", randString) | |
| f, fErr := os.OpenFile(randString, os.O_RDWR|os.O_CREATE, 0600) | |
| if fErr != nil { | |
| http.Error(w, "failed to parse", http.StatusBadRequest) | |
| return | |
| } | |
| defer f.Close() | |
| uploadCoder := json.NewEncoder(f) | |
| uploadCoder.Encode(upload) | |
| }) | |
| http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) { | |
| fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path)) | |
| }) | |
| log.Fatal(http.ListenAndServe(":8080", http.DefaultServeMux)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment