forked from tuxin-skeleton/tuxin-skeleton-go-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebServer.go
72 lines (65 loc) · 2.45 KB
/
WebServer.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
package main
import (
"log"
"strconv"
"net/http"
"github.com/urfave/negroni"
"github.com/phyber/negroni-gzip/gzip"
"github.com/unrolled/secure"
"time"
"github.com/gorilla/mux"
"github.com/jingweno/negroni-gorelic"
)
var secureMiddleware *secure.Secure;
func angular2Handler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, CfgIni.WebServer.ClientPath + "/index.html")
}
func main() {
CfgIni = parseIni(configIniPath);
InitTlsConfig()
secureMiddleware= secure.New(secure.Options{
IsDevelopment: !CfgIni.Server.IsProduction,
SSLRedirect: true,
SSLHost: CfgIni.ServerName,
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"},
STSSeconds: 315360000,
STSIncludeSubdomains: true,
STSPreload: true,
FrameDeny: true,
ContentTypeNosniff: true,
BrowserXssFilter: true,
ContentSecurityPolicy: "default-src 'self'; img-src 'self' cdn.auth0.com; connect-src 'self' " + CfgIni.AccountDomain + ";style-src 'self' 'unsafe-inline' fonts.googleapis.com; font-src 'self' fonts.gstatic.com;script-src 'self' 'unsafe-eval' cdn.auth0.com;",
PublicKey: `pin-sha256="base64+primary=="; pin-sha256="base64+backup=="; max-age=5184000; includeSubdomains; report-uri="https://www.example.com/hpkp-report"`,
})
log.Print("started web server...");
httpsPortStr := ":" + strconv.FormatUint(CfgIni.HttpsPort, 10)
log.Printf("starting https web server at port %v", CfgIni.HttpsPort)
r := mux.NewRouter()
r.Path("/").HandlerFunc(angular2Handler)
for _, routeName := range CfgIni.WebServer.Routes {
r.PathPrefix("/" + routeName).HandlerFunc(angular2Handler)
}
r.PathPrefix("/").Handler(http.FileServer(http.Dir(CfgIni.WebServer.ClientPath)))
n := negroni.New()
n.Use(gzip.Gzip(gzip.DefaultCompression))
n.UseHandler(r)
n.Use(negroni.HandlerFunc(secureMiddleware.HandlerFuncWithNext))
if CfgIni.IsProduction {
n.Use(negronigorelic.New(CfgIni.Licensekey, CfgIni.AppName, true))
}
n.Use(negroni.NewLogger())
n.Use(negroni.NewRecovery())
srv := &http.Server{
Addr: httpsPortStr,
Handler: n,
ReadTimeout: time.Duration(CfgIni.ReadTimeout) * time.Second,
WriteTimeout: time.Duration(CfgIni.WriteTimeout) * time.Second,
TLSConfig: TlsConfig,
}
err := srv.ListenAndServeTLS(CfgIni.CertificateFile,CfgIni.PrivateKeyFile)
if err != nil {
log.Fatalf("https server stopped with the following error: %v", err)
} else {
log.Print("https server stopped with no error")
}
}