|
|
package main
import ( "fmt" "log" "net/http" "time"
"github.com/gorilla/mux" )
type ImageModel struct { File []byte `json:"file"` }
type Route struct { Name string Method string Pattern string HandlerFunc http.HandlerFunc } type Routes []Route
var routes = Routes{ Route{ "Index", "GET", "/", Index, }, Route{ "NewImage", "POST", "/image", NewImage, }, }
func Logger(inner http.Handler, name string) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { start := time.Now()
inner.ServeHTTP(w, r)
log.Printf( "%s\t%s\t%s\t%s", r.Method, r.RequestURI, name, time.Since(start), ) }) } func NewRouter() *mux.Router { router := mux.NewRouter().StrictSlash(true) for _, route := range routes { var handler http.Handler handler = route.HandlerFunc handler = Logger(handler, route.Name)
router. Methods(route.Method). Path(route.Pattern). Name(route.Name). Handler(handler) } return router }
func Index(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "send images to the /image path") } func NewImage(w http.ResponseWriter, r *http.Request) { _, handler, err := r.FormFile("file") check(err)
//imageName := strings.Split(handler.Filename, ".")[0]
//fileName := imageName + ".png"
//data, err := ioutil.ReadAll(file)
//check(err)
img := readImage(handler.Filename) result := knn(dataset, img)
fmt.Println("seems to be a " + result) fmt.Fprintln(w, "seems to be a "+result) }
|