Skip to content

Instantly share code, notes, and snippets.

@wader
Created May 11, 2017 19:31
Show Gist options
  • Save wader/ba95dc9ab0779f8d990ce5f4f9c3795d to your computer and use it in GitHub Desktop.
Save wader/ba95dc9ab0779f8d990ce5f4f9c3795d to your computer and use it in GitHub Desktop.
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