-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
53 lines (46 loc) · 1.17 KB
/
handlers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"io/ioutil"
"net/http"
)
func showHomePage(c *gin.Context) {
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"title": "Home",
})
}
// The 'main' controller to fetch tags via clarifai and px500 clients
/* use dependency injection to pass in clarifai (and px for later) client in order to make
* the function more testable and modular;
* return gin.Handler to satisfy Gin's router method
*/
func fetchTags(c *gin.Context) {
body, err := ioutil.ReadAll(c.Request.Body)
if err != nil {
fmt.Printf("Error--read file: %s", err)
}
var request struct {
Image string `json:"image"`
}
err = json.Unmarshal(body, &request)
if err != nil {
fmt.Printf("Error--parse json: %s", err)
}
// TODO: somehow refresh token before it expires, without waiting for a request on an expired token
if !CLFclient.isAccessible() {
CLFclient.refreshToken()
}
clarifaiTags, err := CLFclient.getImageTags(request.Image)
if err != nil {
c.JSON(http.StatusBadRequest, err)
return
}
tags, err := getPxTags(clarifaiTags)
if err != nil {
c.JSON(http.StatusBadRequest, err)
return
}
c.JSON(http.StatusOK, tags)
}