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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
// Package main is located in the cmd/ponzu directory and contains the code to build
// and operate the command line interface (CLI) to manage Ponzu systems. Here,
// you will find the code that is used to create new Ponzu projects, generate
// code for content types and other files, build Ponzu binaries and run servers.
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/ponzu-cms/ponzu/system/admin"
"github.com/ponzu-cms/ponzu/system/api"
"github.com/ponzu-cms/ponzu/system/api/analytics"
"github.com/ponzu-cms/ponzu/system/db"
"github.com/ponzu-cms/ponzu/system/tls"
_ "github.com/ponzu-cms/ponzu/content"
)
var (
usage = usageHeader + usageNew + usageGenerate +
usageBuild + usageRun + usageUpgrade + usageAdd + usageVersion
port int
httpsport int
https bool
devhttps bool
cli bool
// for ponzu internal / core development
dev bool
fork string
gocmd string
)
func main() {
flag.Usage = func() {
fmt.Println(usage)
}
flag.IntVar(&port, "port", 8080, "port for ponzu to bind its HTTP listener")
flag.IntVar(&httpsport, "httpsport", 443, "port for ponzu to bind its HTTPS listener")
flag.BoolVar(&https, "https", false, "enable automatic TLS/SSL certificate management")
flag.BoolVar(&devhttps, "devhttps", false, "[dev environment] enable automatic TLS/SSL certificate management")
flag.BoolVar(&dev, "dev", false, "modify environment for Ponzu core development")
flag.BoolVar(&cli, "cli", false, "specify that information should be returned about the CLI, not project")
flag.StringVar(&fork, "fork", "", "modify repo source for Ponzu core development")
flag.StringVar(&gocmd, "gocmd", "go", "custom go command if using beta or new release of Go")
flag.Parse()
args := flag.Args()
if len(args) < 1 {
fmt.Println(usage)
os.Exit(0)
}
switch args[0] {
case "help", "h":
if len(args) < 2 {
fmt.Println(usageHelp)
fmt.Println(usage)
os.Exit(0)
}
switch args[1] {
case "new":
fmt.Println(usageNew)
os.Exit(0)
case "generate", "gen", "g":
fmt.Println(usageGenerate)
os.Exit(0)
case "build":
fmt.Println(usageBuild)
os.Exit(0)
case "run":
fmt.Println(usageRun)
os.Exit(0)
case "upgrade":
fmt.Println(usageUpgrade)
os.Exit(0)
case "version", "v":
fmt.Println(usageVersion)
os.Exit(0)
case "add", "a":
fmt.Println(usageAdd)
os.Exit(0)
}
case "new":
if len(args) < 2 {
fmt.Println(usageNew)
os.Exit(0)
}
err := newProjectInDir(args[1])
if err != nil {
fmt.Println(err)
os.Exit(1)
}
case "generate", "gen", "g":
if len(args) < 3 {
fmt.Println(usageGenerate)
os.Exit(0)
}
// check what we are asked to generate
switch args[1] {
case "content", "c":
err := generateContentType(args[2:])
if err != nil {
fmt.Println(err)
os.Exit(1)
}
default:
msg := fmt.Sprintf("Generator '%s' is not implemented.", args[1])
fmt.Println(msg)
}
case "build":
err := buildPonzuServer(args)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
case "run":
var addTLS string
if https {
addTLS = "--https"
} else {
addTLS = "--https=false"
}
if devhttps {
addTLS = "--devhttps"
}
var services string
if len(args) > 1 {
services = args[1]
} else {
services = "admin,api"
}
name := buildOutputName()
buildPathName := strings.Join([]string{".", name}, string(filepath.Separator))
serve := exec.Command(buildPathName,
fmt.Sprintf("--port=%d", port),
fmt.Sprintf("--httpsport=%d", httpsport),
addTLS,
"serve",
services,
)
serve.Stderr = os.Stderr
serve.Stdout = os.Stdout
err := serve.Run()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
case "serve", "s":
db.Init()
defer db.Close()
analytics.Init()
defer analytics.Close()
if len(args) > 1 {
services := strings.Split(args[1], ",")
for _, service := range services {
if service == "api" {
api.Run()
} else if service == "admin" {
admin.Run()
} else {
fmt.Println("To execute 'ponzu serve', you must specify which service to run.")
fmt.Println("$ ponzu --help")
os.Exit(1)
}
}
}
// save the https port the system is listening on
err := db.PutConfig("https_port", fmt.Sprintf("%d", httpsport))
if err != nil {
log.Fatalln("System failed to save config. Please try to run again.", err)
}
// cannot run production HTTPS and development HTTPS together
if devhttps {
fmt.Println("Enabling self-signed HTTPS... [DEV]")
go tls.EnableDev()
fmt.Println("Server listening on https://localhost:10443 for requests... [DEV]")
fmt.Println("----")
fmt.Println("If your browser rejects HTTPS requests, try allowing insecure connections on localhost.")
fmt.Println("on Chrome, visit chrome://flags/#allow-insecure-localhost")
} else if https {
fmt.Println("Enabling HTTPS...")
go tls.Enable()
fmt.Printf("Server listening on :%s for HTTPS requests...\n", db.ConfigCache("https_port").(string))
}
// save the https port the system is listening on so internal system can make
// HTTP api calls while in dev or production w/o adding more cli flags
err = db.PutConfig("http_port", fmt.Sprintf("%d", port))
if err != nil {
log.Fatalln("System failed to save config. Please try to run again.", err)
}
fmt.Printf("Server listening on :%d for HTTP requests...\n", port)
fmt.Println("\nvisit `/admin` to get started.")
log.Fatalln(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
case "version", "v":
// read ponzu.json value to Stdout
p, err := ponzu(cli)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Fprintf(os.Stdout, "Ponzu v%s\n", p["version"])
case "upgrade":
// confirm since upgrade will replace Ponzu core files
path, err := os.Getwd()
if err != nil {
fmt.Println("Failed to find current directory.", err)
os.Exit(1)
}
fmt.Println("Only files you added to this directory, 'addons' and 'content' will be preserved.")
fmt.Println("Upgrade this project? (y/N):")
answer, err := getAnswer()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
switch answer {
case "n", "no", "\r\n", "\n", "":
fmt.Println("")
case "y", "yes":
err := upgradePonzuProjectDir(path)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
default:
fmt.Println("Input not recognized. No upgrade made. Answer as 'y' or 'n' only.")
}
case "add", "a":
// expecting two args, add and the go gettable package uri
if len(args) < 2 {
fmt.Println(usageAdd)
os.Exit(0)
}
err := getAddon(args)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
case "":
fmt.Println(usage)
fmt.Println(usageHelp)
default:
fmt.Println(usage)
fmt.Println(usageHelp)
}
}
|