-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
142 lines (110 loc) · 3.04 KB
/
app.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
package main
import (
"errors"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"time"
"github.com/jinzhu/gorm"
"github.com/labstack/echo"
mw "github.com/labstack/echo/middleware"
"github.com/uhuraapp/uhura-images/database"
"willnorris.com/go/imageproxy"
)
var DB gorm.DB
func cors(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Access-Control-Allow-Origin", "*")
rw.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
rw.Header().Set("Access-Control-Allow-Credentials", "true")
rw.Header().Set("Access-Control-Allow-Methods", "DELETE, OPTIONS, GET, POST, PUT")
rw.Header().Set("Access-Control-Expose-Headers", "Content-Length")
}
func main() {
DB = database.NewPostgresql()
e := echo.New()
e.Use(mw.Logger)
e.Use(cors)
e.Options("/*", func(*echo.Context) error {
return nil
})
e.Get("/cache/:id", get)
e.Get("/resolve", resolve)
e.Run(":" + os.Getenv("PORT"))
}
func get(c *echo.Context) error {
var image database.Image
err := DB.Table("images").Where("id = ?", c.P(0)).Find(&image).Error
if err != nil {
return c.NoContent(http.StatusNotFound)
}
c.Response.Header().Add("Cache-Control", "public, max-age=31536000")
c.Response.Header().Add("Last-Modified", image.UpdatedAt.Format(time.RFC1123))
c.Response.Header().Add("Expires", "Mon, 25 Jun 2030 21:31:12 GMT")
if ifModifiedSince := c.Request.Header.Get("If-Modified-Since"); ifModifiedSince != "" {
ifModifiedSinceTime, err := time.Parse(time.RFC1123, ifModifiedSince)
updatedAt, _ := time.Parse(time.RFC1123, image.UpdatedAt.Format(time.RFC1123))
if err == nil && ifModifiedSinceTime.Sub(updatedAt) < 1 {
c.NoContent(304)
return nil
}
}
c.Response.Write(image.Data)
return nil
}
func resolve(c *echo.Context) error {
var id int64
var ids []int64
url := c.Request.URL.Query().Get("url")
err := DB.Select("id").Table("images").Where("url = ?", url).Pluck("id", &ids).Error
log.Println(ids, err)
if len(ids) == 0 {
requestedImage, requestErr := requestURL(url)
if requestErr != nil {
return c.NoContent(http.StatusNotFound)
}
image, err := save(url, requestedImage)
if err != nil {
c.Response.Write(requestedImage)
return nil
}
id = image.Id
} else {
id = ids[0]
}
idS := strconv.Itoa(int(id))
c.Redirect(http.StatusMovedPermanently, "/cache/"+idS)
return nil
}
func requestURL(url string) ([]byte, error) {
response, err := http.Get(url)
if err != nil {
return nil, err
}
defer response.Body.Close()
if response.StatusCode != 200 {
return nil, errors.New("Status is " + response.Status)
}
body, err := ioutil.ReadAll(response.Body)
return body, err
}
func save(url string, _image []byte) (*database.Image, error) {
image, err := imageproxy.Transform(_image, resizeOptions())
if err != nil {
return nil, err
}
model := database.Image{
Url: url,
Data: image,
}
log.Println("Saving image")
err = DB.Table("images").Save(&model).Error
if err != nil {
return nil, err
}
return &model, err
}
func resizeOptions() imageproxy.Options {
return imageproxy.ParseOptions("250x")
}