aboutsummaryrefslogtreecommitdiffstats
path: root/main.go
blob: c9aebcfeb17aac9437d55a5df5688ce91494421a (plain)
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
package main

import (
	"net/http"
	"unicode/utf8"

	"github.com/inconshreveable/log15"
)

func main() {
	http.HandleFunc("/gen", gen)

	err := http.ListenAndServe(":8080", nil)
	if err != nil {
		log15.Error("serving http requests", "error", err)
	}
}

func gen(w http.ResponseWriter, r *http.Request) {
	log15.Info("serving request")

	err := r.ParseForm()
	if err != nil {
		log15.Error("parsing form in request", "error", err)
	}

	val := r.FormValue("query")

	if !utf8.ValidString(val) {
		log15.Error("non utf-8 encoded string", "key", "query", "val", val)
		w.Write([]byte("non utf-8 encoded string"))
		return
	}

	asciiString := ASCIIRuneArray([]rune(val))

	log15.Info("writing result")
	w.Write([]byte("<html><body style='text-align:center;background:#222;color:#ccc;'><pre><code>" + asciiString + "</code></pre></body></html>"))
}