~mendelmaleh/dummy

ref: d0dd20f1379d448e3306fac63d9f7d6030386b48 dummy/cmd/dummyserver/main.go -rw-r--r-- 5.6 KiB
d0dd20f1Mendel E Add tar endpoint 1 year, 2 months ago
                                                                                
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package main

import (
	"archive/tar"
	"archive/zip"
	"bytes"
	"fmt"
	"html/template"
	"image/png"
	"io"
	"io/ioutil"
	"log"
	"net"
	"net/http"
	"net/url"
	"path/filepath"
	"strings"

	"git.sr.ht/~mendelmaleh/dummy"
	"git.sr.ht/~mendelmaleh/freetype"
	"git.sr.ht/~mendelmaleh/freetype/truetype"
	"github.com/pelletier/go-toml"
	"golang.org/x/net/netutil"
)

type Config struct {
	Dummy struct {
		Conns                      int
		Addr, Pattern, Fonts, Font string
	}
}

var fonts = make(map[string]*truetype.Font)

func main() {
	doc, err := ioutil.ReadFile("config.toml")
	if err != nil {
		log.Fatal(err)
	}

	config := Config{}
	err = toml.Unmarshal(doc, &config)
	if err != nil {
		log.Fatal(err)
	}

	fontfiles, err := ioutil.ReadDir(config.Dummy.Fonts)
	if err != nil {
		log.Fatal(err)
	}

	for _, file := range fontfiles {
		if filepath.Ext(file.Name()) != ".ttf" {
			continue
		}

		filebytes, err := ioutil.ReadFile(config.Dummy.Fonts + file.Name())
		if err != nil {
			log.Println(err) // don't exit if one font doesn't work
			continue
		}

		font, err := freetype.ParseFont(filebytes)
		if err != nil {
			log.Println(err)
			continue
		}

		// fmt.Println(font.Name(truetype.NameIDFontFamily))
		fonts[font.Name(truetype.NameIDFontFamily)] = font
	}

	if len(fonts) < 1 {
		log.Fatal("No fonts loaded")
	}

	// set default font after it's loaded
	font, ok := fonts[config.Dummy.Font]
	if !ok {
		log.Fatal("Default font not found")
	}

	dopt := dummy.DefaultOptions
	dopt.Font = font

	tmpl := template.New("web").Funcs(template.FuncMap{"title": strings.Title})
	tmpl = template.Must(tmpl.Parse(`<!doctype html>
<html prefix="og: http://ogp.me/ns#">
<head>
  <title>{{ title .Name }}</title>
  <meta property="og:title" content="{{ title .Name }}" />
  <meta property="og:type" content="website" />
  <meta property="og:url" content={{ .Image }} />
  <meta property="og:image" content="{{ .Image }}" />
  <meta property="og:description" content="{{ .Desc }}" />
</head>`))

	http.HandleFunc(config.Dummy.Pattern,
		func(w http.ResponseWriter, r *http.Request) {
			Dummy(w, r, Options{dopt.Copy()})
		},
	)

	http.HandleFunc(config.Dummy.Pattern+"tar/",
		func(w http.ResponseWriter, r *http.Request) {
			Tar(w, r, Options{dopt.Copy()})
		},
	)

	http.HandleFunc(config.Dummy.Pattern+"zip/",
		func(w http.ResponseWriter, r *http.Request) {
			Zip(w, r, Options{dopt.Copy()})
		},
	)

	http.HandleFunc(config.Dummy.Pattern+"web/",
		func(w http.ResponseWriter, r *http.Request) {
			Web(w, r, Options{dopt.Copy()}, config, tmpl)
		},
	)

	listener, err := net.Listen("tcp", config.Dummy.Addr)
	if err != nil {
		log.Fatal(err)
	}

	listener = netutil.LimitListener(listener, config.Dummy.Conns)
	log.Panic(http.Serve(listener, nil))
}

