mirror of
https://github.com/roleypoly/roleypoly.git
synced 2025-06-16 17:49:09 +00:00
add some gcf scaffolding
This commit is contained in:
parent
3eba2d2de8
commit
d8a25024de
5 changed files with 116 additions and 0 deletions
60
src/functions/cmd/main.go
Normal file
60
src/functions/cmd/main.go
Normal file
|
@ -0,0 +1,60 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/GoogleCloudPlatform/functions-framework-go/funcframework"
|
||||
sessiondata "github.com/roleypoly/roleypoly/src/functions/session-data"
|
||||
sessionprewarm "github.com/roleypoly/roleypoly/src/functions/session-prewarm"
|
||||
)
|
||||
|
||||
var mappings map[string]http.HandlerFunc = map[string]http.HandlerFunc{
|
||||
"/session-prewarm": sessionprewarm.SessionPrewarm,
|
||||
"/session-data": sessiondata.SessionData,
|
||||
}
|
||||
|
||||
var port string
|
||||
|
||||
func main() {
|
||||
ctx := context.Background()
|
||||
|
||||
err := funcframework.RegisterHTTPFunctionContext(ctx, "/", rootHandler)
|
||||
if err != nil {
|
||||
log.Fatalf("funcframework.RegisterHTTPFunctionContext: %v\n", err)
|
||||
}
|
||||
|
||||
for path, handler := range mappings {
|
||||
err := funcframework.RegisterHTTPFunctionContext(ctx, path, handler)
|
||||
if err != nil {
|
||||
log.Fatalf("funcframework.RegisterHTTPFunctionContext: %v\n", err)
|
||||
}
|
||||
|
||||
fmt.Println("Added", path)
|
||||
}
|
||||
|
||||
// Use PORT environment variable, or default to 6600.
|
||||
port = "6600"
|
||||
if envPort := os.Getenv("PORT"); envPort != "" {
|
||||
port = envPort
|
||||
}
|
||||
|
||||
fmt.Printf("Starting on http://localhost:%s\n", port)
|
||||
|
||||
if err := funcframework.Start(port); err != nil {
|
||||
log.Fatalf("funcframework.Start: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func rootHandler(rw http.ResponseWriter, r *http.Request) {
|
||||
body := `<!doctype html><meta charset="utf-8"><title>Roleypoly Functions</title><style>body{font-family: sans-serif; font-size: 1.4em}</style><h1>Function Index</h1>`
|
||||
|
||||
for path := range mappings {
|
||||
body += fmt.Sprintf(`<a href="http://localhost:%s%s">%s</a><br>`, port, path, path)
|
||||
}
|
||||
|
||||
fmt.Fprintln(rw, body)
|
||||
}
|
0
src/functions/session-data/client/client.go
Normal file
0
src/functions/session-data/client/client.go
Normal file
10
src/functions/session-data/session-data.go
Normal file
10
src/functions/session-data/session-data.go
Normal file
|
@ -0,0 +1,10 @@
|
|||
package sessiondata
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func SessionData(rw http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintln(rw, "Hello world!")
|
||||
}
|
7
src/functions/session-prewarm/session-prewarm.go
Normal file
7
src/functions/session-prewarm/session-prewarm.go
Normal file
|
@ -0,0 +1,7 @@
|
|||
package sessionprewarm
|
||||
|
||||
import "net/http"
|
||||
|
||||
func SessionPrewarm(rw http.ResponseWriter, r *http.Request) {
|
||||
rw.Write([]byte("hello work!"))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue