-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
68 lines (59 loc) · 1.3 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
package main
import (
"context"
"embed"
"flag"
"fmt"
"os"
"bifromq_engine/pkg/db"
"bifromq_engine/pkg/logs"
"bifromq_engine/pkg/routes"
"bifromq_engine/pkg/server"
"bifromq_engine/pkg/signals"
"github.com/gin-gonic/gin"
"github.com/spf13/cobra"
)
//go:embed ui/dist/*
var buildFS embed.FS
func main() {
logs.InitLogger()
ctx := signals.SetupSignalHandler()
if err := NewAppCommand(ctx).Execute(); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}
func NewAppCommand(ctx context.Context) *cobra.Command {
option := server.NewServerConfig()
cmd := &cobra.Command{
Use: "serve",
Long: `serve`,
Run: func(cmd *cobra.Command, args []string) {
logs.Infof("run with option:%+v", option)
if err := Run(ctx, option); err != nil {
logs.Fatal(err)
}
},
}
cmd.Flags().AddGoFlagSet(flag.CommandLine)
option.AddFlags(cmd.Flags())
return cmd
}
func Run(ctx context.Context, option *server.ServerConfig) error {
// Init DB
err := db.InitDB(option.DNS)
if err != nil {
return err
}
// Init Routes
gin := gin.Default()
routes.InitRoutes(gin, buildFS)
// init cordinator
coordinator, err2 := server.InitCoordinator(option.CooridnatorPort)
if err2 != nil {
return err2
}
go coordinator.Run(ctx.Done())
gin.Run(fmt.Sprintf(":%d", option.APIPort))
return nil
}