-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
184 lines (166 loc) · 3.89 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"log"
"net/http"
"path/filepath"
"github.com/Tech-With-Tim/cdn/api"
"github.com/Tech-With-Tim/cdn/api/handlers"
"github.com/Tech-With-Tim/cdn/docs"
"github.com/Tech-With-Tim/cdn/server"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
// _ "net/http/pprof" // only in use when profiling
"os"
"github.com/Tech-With-Tim/cdn/utils"
"github.com/urfave/cli/v2"
)
var app = cli.NewApp()
func main() {
//Export Env Variables If exist
//err := utils.ExportVariables()
config, err := utils.LoadConfig("./", "app")
if err != nil {
log.Fatalln(err.Error())
}
//Register Commands
commands(config)
err = app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func loadconfig(testconf bool) (conf utils.Config, err error) {
if testconf {
conf, err = utils.LoadConfig("./", "test")
} else {
conf, err = utils.LoadConfig("./", "app")
}
return
}
func commands(config utils.Config) {
app.Commands = []*cli.Command{
{
Name: "migrate_up",
Usage: "Migrate DB to latest version",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "test",
Aliases: []string{"t"},
Usage: "loads test.env instead of app.env",
},
},
Action: func(c *cli.Context) error {
conf, err := loadconfig(c.Bool("test"))
if err != nil {
return err
}
err = utils.MigrateUp(conf, "./models/migrations/")
if err != nil {
return err
}
return nil
},
},
{
Name: "dropdb",
Usage: "Drop the DB",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "test",
Aliases: []string{"t"},
Usage: "loads test.env instead of app.env",
},
},
Action: func(c *cli.Context) error {
conf, err := loadconfig(c.Bool("test"))
if err != nil {
return err
}
err = utils.MigrateDown(conf, "./models/migrations/")
if err != nil {
return err
}
return nil
},
},
{
Name: "migrate_steps",
Usage: "Migrate with Steps",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "steps",
Usage: "Number of steps of migrations to run",
},
&cli.BoolFlag{
Name: "test",
Aliases: []string{"t"},
Usage: "loads test.env instead of app.env",
},
},
Action: func(c *cli.Context) error {
conf, err := loadconfig(c.Bool("test"))
if err != nil {
return err
}
err = utils.MigrateSteps(c.Int("steps"), conf, "./models/migrations/")
if err != nil {
return err
}
return nil
},
},
{
Name: "generate_docs",
Usage: "Generate Documentation for the CDN",
Action: func(_ *cli.Context) error {
err := os.Chdir("./api/handlers")
if err != nil {
log.Fatal(err)
}
for route, handler := range api.Routes {
err := docs.AddDocs(route, handler)
if err != nil {
log.Fatal(err)
}
}
docs.GenerateDocs()
return nil
},
},
{
Name: "runserver",
Usage: "Run Api Server",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "host",
Usage: "Host on which server has to be run",
Value: "localhost",
Aliases: []string{"H"},
},
&cli.IntFlag{
Name: "port",
Usage: "Port on which server has to be run",
Value: 5000,
Aliases: []string{"P"},
},
},
Action: func(c *cli.Context) error {
s := server.NewServer(config)
//Create Routers Here
CdnRouter := chi.NewRouter()
//Add Routes to Routers Here
services := handlers.NewServiceHandler(s.Store, *s.Cache)
api.MainRouter(CdnRouter, config, services)
workDir, _ := os.Getwd()
filesDir := http.Dir(filepath.Join(workDir, "docs/docs-template/public"))
server.FileServer(s.Router, "/docs", filesDir)
//Mount Routers here
s.Router.Mount("/", CdnRouter)
s.Router.Mount("/debug/", middleware.Profiler()) // Only in use when profiling
//Store Router in Struct
err := s.RunServer(c.String("host"), c.Int("port"))
return err
},
},
}
}