/
main.go
165 lines (135 loc) · 3.08 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
package main
import (
"encoding/hex"
"fmt"
"go/build"
"html/template"
"image"
"image/color"
"image/draw"
"image/png"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/gorilla/mux"
)
const (
defaultWidth = 300
defaultFg = "ffffff"
defaultBg = "969696"
appName = "go-imgplaceholder"
version = "0.1"
)
var (
templates *template.Template
)
type canvas struct {
img *image.RGBA
w int
h int
fg color.RGBA
bg color.RGBA
}
type data struct {
Title string
Req *http.Request
}
func (c *canvas) fillBackground() {
draw.Draw(c.img, c.img.Bounds(), &image.Uniform{c.bg}, image.ZP, draw.Src)
}
func (c *canvas) fillString(s string) {
drawString(c, s)
}
func init() {
homeHTMLPath := "home.html"
if pkg, err := build.Import("github.com/gedex/go-imgplaceholder", "", build.FindOnly); err == nil {
p := filepath.Join(pkg.Dir, homeHTMLPath)
if _, err := os.Stat(p); err == nil {
homeHTMLPath = p
}
}
fmt.Println(homeHTMLPath)
templates = template.Must(template.ParseFiles(homeHTMLPath))
}
func main() {
r := mux.NewRouter()
// Fixes for no trailing slash
r.HandleFunc("/{width:[0-9]+}", imgHandler)
r.HandleFunc("/{width:[0-9]+}x{height:[0-9]+}", imgHandler)
// Subrouter with image's width specified
s1 := r.PathPrefix("/{width:[0-9]+}").Subrouter()
subroute(s1)
// Subrouter image's width x height specified
s2 := r.PathPrefix("/{width:[0-9]+}x{height:[0-9]+}").Subrouter()
subroute(s2)
// Home
r.HandleFunc("/", home)
http.Handle("/", r)
http.ListenAndServe(":8080", nil)
}
func subroute(s *mux.Router) {
s.HandleFunc("/", imgHandler)
s.HandleFunc("/{bg:[a-fA-F0-9]+}", imgHandler)
s.HandleFunc("/{bg:[a-fA-F0-9]+}/{fg:[a-fA-F0-9]+}", imgHandler)
}
func home(w http.ResponseWriter, r *http.Request) {
d := &data{
Title: fmt.Sprintf("%s/%s", appName, version),
Req: r,
}
if err := templates.ExecuteTemplate(w, "home.html", d); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func imgHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
var val, fgs, bgs string
var ok bool
width := defaultWidth
if val, ok = vars["width"]; ok {
width, _ = strconv.Atoi(val)
}
height := width
if val, ok = vars["height"]; ok {
height, _ = strconv.Atoi(val)
}
if fgs, ok = vars["fg"]; !ok {
fgs = defaultFg
}
if bgs, ok = vars["bg"]; !ok {
bgs = defaultBg
}
c := &canvas{
img: image.NewRGBA(image.Rect(0, 0, width, height)),
w: width,
h: height,
fg: stringToColor(fgs),
bg: stringToColor(bgs),
}
c.fillBackground()
c.fillString(fmt.Sprintf("%dx%d", width, height))
err := png.Encode(w, c.img)
if err != nil {
errStr := fmt.Sprintf("Error encoding: %v", err)
http.Error(w, errStr, http.StatusInternalServerError)
}
w.Header().Set("Content-Type", "image/png")
}
func stringToColor(s string) color.RGBA {
switch len(s) {
case 1:
s = strings.Repeat(s, 6)
case 2:
s = strings.Repeat(s, 3)
case 3:
s = strings.Repeat(s, 2)
case 4:
fallthrough
case 5:
s = s[:4] + "00"
}
h, _ := hex.DecodeString(s)
return color.RGBA{h[0], h[1], h[2], 255}
}