-
Notifications
You must be signed in to change notification settings - Fork 64
/
main.go
executable file
·175 lines (158 loc) · 4.43 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
package main
import (
"os"
"strconv"
"time"
"github.com/gaowei-space/markdown-blog/internal/app"
"github.com/gaowei-space/markdown-blog/internal/utils"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v2/altsrc"
)
//go:generate go-bindata -fs -o internal/bindata/views/views.go -pkg=views -prefix=web/views ./web/views/...
//go:generate go-bindata -fs -o internal/bindata/assets/assets.go -pkg=assets -prefix=web/assets ./web/assets/...
var (
MdDir = "md/"
Title = "Blog"
AppVersion = "1.1.1"
BuildDate, GitCommit string
)
// web服务器默认端口
const DefaultPort = 5006
func main() {
cliApp := cli.NewApp()
cliApp.Name = "markdown-blog"
cliApp.Usage = "Markdown Blog App"
cliApp.Version, _ = utils.FormatAppVersion(AppVersion, GitCommit, BuildDate)
cliApp.Commands = getCommands()
cliApp.Flags = append(cliApp.Flags, []cli.Flag{}...)
cliApp.Run(os.Args)
}
func getCommands() []*cli.Command {
web := webCommand()
return []*cli.Command{web}
}
func webCommand() *cli.Command {
commonFlags := []cli.Flag{
&cli.StringFlag{
Name: "config",
Value: "",
Usage: "Load configuration from `FILE`, default is empty",
},
altsrc.NewStringFlag(&cli.StringFlag{
Name: "dir",
Aliases: []string{"d"},
Value: MdDir,
Usage: "Markdown files dir",
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: "title",
Aliases: []string{"t"},
Value: Title,
Usage: "Blog title",
}),
altsrc.NewIntFlag(&cli.IntFlag{
Name: "port",
Aliases: []string{"p"},
Value: DefaultPort,
Usage: "Bind port",
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: "env",
Aliases: []string{"e"},
Value: "prod",
Usage: "Runtime environment, dev|test|prod",
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: "index",
Aliases: []string{"i"},
Value: "",
Usage: "Home page, default is empty",
}),
altsrc.NewIntFlag(&cli.IntFlag{
Name: "cache",
Aliases: []string{"c"},
Value: 3,
Usage: "The cache time unit is minutes, this parameter takes effect in the prod environment",
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: "icp",
Value: "",
Usage: "ICP, default is empty",
}), altsrc.NewStringFlag(&cli.StringFlag{
Name: "isf",
Value: "",
Usage: "National Internet Security Filing, default is empty",
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: "copyright",
Value: strconv.Itoa(time.Now().Year()),
Usage: "Copyright, default the current year, such as 2024",
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: "fdir",
Value: "public",
Usage: "File directory name",
}),
}
gitalkFlags := []cli.Flag{
altsrc.NewStringFlag(&cli.StringFlag{
Name: "gitalk.client-id",
Usage: "Set up Gitalk ClientId, default is empty",
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: "gitalk.client-secret",
Usage: "Set up Gitalk ClientSecret, default is empty",
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: "gitalk.repo",
Usage: "Set up Gitalk Repo, default is empty",
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: "gitalk.owner",
Usage: "Set up Gitalk Repo, default is empty",
}),
altsrc.NewStringSliceFlag(&cli.StringSliceFlag{
Name: "gitalk.admin",
Usage: "Set up Gitalk Admin, default is `[gitalk.owner]`",
}),
altsrc.NewStringSliceFlag(&cli.StringSliceFlag{
Name: "gitalk.labels",
Usage: "Set up Gitalk Admin, default is `[\"gitalk\"]`",
}),
}
flags := append(commonFlags, gitalkFlags...)
analyzerFlags := []cli.Flag{
altsrc.NewStringFlag(&cli.StringFlag{
Name: "analyzer-baidu",
Aliases: []string{"ab"},
Value: "",
Usage: "Set up Baidu Analyzer, default is empty",
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: "analyzer-google",
Aliases: []string{"ag"},
Value: "",
Usage: "Set up Google Analyzer, default is empty",
}),
}
flags = append(flags, analyzerFlags...)
ignoreFlags := []cli.Flag{
altsrc.NewStringSliceFlag(&cli.StringSliceFlag{
Name: "ignore-file",
Usage: "Set up ignore file, eg: demo.md",
}),
altsrc.NewStringSliceFlag(&cli.StringSliceFlag{
Name: "ignore-path",
Usage: "Set up ignore path, eg: demo",
}),
}
flags = append(flags, ignoreFlags...)
web := cli.Command{
Name: "web",
Usage: "Run blog web server",
Action: app.RunWeb,
Flags: flags,
Before: altsrc.InitInputSourceWithContext(flags, altsrc.NewYamlSourceFromFlagFunc("config")),
}
return &web
}