-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
145 lines (117 loc) · 2.95 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"L5/env"
"L5/interruption"
"crypto/rand"
"encoding/base64"
"github.com/gin-gonic/gin"
"log"
"net/http"
"strconv"
"sync"
)
const arraySize = 30
var indices = make([]int, arraySize)
func init() {
for i := range indices {
indices[i] = i
}
}
type UserData struct {
Array [arraySize]int
Mu sync.Mutex
}
func (data *UserData) Get(i int) int {
data.Mu.Lock()
defer data.Mu.Unlock()
return data.Array[i]
}
func (data *UserData) Set(i int, val int) {
data.Mu.Lock()
defer data.Mu.Unlock()
data.Array[i] = val
}
func main() {
router := gin.Default()
router.LoadHTMLGlob("static/*")
if err := router.SetTrustedProxies(nil); err != nil {
log.Panicln("[main] error setting trusted proxies:", err)
}
router.GET("/", func(c *gin.Context) {
userData := updateCookie(c)
userData.Mu.Lock()
defer userData.Mu.Unlock()
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"numbers": userData.Array,
"indices": indices,
})
})
api := router.Group("/api")
api.GET("/get", func(c *gin.Context) {
userData := updateCookie(c)
indexString := c.Query("i")
index, err := strconv.Atoi(indexString)
if err != nil || index < 0 || index >= arraySize {
log.Println("index =", indexString, "& error:", err)
c.AbortWithStatus(http.StatusBadRequest)
return
}
c.JSON(http.StatusOK, gin.H{
"value": userData.Get(index),
})
})
api.PUT("/set", func(c *gin.Context) {
userData := updateCookie(c)
indexString := c.Query("i")
index, err := strconv.Atoi(indexString)
if err != nil || index < 0 || index >= arraySize {
log.Println("index =", indexString, "& error:", err)
c.AbortWithStatus(http.StatusBadRequest)
return
}
valueString := c.Query("v")
value, err := strconv.Atoi(valueString)
if err != nil {
log.Println("value =", valueString, "& error:", err)
c.AbortWithStatus(http.StatusBadRequest)
return
}
userData.Set(index, value)
c.Status(http.StatusOK)
})
go func() {
if err := router.Run(":" + env.Port()); err != nil {
log.Println("[main] error running server:", err)
}
}()
interruption.Wait()
}
var sessions sync.Map
func updateCookie(c *gin.Context) *UserData {
cookie, err := c.Cookie("sessionId")
if err == nil {
if userData, ok := sessions.Load(cookie); ok {
return userData.(*UserData)
}
} else if err != http.ErrNoCookie {
log.Panicln("[main.updateCookie] unexpected cookie error:", err)
}
token, userData := newToken()
c.SetCookie("sessionId", token, 0, "/", env.Domain(), true, true)
return userData
}
func newToken() (string, *UserData) {
var token [32]byte
if _, err := rand.Read(token[:]); err != nil {
log.Panicln("[main.updateCookie] unexpected rand gen error:", err)
}
tokenString := base64.RawURLEncoding.EncodeToString(token[:])
userData := new(UserData)
for i := range userData.Array {
userData.Array[i] = i + 1
}
if _, loaded := sessions.LoadOrStore(tokenString, userData); loaded {
return newToken()
}
return tokenString, userData
}