-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
104 lines (84 loc) · 2.1 KB
/
server.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package server
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"cloud.google.com/go/datastore"
"github.com/nervelife/learning-golang/src/app/data"
"google.golang.org/api/iterator"
)
var ctx context.Context
var projectID string
var client datastore.Client
func init() {
fmt.Println("Initializing server...")
ctx = context.Background()
projectID = os.Getenv("GOOGLE_CLOUD_PROJECT")
if projectID == "" {
log.Fatal("Project ID Not Found")
}
// GOOGLE_APPLICATION_CREDENTIALS
c, err := datastore.NewClient(ctx, projectID)
if err != nil {
log.Fatalf("Failed to create client %v", err)
}
client = *c
log.Println("Initilized...")
}
// Run the server
func Run() {
http.HandleFunc("/", IndexHandler)
http.HandleFunc("/save-author", SaveAuthorHandler)
http.HandleFunc("/get-all-authors", GetAllAuthors)
port := os.Getenv("PORT")
if port == "" {
port = "8088"
log.Printf("Defaulting to port %s", port)
}
log.Printf("Listening on port %s", port)
if err := http.ListenAndServe(":"+port, nil); err != nil {
log.Fatal(err)
}
}
// IndexHandler is an handler
func IndexHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
fmt.Fprint(w, "Hello, World!")
}
// SaveAuthorHandler is an handler
func SaveAuthorHandler(w http.ResponseWriter, r *http.Request) {
a := data.AuthorEntity{
Name: "Giang giang",
Alive: true,
}
aKey := datastore.IncompleteKey("Authors", nil)
if _, err := client.Put(ctx, aKey, &a); err != nil {
log.Fatalf("Error saving to datastore %v", err)
}
}
// GetAllAuthors is an function
func GetAllAuthors(w http.ResponseWriter, r *http.Request) {
query := datastore.NewQuery("Authors").Filter("alive >", false)
it := client.Run(ctx, query)
var authors []data.AuthorEntity
for {
var author data.AuthorEntity
key, err := it.Next(&author)
if err == iterator.Done {
break
}
if err != nil {
log.Fatalf("Error fetching next author: %v", err)
}
author.ID = key.ID
authors = append(authors, author)
}
w.Header().Add("Content-Type", "application/json")
json.NewEncoder(w).Encode(authors)
}