-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
160 lines (138 loc) · 3.84 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
package main
//go:generate go run -tags=dev generate_assets.go
import (
"log"
"fmt"
"runtime/debug"
"github.com/appservR/appservR/modules/config"
"github.com/kardianos/service"
"github.com/spf13/cobra"
)
var (
address string
port string
mode string
logger service.Logger
)
func startApp() {
server, err := InitializeServer(config.RunFlags{Address: address, Mode: mode, Port: port})
if err != nil {
panic(err)
}
server.Start()
}
type program struct{}
func (p *program) Start(s service.Service) error {
go p.run()
return nil
}
func (p *program) run() {
startApp()
}
func (p *program) Stop(s service.Service) error {
return nil
}
func main() {
defer func() {
if r := recover(); r != nil {
fmt.Println("stacktrace from panic: \n" + string(debug.Stack()))
}
}()
svcConfig := &service.Config{
Name: "appservR",
DisplayName: "appservR",
Description: "Serving R web apps",
}
prg := &program{}
s, err := service.New(prg, svcConfig)
if err != nil {
log.Fatal(err)
}
logger, err = s.Logger(nil)
if err != nil {
log.Fatal(err)
}
cmdRoot := &cobra.Command{
Use: "appservR",
Short: "A server for R web apps",
Long: `AppservR is a program to deploy easily R web app on Windows and Linux`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Use \"appservR help\" for more information about available commands")
s.Run()
},
}
cmdRoot.Flags().StringVarP(&address, "address", "a", "", "server hostname or ip adress (default \"localhost\")")
cmdRoot.Flags().StringVarP(&port, "port", "p", "", "server port (default 8080)")
cmdRoot.Flags().StringVarP(&mode, "mode", "m", "", "prod or debug mode (default \"prod\")")
cmdServe := &cobra.Command{
Use: "serve",
Short: "Start server",
Long: `Start server`,
Run: func(cmd *cobra.Command, args []string) {
startApp()
},
}
cmdServe.Flags().StringVarP(&address, "address", "a", "", "server hostname or ip adress (default \"localhost\")")
cmdServe.Flags().StringVarP(&port, "port", "p", "", "server port (default 8080)")
cmdServe.Flags().StringVarP(&mode, "mode", "m", "", "prod or debug mode (default \"prod\")")
cmdService := &cobra.Command{
Use: "service",
Short: "Manage service (install, remove, start, stop, run)",
Long: `Manage appservR service (install, remove, start, stop, run)`,
}
cmdInstall := &cobra.Command{
Use: "install",
Short: "Install appservR as a service",
Long: `Install appservR as a service`,
Run: func(cmd *cobra.Command, args []string) {
err := s.Install()
if err != nil {
fmt.Printf("Failed to install: %s\n", err)
return
}
fmt.Printf("Service \"%s\" installed.\n", svcConfig.DisplayName)
},
}
var cmdRemove = &cobra.Command{
Use: "remove",
Short: "Remove appservR service",
Long: `Remove appservR service if previously installed`,
Run: func(cmd *cobra.Command, args []string) {
err := s.Uninstall()
if err != nil {
fmt.Printf("Failed to remove: %s\n", err)
return
}
fmt.Printf("Service \"%s\" removed.\n", svcConfig.DisplayName)
},
}
var cmdStart = &cobra.Command{
Use: "start",
Short: "Start appservR service",
Long: `Start appservR service if previously installed`,
Run: func(cmd *cobra.Command, args []string) {
err := s.Start()
if err != nil {
fmt.Printf("Failed to start: %s\n", err)
return
}
fmt.Printf("Service \"%s\" started.\n", svcConfig.DisplayName)
},
}
var cmdStop = &cobra.Command{
Use: "stop",
Short: "Stop appservR service",
Long: `Stop appservR service`,
Run: func(cmd *cobra.Command, args []string) {
err := s.Stop()
if err != nil {
fmt.Printf("Failed to stop: %s\n", err)
return
}
fmt.Printf("Service \"%s\" stopped.\n", svcConfig.DisplayName)
},
}
cmdRoot.AddCommand(cmdServe, cmdService)
cmdService.AddCommand(cmdInstall, cmdRemove, cmdStart, cmdStop)
cmdRoot.Execute()
}