Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
131 lines (107 loc) · 2.47 KB
/
Copy pathmain.go
File metadata and controls
131 lines (107 loc) · 2.47 KB
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
package main
import (
"encoding/json"
"encoding/xml"
"log"
"net/http"
"os"
"regexp"
"strings"
"github.com/gin-gonic/gin"
)
var (
errorCodes []ErrorCode
)
func checkError(err error) {
if err != nil {
log.Fatalf("WiiLink WFC Error API has encountered an error! Reason: %v\n", err)
}
}
func lookupCode(c *gin.Context) {
errorCode := c.Query("code")
if errorCode == "" {
c.JSON(http.StatusBadRequest, [1]ErrorResponse{{
Error: errorCode,
Found: 0,
InfoList: []ErrorInfo{},
}})
return
}
errorCodeRunes := []rune(errorCode)
var matchingCodes []ErrorCode
for _, codeInfo := range errorCodes {
matches, err := regexp.MatchString("^"+codeInfo.Regex+"$", errorCode)
checkError(err)
if matches {
// Replace placeholder characters
cardRunes := []rune(codeInfo.Card)
for index, character := range cardRunes {
if character == '?' {
cardRunes[index] = errorCodeRunes[index]
}
}
codeInfo.Card = string(cardRunes)
matchingCodes = append(matchingCodes, codeInfo)
}
}
if len(matchingCodes) == 0 {
c.JSON(http.StatusOK, [1]ErrorResponse{{
Error: errorCode,
Found: 0,
InfoList: []ErrorInfo{},
}})
return
}
var codeInfoList []ErrorInfo
for _, codeInfo := range matchingCodes {
xCount := strings.Count(codeInfo.Card, "x")
var itemType string
switch {
case xCount >= 4:
itemType = "Class"
case xCount >= 3:
itemType = "Section"
case xCount >= 1:
itemType = "Group"
default:
itemType = "Error"
}
if itemType == "Error" && len(codeInfo.Description) != 0 {
codeInfoList = append(codeInfoList, ErrorInfo{
Type: "Error",
Name: codeInfo.Card,
Info: strings.Join(codeInfo.Description, "\n"),
})
} else {
codeInfoList = append(codeInfoList, ErrorInfo{
Type: itemType,
Name: codeInfo.Card,
Info: codeInfo.Comment,
})
}
}
c.JSON(http.StatusOK, [1]ErrorResponse{{
Error: errorCode,
Found: 1,
InfoList: codeInfoList,
}})
}
func main() {
// Load the config
rawConfig, err := os.ReadFile("./config.xml")
checkError(err)
config := &Config{}
err = xml.Unmarshal(rawConfig, config)
checkError(err)
rawCodes, err := os.ReadFile("./error_codes.json")
checkError(err)
err = json.Unmarshal(rawCodes, &errorCodes)
checkError(err)
gin.SetMode(gin.ReleaseMode)
r := gin.Default()
r.GET("/", func(c *gin.Context) {
c.Redirect(http.StatusPermanentRedirect, config.RedirectAddress)
})
r.GET("/error", lookupCode)
log.Fatal(r.Run(config.APIAddress))
}