You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
1.5 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "time"
  7. "github.com/gorilla/mux"
  8. )
  9. type ImageModel struct {
  10. File []byte `json:"file"`
  11. }
  12. type Route struct {
  13. Name string
  14. Method string
  15. Pattern string
  16. HandlerFunc http.HandlerFunc
  17. }
  18. type Routes []Route
  19. var routes = Routes{
  20. Route{
  21. "Index",
  22. "GET",
  23. "/",
  24. Index,
  25. },
  26. Route{
  27. "NewImage",
  28. "POST",
  29. "/image",
  30. NewImage,
  31. },
  32. }
  33. func Logger(inner http.Handler, name string) http.Handler {
  34. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  35. start := time.Now()
  36. inner.ServeHTTP(w, r)
  37. log.Printf(
  38. "%s\t%s\t%s\t%s",
  39. r.Method,
  40. r.RequestURI,
  41. name,
  42. time.Since(start),
  43. )
  44. })
  45. }
  46. func NewRouter() *mux.Router {
  47. router := mux.NewRouter().StrictSlash(true)
  48. for _, route := range routes {
  49. var handler http.Handler
  50. handler = route.HandlerFunc
  51. handler = Logger(handler, route.Name)
  52. router.
  53. Methods(route.Method).
  54. Path(route.Pattern).
  55. Name(route.Name).
  56. Handler(handler)
  57. }
  58. return router
  59. }
  60. func Index(w http.ResponseWriter, r *http.Request) {
  61. fmt.Fprintln(w, "send images to the /image path")
  62. }
  63. func NewImage(w http.ResponseWriter, r *http.Request) {
  64. _, handler, err := r.FormFile("file")
  65. check(err)
  66. //imageName := strings.Split(handler.Filename, ".")[0]
  67. //fileName := imageName + ".png"
  68. //data, err := ioutil.ReadAll(file)
  69. //check(err)
  70. img := readImage(handler.Filename)
  71. result := knn(dataset, img)
  72. fmt.Println("seems to be a " + result)
  73. fmt.Fprintln(w, "seems to be a "+result)
  74. }