/
fgframes.go
98 lines (85 loc) · 1.96 KB
/
fgframes.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 (
"github.com/gin-gonic/gin"
"io/ioutil"
"path/filepath"
"strings"
)
const BasePath = "data/title"
type TitleMap struct {
name string
characterMap map[string]Character
}
type Character struct {
name string
data string
}
func BuildData(title string) map[string]Character {
dataMap := make(map[string]Character)
titleMap := map[string]bool{
"usf4": true,
"sfv": true,
}
_, ok := titleMap[title]
if !ok {
return dataMap
}
names := CharacterNames(title)
for _, name := range names {
_, data := FindData(title, name)
dataMap[name] = Character{name, data}
}
return dataMap
}
func CharacterNames(title string) []string {
path := BasePath + "/" + title + "/" + "json"
names := []string{}
files, _ := ioutil.ReadDir(path)
for _, file := range files {
baseName := file.Name()
name := strings.TrimSuffix(baseName, filepath.Ext(baseName))
names = append(names, name)
}
return names
}
func FindData(title string, name string) (bool, string) {
path := BasePath + "/" + title + "/" + "json" + "/" + name + ".json"
data, err := ioutil.ReadFile(path)
if err != nil {
return false, "Character not found"
}
d := string(data)
return true, d
}
func main() {
router := gin.Default()
usf4Map := TitleMap{name: "usf4", characterMap: BuildData("usf4")}
router.GET("/:title/:name", func(context *gin.Context) {
switch title := context.Param("title"); title {
case "usf4":
name := context.Param("name")
if strings.EqualFold(name, "characters") {
context.JSON(200, gin.H{
"names": CharacterNames(title),
})
} else {
character, characterFound := usf4Map.characterMap[name]
if !characterFound {
context.JSON(404, gin.H{
"message": "character not found",
})
}
context.String(200, character.data)
}
case "sfv":
context.JSON(501, gin.H{
"message": "coming soon",
})
default:
context.JSON(404, gin.H{
"message": "title not found",
})
}
})
router.Run(":8080")
}