-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
98 lines (88 loc) · 2.5 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
package main
import (
"flag"
"github.com/Wastack/adventure/controller"
"github.com/Wastack/adventure/docs" // docs is generated by Swag CLI, you have to import it.
"github.com/Wastack/adventure/engine"
"github.com/Wastack/adventure/engine/data/yaml"
"github.com/Wastack/adventure/graphgen"
"github.com/gin-gonic/gin"
"github.com/swaggo/files"
"github.com/swaggo/gin-swagger"
"io/ioutil"
"log"
"os"
)
func parse(file_path string, is_verbose bool) (engine.GameDataI, error) {
file, err := os.Open(file_path)
if err != nil {
return nil, err
}
b, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
return yaml.Parse_yaml(b, is_verbose)
}
type CmdParams struct {
port_number string
with_api bool
yaml_path string
is_verbose bool
generate_dot_path string
}
func parse_clo() (params CmdParams) {
portPtr := flag.String("port", "8080", "API port")
withApiPtr := flag.Bool("with-api", true, "whether API should be served")
isVerbosePtr := flag.Bool("verbose", false, "Verbose output")
var generate_dot_path string
flag.StringVar(&generate_dot_path, "generate-dot", "", "Generate dot file to file")
flag.Parse()
params = CmdParams{
port_number: *portPtr,
with_api: *withApiPtr,
is_verbose: *isVerbosePtr,
generate_dot_path: generate_dot_path,
}
args := flag.Args()
if len(args) < 1 {
log.Fatalf("Missing yaml file path as first argument\n")
}
params.yaml_path = args[0]
return
}
func main() {
params := parse_clo()
game_data, err := parse(params.yaml_path, params.is_verbose)
if err != nil {
log.Fatalf("Error while parsing yaml file: %v", err)
}
if params.generate_dot_path != "" {
err := graphgen.WriteDotGraphTo(params.generate_dot_path, game_data)
if err != nil {
log.Printf("Dot file generation failed: %v", err)
}
os.Exit(0)
}
if params.with_api {
if !params.is_verbose {
gin.SetMode(gin.ReleaseMode)
}
// Set swagger info
docs.SwaggerInfo.Title = "Swagger Adventure API"
docs.SwaggerInfo.Description = "This is a server for the adventure game engine."
docs.SwaggerInfo.Version = "0.0"
r := gin.New()
c := controller.NewController(game_data)
// Create API for engine
adventure := r.Group("/adventure")
{
adventure.GET("/first", c.ShowFirstState)
adventure.GET("", c.NextState)
adventure.GET("/secret", c.ResolveSecret)
}
// use ginSwagger middleware to serve the API docs
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
r.Run(":" + params.port_number)
}
}