forked from argylelabcoat/avatar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exampleHTTP.go
39 lines (29 loc) · 866 Bytes
/
exampleHTTP.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
package main
import (
"fmt"
"net/http"
"strings"
"github.com/elseano/avatar"
"github.com/go-chi/chi"
)
func main() {
r := chi.NewRouter()
// Get the png based on the initials, You would use it like this:
// <img src="http://localhost:3000/avatar/ae/png" width="150">
r.Get("/avatar/{initials}.png", func(w http.ResponseWriter, r *http.Request) {
initials := chi.URLParam(r, "initials")
avatar.ToHTTP(initials, w)
})
// Show a heap of avatars
r.Get("/showoff", func(w http.ResponseWriter, r *http.Request) {
abc := strings.Repeat("ABCDEFGHIJKLMNOPQRSTUVQXYZ", 10)
image := `<img src="http://localhost:3000/avatar/%s%s.png" width="100" height="100">`
html := "<body>"
for _, c := range abc {
html += fmt.Sprintf(image, string(c), string(c))
}
html += "</body>"
w.Write([]byte(html))
})
http.ListenAndServe(":3000", r)
}