-
Notifications
You must be signed in to change notification settings - Fork 0
/
youtube-gif-go.go
192 lines (156 loc) · 5.04 KB
/
youtube-gif-go.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/Stantheman/youtube-gif-go/api"
"github.com/Stantheman/youtube-gif-go/config"
"github.com/Stantheman/youtube-gif-go/logger"
"github.com/Stantheman/youtube-gif-go/redisPool"
"github.com/garyburd/redigo/redis"
"github.com/gorilla/mux"
"github.com/unrolled/render"
"net/http"
"os"
"strconv"
)
// http://codegangsta.gitbooks.io/building-web-apps-with-go/
var l = logger.Get()
var rend = render.New(render.Options{})
func main() {
conf := config.Get()
r := mux.NewRouter().StrictSlash(true)
r.HandleFunc("/", HomeHandler)
// Gifs collection
gifs := r.Path("/gifs").Subrouter()
gifs.Methods("GET").HandlerFunc(GifsIndexHandler)
gifs.Methods("POST").HandlerFunc(GifsCreateHandler)
// Gifs singular
gif := r.PathPrefix(`/gifs/{id:\d+}`).Subrouter()
gif.Methods("GET").HandlerFunc(GifShowHandler)
// Job singular
job := r.PathPrefix(`/jobs/{id:\d+}`).Subrouter()
job.Methods("GET").HandlerFunc(JobShowHandler)
connect := conf.Site.Ip + ":" + conf.Site.Port
fmt.Println("Starting server on " + connect)
http.ListenAndServe(connect, r)
}
func HomeHandler(rw http.ResponseWriter, r *http.Request) {
fmt.Fprintln(rw, "Home")
}
// GifsIndexHandler: ask redis for a list of completed images
func GifsIndexHandler(rw http.ResponseWriter, r *http.Request) {
c := redisPool.Pool.Get()
defer c.Close()
// cant tell if active image list is weird or redis
if exists, _ := redis.Int(c.Do("EXISTS", "active_images")); exists != 1 {
rend.JSON(rw, http.StatusNoContent, jsonErr(errors.New("no images")))
l.Notice("checking active_images: none yet")
return
}
images, err := redis.Strings(c.Do("LRANGE", "active_images", 0, -1))
if err != nil {
rend.JSON(rw, http.StatusInternalServerError, jsonErr(err))
l.Notice("getting images: " + err.Error())
return
}
jlist := make(map[string]string)
for _, image := range images {
jlist[image] = "/gifs/" + image
}
rend.JSON(rw, http.StatusOK, jlist)
}
/* GifsCreateHandler: take a URL over post and put it in a queue
Pass URL to this handler, it'll check that it looks like a URL
give a 202 ACK, and bounce*/
func GifsCreateHandler(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Access-Control-Allow-Origin", "*")
if err := r.ParseForm(); err != nil {
l.Notice("parsing form: " + err.Error())
rend.JSON(rw, http.StatusBadRequest, jsonErr(err))
return
}
msg, errs := api.ValidateParams(r.Form)
if len(errs) > 0 {
var probs []string = make([]string, 0)
for _, prob := range errs {
probs = append(probs, prob.Error())
}
rend.JSON(rw, http.StatusBadRequest, map[string][]string{"errors": probs})
return
}
id, err := addURLToRedis(msg)
if err != nil {
rend.JSON(rw, http.StatusInternalServerError, jsonErr(err))
return
}
// validate options, pass 202 Accepted
rw.Header().Set("Location", "/jobs/"+id)
rend.JSON(rw, http.StatusAccepted, map[string]string{"id": id})
}
// GifShowHandler: do we even want to hand back gifs? json?
func GifShowHandler(rw http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["id"]
c := redisPool.Pool.Get()
defer c.Close()
rw.Header().Set("Access-Control-Allow-Origin", "*")
// verify that the file is actually there
conf := config.Get()
path := conf.Site.Gif_Dir + "/" + id + ".gif"
if _, err := os.Stat(path); os.IsNotExist(err) {
rend.JSON(rw, http.StatusNotFound, jsonErr(errors.New("file doesn't exist")))
l.Err("checking on disk path: " + err.Error())
return
}
http.ServeFile(rw, r, path)
}
func JobShowHandler(rw http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["id"]
c := redisPool.Pool.Get()
defer c.Close()
rw.Header().Set("Access-Control-Allow-Origin", "*")
// freak if it ain't real
resp, err := redis.Strings(c.Do("HMGET", "gif:"+id, "status", "description"))
if err != nil {
rend.JSON(rw, http.StatusBadRequest, jsonErr(errors.New("gif doesn't exist")))
l.Notice("getting gif: " + err.Error())
return
}
rend.JSON(rw, http.StatusOK, map[string]string{"status": resp[0], "description": resp[1]})
}
func addURLToRedis(msg *api.PubSubMessage) (id string, err error) {
c := redisPool.Pool.Get()
defer c.Close()
// replace id increment with GUID?
new_id, err := redis.Int(c.Do("INCR", "autoincr:gif"))
if err != nil {
l.Err("getting new id: " + err.Error())
return "", err
}
msg.ID = strconv.Itoa(new_id)
keyname := "gif:" + msg.ID
// set up the json bomb for pubsub. grab pubsubmessage from worker
payload, err := json.Marshal(msg)
if err != nil {
l.Err("marshalling json: " + err.Error())
return "", err
}
// pipelined transaction city
c.Send("MULTI")
c.Send("HSET", keyname, "origin", msg.URL)
// enum/smarts for status/queue
c.Send("HSET", keyname, "status", "pending download")
// one shot, do not miss your chance to blow
c.Send("EXPIRE", keyname, 3600)
c.Send("PUBLISH", "download-queue", payload)
if _, err = c.Do("EXEC"); err != nil {
l.Err("adding image to redis: " + err.Error())
return "", err
}
return msg.ID, nil
}
func jsonErr(msg error) (res map[string]string) {
return map[string]string{
"error": msg.Error(),
}
}