-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
50 lines (40 loc) · 1.34 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
package main
import (
"fmt"
"log"
"myblog/app"
"net/http"
"os"
"path/filepath"
"github.com/gobuffalo/packr/v2"
"github.com/gorilla/mux"
"github.com/unrolled/secure"
)
var path, _ = os.Executable()
var baseDir = filepath.Dir(path)
var staticDir = baseDir + "/web/static/"
var staticBox = packr.New("Static", "./web/static")
func main() {
router := mux.NewRouter()
router.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
log.Println(r.Header)
log.Println("Healthcheck /healthz")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "ok")
})
// Redirect to HTTPS in prod
sslRedirect := (os.Getenv("SSL_REDIRECT") == "true")
secureMiddleware := secure.New(secure.Options{
SSLProxyHeaders: map[string]string{"X-Forwarded-Proto": "https"},
SSLRedirect: sslRedirect,
})
subRouter := router.PathPrefix("/").Subrouter()
subRouter.Use(secureMiddleware.Handler)
subRouter.HandleFunc("/", app.HomeHandler).Methods("GET")
subRouter.HandleFunc("/about", app.AboutHandler).Methods("GET")
subRouter.HandleFunc("/posts/{slug}", app.PostHandler).Methods("GET")
subRouter.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(staticBox)))
router.PathPrefix("/").HandlerFunc(app.CatchAllHandler)
log.Println("Listening on port " + os.Getenv("PORT"))
http.ListenAndServe(":"+os.Getenv("PORT"), router)
}