-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
140 lines (128 loc) · 3.34 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
package main
import (
"context"
"fmt"
"log"
"os"
"path"
"sort"
_ "ariga.io/sqlcomment"
_ "github.com/Joker/hpp"
_ "github.com/davecgh/go-spew/spew"
"github.com/ngocphuongnb/tetua/app/asset"
"github.com/ngocphuongnb/tetua/app/auth"
"github.com/ngocphuongnb/tetua/app/cache"
"github.com/ngocphuongnb/tetua/app/cmd"
"github.com/ngocphuongnb/tetua/app/config"
"github.com/ngocphuongnb/tetua/app/fs"
"github.com/ngocphuongnb/tetua/app/logger"
"github.com/ngocphuongnb/tetua/app/repositories"
"github.com/ngocphuongnb/tetua/app/web"
sa "github.com/ngocphuongnb/tetua/packages/auth"
ent "github.com/ngocphuongnb/tetua/packages/entrepository"
"github.com/ngocphuongnb/tetua/packages/rclonefs"
zap "github.com/ngocphuongnb/tetua/packages/zaplogger"
"github.com/urfave/cli/v2"
)
func prepare(workingDir string) {
config.Init(workingDir)
themeDir := path.Join(config.ROOT_DIR, "app/themes", config.APP_THEME)
logger.New(zap.New(zap.Config{
Development: config.DEVELOPMENT,
LogFile: path.Join(config.PRIVATE_DIR, "logs/tetua.log"),
}))
repositories.New(ent.New(ent.Config{DB_DSN: config.DB_DSN}))
config.Settings(repositories.Setting.All(context.Background()))
asset.Load(themeDir, false)
fs.New(
config.STORAGES.DefaultDisk,
rclonefs.NewFromConfig(config.STORAGES),
)
auth.New(map[string]auth.NewProviderFn{
"local": sa.NewLocal,
"github": sa.NewGithub,
"google": sa.NewGoogle,
"twitter": sa.NewTwitter,
})
if err := cache.All(); err != nil {
log.Fatal("Cache error", err)
}
}
func getWd(c *cli.Context) string {
workingDir := c.Args().First()
if workingDir == "" {
workingDir = "."
}
if _, err := os.Stat(workingDir); os.IsNotExist(err) {
fmt.Println("Application directory not found")
os.Exit(1)
}
return workingDir
}
func main() {
app := &cli.App{
Name: "tetua",
Usage: "Easy blogging cms!",
Commands: []*cli.Command{
{
Name: "run",
Aliases: []string{"r"},
Usage: "Start tetua server",
Action: func(c *cli.Context) error {
prepare(getWd(c))
web.NewServer(web.Config{
JwtSigningKey: config.APP_KEY,
Theme: config.APP_THEME,
}).Listen(":3000")
return nil
},
},
{
Name: "init",
Aliases: []string{"i"},
Usage: "Initialize the Tetua application",
Action: func(c *cli.Context) error {
return config.CreateConfigFile(getWd(c))
},
},
{
Name: "setup",
Aliases: []string{"s"},
Usage: "Setup the tetua application",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "username",
Aliases: []string{"u"},
Usage: "Admin username",
Required: true,
},
&cli.StringFlag{
Name: "password",
Aliases: []string{"p"},
Usage: "Admin password",
Required: true,
},
},
Action: func(c *cli.Context) error {
prepare(getWd(c))
return cmd.Setup(c.String("username"), c.String("password"))
},
},
{
Name: "bundlestatic",
Usage: "Bundle static files",
Action: func(c *cli.Context) error {
prepare(getWd(c))
themeDir := path.Join(config.ROOT_DIR, "app/themes", config.APP_THEME)
asset.Load(themeDir, true)
return cmd.BundleStaticAssets()
},
},
},
}
sort.Sort(cli.FlagsByName(app.Flags))
sort.Sort(cli.CommandsByName(app.Commands))
if err := app.Run(os.Args); err != nil {
panic(err)
}
}