generated from cds-snc/project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
42 lines (34 loc) · 1.05 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
package main
import (
"net/http"
"os"
"github.com/aws/aws-lambda-go/lambda"
"github.com/awslabs/aws-lambda-go-api-proxy/httpadapter"
)
func addDefaultHeaders(w http.ResponseWriter) http.ResponseWriter {
// HSTS
w.Header().Set("Strict-Transport-Security", "max-age=31536000; includeSubDomains; preload")
// X-Frame-Options
w.Header().Set("X-Frame-Options", "SAMEORIGIN")
// X-Content-Type-Options
w.Header().Set("X-Content-Type-Options", "nosniff")
// Referrer-Policy
w.Header().Set("Referrer-Policy", "no-referrer-when-downgrade")
return w
}
func addSecurityHeaders(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w = addDefaultHeaders(w)
h.ServeHTTP(w, r)
})
}
func main() {
// Static dir from ENV
static_dir := "/var/www/html"
if static_dir_env := os.Getenv("CONTENT_DIR"); static_dir_env != "" {
static_dir = static_dir_env
}
fs := http.FileServer(http.Dir(static_dir))
http.Handle("/", addSecurityHeaders(fs))
lambda.Start(httpadapter.NewV2(http.DefaultServeMux).ProxyWithContext)
}