-
Notifications
You must be signed in to change notification settings - Fork 506
/
start.go
88 lines (81 loc) · 2.23 KB
/
start.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
package cmd
import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/LyricTian/gin-admin/v10/internal/bootstrap"
"github.com/LyricTian/gin-admin/v10/internal/config"
"github.com/urfave/cli/v2"
)
// The function defines a CLI command to start a server with various flags and options, including the
// ability to run as a daemon.
func StartCmd() *cli.Command {
return &cli.Command{
Name: "start",
Usage: "Start server",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "workdir",
Aliases: []string{"d"},
Usage: "Working directory",
DefaultText: "configs",
Value: "configs",
},
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Usage: "Runtime configuration files or directory (relative to workdir, multiple separated by commas)",
DefaultText: "dev",
Value: "dev",
},
&cli.StringFlag{
Name: "static",
Aliases: []string{"s"},
Usage: "Static files directory",
},
&cli.BoolFlag{
Name: "daemon",
Usage: "Run as a daemon",
},
},
Action: func(c *cli.Context) error {
workDir := c.String("workdir")
staticDir := c.String("static")
configs := c.String("config")
if c.Bool("daemon") {
bin, err := filepath.Abs(os.Args[0])
if err != nil {
fmt.Printf("failed to get absolute path for command: %s \n", err.Error())
return err
}
args := []string{"start"}
args = append(args, "-d", workDir)
args = append(args, "-c", configs)
args = append(args, "-s", staticDir)
fmt.Printf("execute command: %s %s \n", bin, strings.Join(args, " "))
command := exec.Command(bin, args...)
err = command.Start()
if err != nil {
fmt.Printf("failed to start daemon thread: %s \n", err.Error())
return err
}
pid := command.Process.Pid
_ = os.WriteFile(fmt.Sprintf("%s.lock", c.App.Name), []byte(fmt.Sprintf("%d", pid)), 0666)
fmt.Printf("service %s daemon thread started with pid %d \n", config.C.General.AppName, pid)
os.Exit(0)
}
err := bootstrap.Run(context.Background(), bootstrap.RunConfig{
WorkDir: workDir,
Configs: configs,
StaticDir: staticDir,
})
if err != nil {
panic(err)
}
return nil
},
}
}