-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
146 lines (129 loc) · 3.43 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
package main
import (
"crypto/tls"
"fmt"
stderr "log"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/urfave/cli"
)
func main() {
app := cli.NewApp()
app.Name = "opinionated"
app.Usage = "Opinionated."
app.Version = MAJOR + "." + VERSION
app.EnableBashCompletion = true
Settings.Version = app.Version
Settings.Server = app.Name
Settings.Powered = app.Name + "/" + app.Version
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "debug, debugger, debugging, d",
Usage: "Enable debugging server.",
Destination: &Settings.Debugger,
},
}
app.Before = func(ctx *cli.Context) error {
if Settings.Debugger {
Debugger()
}
Settings.Mode = strings.ToLower(Settings.Mode)
if Settings.Addr == "" && Settings.Mode == "development" {
Settings.Addr = "localhost"
}
return nil
}
app.Commands = []cli.Command{
cli.Command{
Name: "start",
Aliases: []string{"server", "serve", "run"},
Usage: "Run.",
Flags: []cli.Flag{
cli.StringFlag{
Name: "address, a",
Usage: "Bind to address.",
Value: "localhost",
Destination: &Settings.Addr,
},
cli.IntFlag{
Name: "port, p",
Usage: "Server port.",
Value: 3443,
Destination: &Settings.Port,
},
cli.BoolFlag{
Name: "secure, k",
Usage: "Enable TLS server.",
Destination: &Settings.Secure,
},
cli.BoolFlag{
Name: "verbose, v",
Usage: "Enable verbose server.",
Destination: &Settings.Verbose,
},
cli.StringFlag{
Name: "mode",
Usage: "Provide a run-mode.",
EnvVar: "DEPLOYMENT",
Destination: &Settings.Mode,
},
},
Before: func(ctx *cli.Context) error {
Settings.Mode = strings.ToLower(Settings.Mode)
if Settings.Addr == "" && Settings.Mode == "development" {
Settings.Addr = "localhost"
}
return nil
},
Action: func(ctx *cli.Context) error {
privPort := strconv.Itoa(Settings.Port)
pubPort := "80"
if Settings.Port != 443 || Settings.Port >= 3443 {
pubPort = strconv.Itoa(Settings.Port - 363)
}
// Create service
srv := service()
pubAddr := Settings.Addr + ":" + pubPort
switch Settings.Secure {
case true:
crt := filepath.Join(Settings.Program.Data, "server.crt")
key := filepath.Join(Settings.Program.Data, "server.key")
_, err := tls.LoadX509KeyPair(crt, key)
if err == nil {
{
routes := http.NewServeMux()
pub := &http.Server{
Addr: pubAddr,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
Handler: routes,
}
routes.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
host := strings.Split(req.Host, ":")
forward := fmt.Sprintf("https://%s:%s%s", host[0], privPort, req.RequestURI)
stderr.Println("Forwarding to " + forward)
http.Redirect(w, req, forward, http.StatusMovedPermanently)
})
stderr.Printf("HTTP %s\n", pub.Addr)
go pub.ListenAndServe()
}
privAddr := Settings.Addr + ":" + privPort
stderr.Printf("HTTPS %s\n", privAddr)
return srv.ListenAndServeTLS(privAddr, crt, key)
}
stderr.Println(err)
fallthrough
case false:
stderr.Printf("HTTP %s\n", pubAddr)
return srv.ListenAndServe(pubAddr)
}
return nil
},
},
}
app.Run(os.Args)
}