-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_api.go
98 lines (87 loc) · 2.27 KB
/
setup_api.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
type SetupAPIRequest struct {
SQL string `json:"sql"`
SchemaVersion int64 `json:"schemaVersion"`
Limit int `json:"limit"`
Digit int `json:"digit"` // TODO UUIDの桁数を指定しようかと思っているが未実装
}
func HandleSetupAPI(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
form := &SetupAPIRequest{}
if err := json.Unmarshal(b, form); err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
log.Printf("SETUP API BODY:%s\n", string(b))
if form.SQL == "" {
log.Printf("required SQL\n")
w.WriteHeader(http.StatusBadRequest)
return
}
if form.SchemaVersion == 0 {
log.Printf("required SchemaVersion")
w.WriteHeader(http.StatusBadRequest)
return
}
if form.Limit == 0 {
log.Printf("required Limit")
w.WriteHeader(http.StatusBadRequest)
return
}
pqs, err := NewFireQueueService(r.Host, TasksClient)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("failed NewFireQueueService.err=%+v", err)
return
}
prefix := GenerateUUIDPrefix()
for _, p := range prefix {
log.Printf("SQL is %s, Start:%s\n", form.SQL, p)
if err := pqs.AddTask(r.Context(), &FireQueueTask{
SQL: form.SQL,
SchemaVersion: form.SchemaVersion,
Limit: form.Limit,
StartID: p,
LastID: "",
}); err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Printf("failed AddTask.err=%+v", err)
return
}
}
w.WriteHeader(http.StatusOK)
_, err = w.Write([]byte("DONE"))
if err != nil {
log.Printf("failed response.write.err=%+v", err)
}
}
// GenerateUUIDPrefix is UUIDの先頭文字になりえる2桁の文字列一覧を返す
// for example 00, 01, 02..., a0, a1, a2 ...
func GenerateUUIDPrefix() []string {
var runeList []string
for i := 0; i < 10; i++ {
runeList = append(runeList, fmt.Sprintf("%d", i))
}
for i := 0; i < 6; i++ {
r := rune('a' + i)
runeList = append(runeList, fmt.Sprintf("%v", string(r)))
}
var results []string
for _, i := range runeList {
for _, j := range runeList {
results = append(results, fmt.Sprintf("%s%s", i, j))
}
}
return results
}