-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
122 lines (82 loc) · 2.39 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
package main
import (
"os"
"strings"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/notnil/chess"
"github.com/notnil/chess/image"
)
type Fen struct {
Fen string `query:"fen"`
}
type boardReq struct {
Fen string `form:"Fen" json:"Fen" xml:"Fen"`
}
func main() {
router := fiber.New()
router.Static("/", "./build")
router.Use(cors.New(cors.Config{
AllowOrigins: "*",
AllowHeaders: "*",
}))
router.Get("/api", createSvg)
router.Post("/api/adv", avdCreateSvg)
router.Listen("0.0.0.0:8080")
}
func createSvg(c *fiber.Ctx) error {
fen := new(Fen)
// check if fun is in call
if err := c.QueryParser(fen); err != nil {
return c.SendString("Oh no!")
}
name := strings.ReplaceAll(fen.Fen, "/", "")
// check if in system + if in system send it
if _, err := os.Stat("build/images/" + name); err == nil {
return c.SendFile("build/images/" + name)
}
// create a file
f, err := os.Create("build/images/" + name + ".svg")
if err != nil {
c.SendString("Couldn't Create the File" + err.Error())
}
defer f.Close()
// create board position
pos := &chess.Position{}
if err := pos.UnmarshalText([]byte(fen.Fen)); err != nil {
return c.SendString("This position is not valid" + err.Error())
}
// write board SVG to file
if err := image.SVG(f, pos.Board()); err != nil {
return c.SendString("We could not complete the SVG create Process" + err.Error())
}
return c.SendFile("build/images/" + name + ".svg")
}
func avdCreateSvg(c *fiber.Ctx)error{
boardReq := new(boardReq)
if err := c.BodyParser(boardReq); err != nil {
return err
}
// check if fun is in call
name := strings.ReplaceAll(boardReq.Fen, "/", "")
// check if in system + if in system send it
if _, err := os.Stat("build/images/" + name); err == nil {
return c.SendFile("build/images/" + name)
}
// create a file
f, err := os.Create("build/images/" + name + ".svg")
if err != nil {
c.SendString("Couldn't Create the File" + err.Error())
}
defer f.Close()
// create board position
pos := &chess.Position{}
if err := pos.UnmarshalText([]byte(boardReq.Fen)); err != nil {
return c.SendString("This position is not valid" + err.Error())
}
// write board SVG to file
if err := image.SVG(f, pos.Board()); err != nil {
return c.SendString("We could not complete the SVG create Process" + err.Error())
}
return c.SendFile("build/images/" + name + ".svg")
}