-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
68 lines (56 loc) · 1.45 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"cloud.google.com/go/datastore"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
)
type User struct {
Description string
}
// cronOnly defines a handler that only allows google cron jobs
func cronOnly(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("X-Appengine-Cron") != "true" {
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
return
}
next.ServeHTTP(w, r)
})
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
dc := r.Context().Value("datastore_client").(*datastore.Client)
dc = dc
fmt.Fprint(w, "Hello, World, from batch with love!")
}
func main() {
ctx := context.Background()
// This is because there is a bug in the
// https://github.com/googleapis/google-cloud-go/issues/1751
projectID := datastore.DetectProjectID
if os.Getenv("RUN_WITH_DEVAPPSERVER") == "1" {
projectID = "asdsad"
}
client, err := datastore.NewClient(ctx, projectID)
if err != nil {
log.Fatal(err)
}
defer client.Close()
r := chi.NewRouter()
r.Use(middleware.WithValue("datastore_client", client))
r.Use(cronOnly)
r.Get("/", indexHandler)
port := os.Getenv("PORT")
if port == "" {
port = "8080"
log.Printf("Defaulting to port %s", port)
}
log.Printf("Listening on port %s", port)
if err := http.ListenAndServe(":"+port, r); err != nil {
log.Fatal(err)
}
}