]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git/blame - api/routes.go
poloniex balance draft
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git] / api / routes.go
CommitLineData
7a9e5112 1package api
2
3import (
4 "encoding/base64"
5
6 "github.com/gin-gonic/gin"
7)
8
9type Route struct {
10 Method string
11 Handlers []gin.HandlerFunc
12 Path string
13}
14
15type Group struct {
16 Root string
17 Middlewares []Middleware
18 Routes []Route
19}
20
21var Groups = []Group{
22 {
23 "",
24 nil,
25 []Route{
26 {"POST", []gin.HandlerFunc{Signup}, "/signup"},
27 {"POST", []gin.HandlerFunc{Signin}, "/signin"},
28 },
29 },
30 {
31 "/otp",
32 []Middleware{JwtAuth, UserConfirmed},
33 []Route{
34 {"GET", []gin.HandlerFunc{OtpEnrollment}, "/enroll"},
35 {"POST", []gin.HandlerFunc{OtpValidate}, "/validate"},
36 },
37 },
38 {
39 "/market",
40 []Middleware{JwtAuth, UserConfirmed, OtpAuth},
41 []Route{
42 {"GET", []gin.HandlerFunc{GetMarketConfig}, "/:name"},
43 {"POST", []gin.HandlerFunc{UpdateMarketConfig}, "/:name/update"},
6c5c1403 44 {"GET", []gin.HandlerFunc{GetMarketBalance}, "/:name/balance"},
7a9e5112 45 },
46 },
47}
48
49func Signup(c *gin.Context) {
50 query := &SignupQuery{}
51
52 query.In.Email = c.PostForm("email")
53 query.In.Password = c.PostForm("password")
54
55 RunQuery(query, c)
56}
57
58func Signin(c *gin.Context) {
59 query := &SigninQuery{}
60
61 query.In.Email = c.PostForm("email")
62 query.In.Password = c.PostForm("password")
63
64 RunQuery(query, c)
65}
66
67func OtpValidate(c *gin.Context) {
68 query := &OtpValidateQuery{}
69
70 query.In.Pass = c.PostForm("pass")
71 query.In.User = GetUser(c)
72 query.In.Claims = GetClaims(c)
73
74 RunQuery(query, c)
75}
76
77func OtpEnrollment(c *gin.Context) {
78 query := &OtpEnrollmentQuery{}
79
80 query.In.User = GetUser(c)
81
82 qrPng, secret, err := query.Run()
83 if err != nil {
84 WriteJsonResponse(ErrorResponse(err.Code, err.UserMessage), c)
85 c.Error(err)
86 return
87 }
88
89 if c.Query("format") == "png" {
90 c.Header("X-OTP-Secret", secret)
91 WriteBinary("image/png", qrPng.Bytes(), c)
92 } else {
93 response := struct {
94 Base64img string `json:"base64img"`
95 OtpSecret string `json:"secret"`
96 }{
97 base64.StdEncoding.EncodeToString(qrPng.Bytes()),
98 secret,
99 }
100
101 WriteJsonResponse(SuccessResponse(response), c)
102 }
103
104}
105
106func GetMarketConfig(c *gin.Context) {
107 query := &MarketConfigQuery{}
108
109 query.In.User = GetUser(c)
110 query.In.Market = c.Param("name")
111
112 RunQuery(query, c)
113}
114
6c5c1403
J
115func GetMarketBalance(c *gin.Context) {
116 query := &MarketBalanceQuery{}
117
118 query.In.User = GetUser(c)
119 query.In.Market = c.Param("name")
120 query.In.Currency = "BTC"
121
122 RunQuery(query, c)
123}
124
7a9e5112 125func UpdateMarketConfig(c *gin.Context) {
126 query := &UpdateMarketConfigQuery{}
127
128 query.In.User = GetUser(c)
129 query.In.Market = c.Param("name")
130 query.In.Key = c.PostForm("key")
131 query.In.Secret = c.PostForm("secret")
132
133 RunQuery(query, c)
134}