-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
94 lines (77 loc) · 2.05 KB
/
main.go
File metadata and controls
94 lines (77 loc) · 2.05 KB
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
package main
import (
"flag"
"fmt"
"os"
"github.com/egansoft/breezy/config"
"github.com/egansoft/breezy/routing"
"github.com/egansoft/breezy/server"
"github.com/egansoft/breezy/utils"
)
var helpMsg = `Usage: breezy [OPTIONS] FILE
Run a Breezy server using the routes defined in FILE.
FILE consists of a newline seperated list of routes, which have the forms:
shell command:
/say/hello $ echo Hello!
Args can be specified with brackets:
/say/[msg] $ echo [msg]!
The request body is piped into stdin.
filesystem root:
/resources/url : myfilesystem/static/images
Static files from the filesystem path are served on the url path, as if the
filesystem were mounted on the url path.
If multiple routes match a given url, the first one is used.
Options:
-p, --port specify the port to run on, with 8080 as the default
-s, --shell specify a shell to use, with sh as the default
-d, --debug enable debug mode
-h, --help display this message and exit
`
func main() {
filename := parseArgs()
routes, err := utils.ReadFile(filename)
if err != nil {
fmt.Println(err)
usageAndExit()
}
router, err := routing.Parse(routes)
if err != nil {
fmt.Println(err)
usageAndExit()
}
s := server.New(router)
s.Start()
}
func parseArgs() string {
port := flag.Int("port", config.Port, "")
flag.IntVar(port, "p", config.Port, "")
debug := flag.Bool("debug", config.DebugMode, "")
flag.BoolVar(debug, "d", config.DebugMode, "")
shell := flag.String("shell", config.Shell, "")
flag.StringVar(shell, "s", config.Shell, "")
help := flag.Bool("help", false, "")
flag.BoolVar(help, "h", false, "")
flag.Usage = usageAndExit
flag.Parse()
if *help {
helpAndExit()
}
config.Port = *port
config.DebugMode = *debug
config.SetShell(*shell)
args := flag.Args()
if len(args) != 1 {
fmt.Println("Incorrect number of arguments")
usageAndExit()
}
routesFile := flag.Arg(0)
return routesFile
}
func helpAndExit() {
fmt.Printf(helpMsg)
os.Exit(1)
}
func usageAndExit() {
fmt.Println("See 'breezy --help'")
os.Exit(1)
}