func Dummy(w http.ResponseWriter, r *http.Request, opt Options) {
	err := r.ParseForm()
	if err != nil {
		fmt.Fprintln(w, err)
		return
	}

	err = opt.FromForm(r.Form)
	if err != nil {
		fmt.Fprintln(w, err)
		return
	}

	im := dummy.Image(opt.Options)
	png.Encode(w, im)

	// fmt.Fprintf(w, "%+v\n", dopt)
}

func Tar(w http.ResponseWriter, r *http.Request, opt Options) {
	err := r.ParseForm()
	if err != nil {
		fmt.Fprintln(w, err)
		return
	}

	err = opt.FromForm(r.Form)
	if err != nil {
		fmt.Fprintln(w, err)
		return
	}

	files := []struct {
		name string
		size int
	}{
		{"favicon-16x16", 16},
		{"favicon-32x32", 32},
		{"apple-touch-icon", 180},
		{"android-chrome-192x192", 192},
		{"android-chrome-512x512", 512},
	}

	w.Header().Set("Content-Disposition", "attachment; filename=\"favicons.tar\"")
	w.Header().Set("Content-Type", "application/tar")

	archive := tar.NewWriter(w)

	for _, f := range files {
		opt.Width, opt.Height = f.size, f.size
		opt.DPI = float64(f.size) / 128 * 72

		var buf bytes.Buffer
		png.Encode(&buf, dummy.Image(opt.Options))

		hdr := &tar.Header{
			Name: f.name,
			Mode: 0600,
			Size: int64(buf.Len()),
		}

		if err := archive.WriteHeader(hdr); err != nil {
			fmt.Fprintln(w, err)
			return
		}

		if _, err := io.Copy(archive, &buf); err != nil {
			fmt.Fprintln(w, err)
			return
		}
	}

	if err := archive.Close(); err != nil {
		fmt.Fprintln(w, err)
		return
	}
}

func Zip(w http.ResponseWriter, r *http.Request, opt Options) {
	err := r.ParseForm()
	if err != nil {
		fmt.Fprintln(w, err)
		return
	}

	err = opt.FromForm(r.Form)
	if err != nil {
		fmt.Fprintln(w, err)
		return
	}

	files := []struct {
		name string
		size int
	}{
		{"favicon-16x16", 16},
		{"favicon-32x32", 32},
		{"apple-touch-icon", 180},
		{"android-chrome-192x192", 192},
		{"android-chrome-512x512", 512},
	}

	w.Header().Set("Content-Disposition", "attachment; filename=\"favicons.zip\"")
	w.Header().Set("Content-Type", "application/zip")

	archive := zip.NewWriter(w)

	for _, f := range files {
		opt.Width, opt.Height = f.size, f.size
		opt.DPI = float64(f.size) / 128 * 72

		file, err := archive.Create(f.name + ".png")
		if err != nil {
			fmt.Fprintln(w, err)
			return
		}

		png.Encode(file, dummy.Image(opt.Options))
	}

	err = archive.Close()
	if err != nil {
		fmt.Fprintln(w, err)
		return
	}

}

func Web(w http.ResponseWriter, r *http.Request, opt Options, config Config, tmpl *template.Template) {
	err := r.ParseForm()
	if err != nil {
		fmt.Fprintln(w, err)
		return
	}

	err = opt.FromForm(r.Form)
	if err != nil {
		fmt.Fprintln(w, err)
		return
	}

	u := url.URL{
		Host:     r.Host,
		RawQuery: r.URL.RawQuery,
		Path:     config.Dummy.Pattern,
	}

	if v, ok := r.Header["X-Forwarded-Proto"]; ok {
		u.Scheme = strings.Join(v, "")
	} else {
		u.Scheme = "http"
	}

	tmpl.Execute(w, struct {
		Name, Desc string
		Image      template.URL
	}{"Dummy", opt.Desc, template.URL(u.String())})

}