package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"io/ioutil"
|
|
"net/http"
|
|
|
|
"github.com/gorilla/mux"
|
|
)
|
|
|
|
func Index(w http.ResponseWriter, r *http.Request) {
|
|
fmt.Fprintln(w, "Welcome! To send images, go to /image")
|
|
}
|
|
|
|
func ImageShow(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
imageName := vars["imageName"]
|
|
fmt.Fprintln(w, "Image show:", imageName)
|
|
}
|
|
|
|
func NewImage(w http.ResponseWriter, r *http.Request) {
|
|
var img ImageModel
|
|
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
if err := r.Body.Close(); err != nil {
|
|
panic(err)
|
|
}
|
|
fmt.Println(string(body))
|
|
if err := json.Unmarshal(body, &img); err != nil {
|
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
|
w.WriteHeader(422) // unprocessable entity
|
|
if err := json.NewEncoder(w).Encode(err); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
img.File = body
|
|
fmt.Println(img)
|
|
fmt.Println(img.File)
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
|
w.WriteHeader(http.StatusCreated)
|
|
if err := json.NewEncoder(w).Encode(img.File); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|