-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathserver.go
96 lines (87 loc) · 2.45 KB
/
server.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"encoding/json"
"net/http"
"net/url"
"path/filepath"
"github.com/machinebox/sdk-go/tagbox"
"github.com/matryer/way"
)
// Server is the app server.
type Server struct {
assets string
tagbox *tagbox.Client
items map[string]Item
router *way.Router
}
// NewServer makes a new Server.
func NewServer(assets string, tagbox *tagbox.Client, items map[string]Item) *Server {
srv := &Server{
assets: assets,
tagbox: tagbox,
items: items,
router: way.NewRouter(),
}
srv.router.Handle(http.MethodGet, "/assets/", Static("/assets/", assets))
srv.router.HandleFunc(http.MethodGet, "/api/random-images", srv.handleRandomImages)
srv.router.HandleFunc(http.MethodGet, "/api/similar-images", srv.handleSimilarImages)
srv.router.HandleFunc(http.MethodGet, "/", srv.handleIndex)
return srv
}
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.router.ServeHTTP(w, r)
}
func (s *Server) handleIndex(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filepath.Join(s.assets, "index.html"))
}
func (s *Server) handleSimilarImages(w http.ResponseWriter, r *http.Request) {
urlStr := r.URL.Query().Get("url")
u, err := url.Parse(urlStr)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
tags, err := s.tagbox.SimilarURL(u)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var res struct {
Items []Item `json:"items"`
}
for _, tag := range tags {
item := s.items[tag.ID]
item.Confidence = tag.Confidence
res.Items = append(res.Items, item)
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
if err := json.NewEncoder(w).Encode(res); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func (s *Server) handleRandomImages(w http.ResponseWriter, r *http.Request) {
var res struct {
Items []Item `json:"items"`
}
var count int
for _, v := range s.items {
res.Items = append(res.Items, v)
count++
if count == 20 {
break
}
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
if err := json.NewEncoder(w).Encode(res); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
// Static gets a static file server for the specified path.
func Static(stripPrefix, dir string) http.Handler {
h := http.StripPrefix(stripPrefix, http.FileServer(http.Dir(dir)))
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h.ServeHTTP(w, r)
})
}