-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfaas.go
53 lines (42 loc) · 1.46 KB
/
faas.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
// for the main package contents, please see the trips package
// the faas top-level package is a GCP cloud function entry point.
// faas is an entry point for GCP serverless function, which cheekily
// supports other urls too
package faas
import (
"log"
"net/http"
"github.com/gorilla/mux"
"github.com/rorycl/timeaway/web"
)
// the BaseURL for GCP is not "/" but the project name
func init() {
web.BaseURL = "/timeaway"
}
// GCPServer is the entry point for a FAAS application as set out in
// section 4.5 of Joel Holmes' "Shipping Go". The idea for multiple
// endpoints is discussed at
// https://medium.com/google-cloud/hack-use-cloud-functions-as-a-webserver-with-golang-42edc7935247
func GCPServer(w http.ResponseWriter, r *http.Request) {
// setup the filesystem
web.SetupFS()
log.Println("dirfs: ", web.DirFS.TplFS, web.DirFS.StaticFS)
m := mux.NewRouter()
// static
m.PathPrefix("/static/").Handler(
http.StripPrefix("/static/",
http.FileServer(http.FS(web.DirFS.StaticFS))),
)
// partials
m.HandleFunc("/partials/details/show", web.PartialDetailsShow)
m.HandleFunc("/partials/details/hide", web.PartialDetailsHide)
m.HandleFunc("/partials/report", web.PartialReport)
m.HandleFunc("/partials/nocontent", web.PartialNoContent)
m.HandleFunc("/partials/addtrip", web.PartialAddTrip)
// main routes
m.HandleFunc("/", web.Home)
m.HandleFunc("/home", web.Home)
m.HandleFunc("/trips", web.Trips)
m.HandleFunc("/health", web.Health)
m.ServeHTTP(w, r)
}