예제 #1
0
파일: serve.go 프로젝트: kellegous/grund
func setup(data string, r pork.Router) {
	r.HandleFunc("/save", func(w http.ResponseWriter, r *http.Request) {
		if r.Method != "POST" {
			http.Error(w, "Invalid", http.StatusMethodNotAllowed)
			return
		}

		var req struct {
			Path string
			Data string
		}

		if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
			panic(err)
		}

		b, err := base64.StdEncoding.DecodeString(req.Data)
		if err != nil {
			panic(err)
		}

		if err := ioutil.WriteFile(filepath.Join(data, req.Path), b, os.ModePerm); err != nil {
			panic(err)
		}

		res := map[string]interface{}{
			"Error": nil,
		}
		w.Header().Set("Content-Type", "application/json")
		if err := json.NewEncoder(w).Encode(res); err != nil {
			panic(err)
		}
	})
}