-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
202 lines (165 loc) · 4.54 KB
/
main.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
193
194
195
196
197
198
199
200
201
202
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"html/template"
"image"
"image/jpeg"
"io"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"strings"
"time"
"github.com/fogleman/gg"
"golang.org/x/image/font"
)
var (
fontSize float64 = 42
imageFont font.Face
)
var imageProviders = []string{
"https://placeimg.com/%d/%d/nature",
"http://lorempixel.com/%d/%d/nature/",
}
// Xingamento 0800
type Xingamento struct {
Value string `json:"xingamento"`
}
type slackResponse struct {
ResponseType string `json:"response_type"`
Text string `json:"text"`
}
// NewRandomXingamento 0800
func NewRandomXingamento() Xingamento {
return Xingamento{
Value: fmt.Sprintf("%s %s",
getRandomFromFile("data/subjects.txt"),
getRandomFromFile("data/predicates.txt"),
),
}
}
// NewRandomXingamentoImage 0800
func NewRandomXingamentoImage(client *http.Client, text string) io.Reader {
const width = 1024 / 2
const height = 768 / 2
image := getPlaceholder(client, width, height)
context := gg.NewContextForImage(image)
context.SetFontFace(imageFont)
context.SetRGB(0, 0, 0)
strokeSize := 3
for dy := -strokeSize; dy <= strokeSize; dy++ {
for dx := -strokeSize; dx <= strokeSize; dx++ {
if dx*dx+dy*dy >= strokeSize*strokeSize {
continue
}
x := width/2 + float64(dx)
y := height/2 + float64(dy)
context.DrawStringWrapped(text, x, y, 0.5, 0.5, width/2, 1.5, gg.AlignCenter)
}
}
context.SetRGB(1, 1, 1)
context.DrawStringWrapped(text, width/2, height/2, 0.5, 0.5, width/2, 1.5, gg.AlignCenter)
buff := new(bytes.Buffer)
imgOut := context.Image()
jpeg.Encode(buff, imgOut, &jpeg.Options{Quality: jpeg.DefaultQuality})
return buff
}
func getPlaceholder(client *http.Client, width, height int) image.Image {
return getPlaceholders(client, imageProviders[rand.Intn(len(imageProviders))], width, height)
}
func getPlaceholders(client *http.Client, url string, width, height int) image.Image {
placeholder := fmt.Sprintf(url, width, height)
requester := http.Get
if client != nil {
requester = client.Get
}
response, err := requester(placeholder)
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
var wallpaper image.Image
wallpaper, _, err = image.Decode(response.Body)
return wallpaper
}
func getRandomFromFile(file string) string {
rand.Seed(time.Now().UnixNano())
all, _ := ioutil.ReadFile(file)
list := bytes.Split(all, []byte("\n"))
return string(list[rand.Intn(len(list))])
}
func loadFontFace() {
// TODO: improve better loading fontface
var err error
imageFont, err = gg.LoadFontFace("fonts/Impact.ttf", fontSize)
if err != nil {
panic(err)
}
}
func apiHandler(w http.ResponseWriter, r *http.Request) {
xingamento, _ := json.Marshal(NewRandomXingamento())
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.Write(xingamento)
}
func slackHandler(w http.ResponseWriter, r *http.Request) {
xingamento := NewRandomXingamento()
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
json.NewEncoder(w).Encode(slackResponse{
ResponseType: "in_channel",
Text: xingamento.Value,
})
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
content, _ := ioutil.ReadFile("templates/index.html")
tpl, _ := template.New("index").Parse(string(content))
xingamento := NewRandomXingamento()
data := struct {
Value string
}{
Value: xingamento.Value,
}
tpl.Execute(w, data)
}
func imageHandler(w http.ResponseWriter, r *http.Request) {
client := &http.Client{}
hash := strings.TrimPrefix(r.URL.Path, "/image/")
text, err := base64.StdEncoding.DecodeString(hash)
if err != nil || len(text) == 0 {
http.Redirect(w, r, "/image", 301)
return
}
imageReader := NewRandomXingamentoImage(client, string(text))
w.Header().Set("Content-Type", "image/jpeg")
io.Copy(w, imageReader)
}
func randomImageHandler(w http.ResponseWriter, r *http.Request) {
xingamento := NewRandomXingamento()
text := strings.ToUpper(xingamento.Value)
hash := base64.StdEncoding.EncodeToString([]byte(text))
permURL := fmt.Sprintf("/image/%s", hash)
http.Redirect(w, r, permURL, 301)
}
func init() {
loadFontFace()
http.HandleFunc("/api", apiHandler)
http.HandleFunc("/slack", slackHandler)
http.HandleFunc("/image", randomImageHandler)
http.HandleFunc("/image/", imageHandler)
http.HandleFunc("/", indexHandler)
}
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8080"
log.Printf("Defaulting to port %s", port)
}
log.Printf("Listening on port %s", port)
if err := http.ListenAndServe(":"+port, nil); err != nil {
log.Fatal(err)
}
}