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