-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
65 lines (55 loc) · 1.25 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
// main.go
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
"time"
"github.com/gabrielbarker/identico/identicon"
)
func welcome(w http.ResponseWriter, r *http.Request) {
message := getMessageParameter(w, r)
quadrantSize := getQuadrantSizeParameter(r)
id := identicon.QuadrantIdenticon{}
s := id.Create(message, quadrantSize)
w.Header().Set("Content-Type", "image/svg+xml")
if message != "" {
}
io.WriteString(w, s.ToString())
}
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "5000"
}
http.HandleFunc("/", welcome)
http.ListenAndServe(":"+port, nil)
}
func getMessageParameter(w http.ResponseWriter, r *http.Request) string {
message := getURLParameter("message", "", r)
if message == "" {
w.Header().Set("Cache-Control", "no-store")
return fmt.Sprintf("%v", time.Now().UnixNano())
}
return message
}
func getQuadrantSizeParameter(r *http.Request) int {
size, err := strconv.Atoi(getURLParameter("quadrant", "8", r))
if err != nil {
log.Fatal(err)
}
if size > 128 {
size = 128
}
return size
}
func getURLParameter(parameter string, defaultValue string, r *http.Request) string {
params, ok := r.URL.Query()[parameter]
if !ok || len(params[0]) < 1 {
return defaultValue
}
return params[0]
}