-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (60 loc) · 1.96 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
package main
import (
"az-appservice/httpApi"
"az-appservice/log"
"crypto/tls"
"fmt"
"github.com/namsral/flag"
"net"
"net/http"
"os"
"os/signal"
"time"
)
var prod = flag.Bool("prod", false, "Disable verbose logging")
var httpPortFlag = flag.Int("http port", 8001, "A port to accept http requests, https will be set to this port +100")
var apiKeyFileFlag = flag.String("apiKey", "api-key.pem", "A path to a file containing PEM private key for http API")
var apiCertFileFlag = flag.String("apiCert", "api-cert.pem", "A path to a file containing PEM certificate for http API")
func runHTTP(port int, server *http.Server, keyFile string, certFile string) {
httpListener, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
log.Sugar.Errorf("Can not start http listening on port:%d\n%v", port, err)
os.Exit(3)
}
log.Sugar.Infof("Start http listening :%d", port)
//goland:noinspection GoUnhandledErrorResult
go server.Serve(httpListener)
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
log.Sugar.Errorf("Loading certificate: %v", err)
} else {
port = port + 100
tlsCfg := &tls.Config{Certificates: []tls.Certificate{cert}}
httpsListener, err := tls.Listen("tcp", fmt.Sprintf(":%d", port), tlsCfg)
if err != nil {
log.Sugar.Errorf("Can not start http listening on port:%d\n%v", port, err)
os.Exit(32)
}
log.Sugar.Infof("Start https listening :%d", port)
//goland:noinspection GoUnhandledErrorResult
go server.Serve(httpsListener)
}
}
func mainWithDeferred() {
//goland:noinspection GoUnhandledErrorResult
defer log.Log.Sync()
httpServer := httpApi.GetHttpServer()
runHTTP(*httpPortFlag, httpServer, *apiKeyFileFlag, *apiCertFileFlag)
time.Sleep(200 * time.Millisecond)
log.Sugar.Info("Hit Ctrl-C to exit...")
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, os.Interrupt)
_ = <-sigs
fmt.Println("\nGraceful exit")
time.Sleep(1 * time.Second)
}
func main() {
flag.Parse()
log.Setup(*prod)
mainWithDeferred()
}