~mendelmaleh/dummy

ref: a396b155400c425ab99e147f2a76023e0d78221b dummy/cmd/dummyserver/main.go -rw-r--r-- 3.1 KiB
a396b155Mendel E Rename main cmd to dummyserver 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
package main

import (
	"fmt"
	"html/template"
	"image/png"
	"io/ioutil"
	"log"
	"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"
)

type Config struct {
	Dummy struct {
		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, dopt.Copy())
		},
	)

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

	log.Panic(http.ListenAndServe(config.Dummy.Addr, nil))
}

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

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

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

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

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

	opt, err = FromForm(opt, 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())})

}