-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
84 lines (77 loc) · 1.87 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
identicon "github.com/StellarCN/stellar-identicon-go"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"image/png"
"strconv"
)
func IndexHandler(c *gin.Context) {
c.JSON(200, gin.H{
"_link": gin.H{
"avatar": gin.H{
"href": "/avatar/{account_id}/{?width,height}",
},
"self": gin.H{
"href": "/",
},
},
"stellar_identicon_api_source_code": "https://github.com/overcat/stellar-identicon-api",
"stellar_identicon_go_source_code": "https://github.com/overcat/stellar-identicon-go",
"sep_0033_file": "https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0033.md",
})
}
func NoRouterHandler(c *gin.Context) {
c.JSON(404, gin.H{
"code": "PAGE_NOT_FOUND",
"message": "Page not found",
})
}
func AvatarHandler(c *gin.Context) {
accountId := c.Param("account_id")
widthQuery := c.DefaultQuery("width", "")
heightQuery := c.DefaultQuery("height", "")
width := identicon.Width
if widthQuery != "" {
width_, err := strconv.Atoi(widthQuery)
if err != nil {
c.JSON(400, gin.H{
"code": "INVALID_WIDTH",
"message": "invalid width",
})
return
}
width = width_
}
height := identicon.Height
if heightQuery != "" {
height_, err := strconv.Atoi(heightQuery)
if err != nil {
c.JSON(400, gin.H{
"code": "INVALID_HEIGHT",
"message": "invalid height",
})
return
}
height = height_
}
img, err := identicon.Generate(accountId, width, height)
if err != nil {
c.JSON(400, gin.H{
"code": "INVALID_STELLAR_ID",
"message": "invalid stellar id",
})
return
}
c.Header("Content-Type", "image/png")
c.Header("Cache-Control", "public, max-age=86400")
png.Encode(c.Writer, img)
}
func main() {
r := gin.Default()
r.Use(cors.Default())
r.GET("/", IndexHandler)
r.GET("/avatar/:account_id", AvatarHandler)
r.NoRoute(NoRouterHandler)
r.Run()
}