fork download
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "image"
  6. _ "image/jpeg"
  7. _ "image/png"
  8. "io"
  9. "log"
  10. "os"
  11.  
  12. "github.com/labstack/echo/v4"
  13. "github.com/labstack/echo/v4/middleware"
  14. )
  15.  
  16. // Send an image to microservice for image analysis
  17. func ImageRequestHandler(c echo.Context, img *os.File) error {
  18. // TODO: Use user image instead
  19. log.Print(img.Name())
  20.  
  21. // Get filesize of image
  22. stat, err := img.Stat()
  23. log.Print("File size: ", stat.Size()/1024, " kb")
  24.  
  25. // Get width and height in px for the image
  26. im, _, err := image.DecodeConfig(img)
  27. if err != nil {
  28. fmt.Fprintf(os.Stderr, "%s: %v\n", img.Name(), err)
  29. }
  30. log.Print(im.Width)
  31.  
  32. // Form JSON response that contains name, size, height and width
  33. jsonStr := fmt.Sprintf(`{ "name": "%s",
  34. "size": %d,
  35. "width": %d,
  36. "height": %d
  37. }`, img.Name(), stat.Size(), im.Width, im.Height)
  38. log.Print(jsonStr)
  39.  
  40. c.Response().Header().Set("Content-Type", "application/json")
  41. //TODO: Avoid double casting []byte string?!
  42. c.Response().Write([]byte(string(jsonStr)))
  43.  
  44. // TODO: Figure out how to avoid return and just return with c.Response() similar to w.Write
  45. return err
  46. }
  47.  
  48. func main() {
  49. e := echo.New()
  50.  
  51. // Avoid CORS error when testing with redocly
  52. e.Use(middleware.CORS())
  53.  
  54. // API Versioning
  55. apiGroup := e.Group("/api")
  56. apiv1Group := apiGroup.Group("/v1")
  57.  
  58. apiv1Group.POST("/imageanalysis", func(c echo.Context) error {
  59. // TODO: Catch error when no file is send in POST Request
  60. // Source
  61. file, err := c.FormFile("file")
  62. if err != nil {
  63. return err
  64. }
  65. src, err := file.Open()
  66. if err != nil {
  67. return err
  68. }
  69. defer src.Close()
  70.  
  71. // Destination
  72. dst, err := os.Create(file.Filename)
  73. if err != nil {
  74. return err
  75. }
  76. defer dst.Close()
  77.  
  78. // Copy
  79. if _, err = io.Copy(dst, src); err != nil {
  80. return err
  81. }
  82. // TODO: Why do i need to open the file here again, instead of just passing the reference from os.Create()?
  83. // TODO: Maybe don't create the file on disk at all, just send it from memory
  84. buf, err := os.Open(dst.Name())
  85. if err != nil {
  86. log.Fatal(err)
  87. }
  88. defer buf.Close()
  89.  
  90. return ImageRequestHandler(c, buf)
  91. })
  92. e.Logger.Fatal(e.Start(":1324"))
  93. }
  94.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.go:12:2: cannot find package "github.com/labstack/echo/v4" in any of:
	/opt/go/src/github.com/labstack/echo/v4 (from $GOROOT)
	/home/OYA5DQ/go/src/github.com/labstack/echo/v4 (from $GOPATH)
prog.go:13:2: cannot find package "github.com/labstack/echo/v4/middleware" in any of:
	/opt/go/src/github.com/labstack/echo/v4/middleware (from $GOROOT)
	/home/OYA5DQ/go/src/github.com/labstack/echo/v4/middleware (from $GOPATH)
stdout
Standard output is empty