Skip to content

Commit 30e8681

Browse files
committed
Initial commit
0 parents  commit 30e8681

File tree

7 files changed

+232
-0
lines changed

7 files changed

+232
-0
lines changed

.gcloudignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# This file specifies files that are *not* uploaded to Google Cloud Platform
2+
# using gcloud. It follows the same syntax as .gitignore, with the addition of
3+
# "#!include" directives (which insert the entries of the given .gitignore-style
4+
# file at that point).
5+
#
6+
# For more information, run:
7+
# $ gcloud topic gcloudignore
8+
#
9+
.gcloudignore
10+
# If you would like to upload your .git directory, .gitignore file or files
11+
# from your .gitignore file, remove the corresponding line
12+
# below:
13+
.git
14+
.gitignore
15+
16+
node_modules
17+
.serverless

.gitignore

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Binaries for programs and plugins
2+
*.exe
3+
*.exe~
4+
*.dll
5+
*.so
6+
*.dylib
7+
8+
# Test binary, built with `go test -c`
9+
*.test
10+
11+
# Output of the go coverage tool, specifically when used with LiteIDE
12+
*.out
13+
14+
# Dependency directories (remove the comment below to include it)
15+
# vendor/
16+
17+
# Other
18+
.DS_Store
19+
node_modules
20+
.serverless

README.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Serverless Google Cloud Functions Golang Template
2+
3+
This template project is designed to be used as a starting point for a Google Cloud Functions project using the Golang runtime and the Serverless Framework.
4+
5+
## Requirements
6+
7+
- Install [Serverless Framework](https://www.serverless.com/framework/docs/getting-started/)
8+
- Setup you serverless environment to work with GCP by working you way through the Serverless Framework [GCP guides](https://www.serverless.com/framework/docs/providers/google/guide/intro/)
9+
10+
## Usage
11+
12+
Create a new service for each logical microservice you intend to deploy.
13+
14+
Each microservice could be made up of a single or several cloud functions.
15+
16+
### Create a new service
17+
18+
Use the `serverless create` command to download the template into a new project folder:
19+
20+
<pre>
21+
serverless create --template-url https://github.com/cgossain/serverless-google-cloud-functions-golang-template.git --path <b>mynewservice</b>
22+
</pre>
23+
24+
### Configure the template
25+
26+
1. Navigate to the template directory and install the Google Cloud Functions Provider Plugin:
27+
28+
<pre>
29+
cd <b>mynewservice</b> && serverless plugin install --name serverless-google-cloudfunctions
30+
</pre>
31+
32+
2. Update `go.mod` with your module name. For example:
33+
34+
<pre>
35+
module github.com/<b>my-gcp-project-name</b>/<b>mynewservice</b>
36+
37+
go 1.13
38+
</pre>
39+
40+
3. Change the root package name in both `fn.go` adn `fn_test.go` to match you service name:
41+
42+
<pre>
43+
package <b>mynewservice</b>
44+
45+
...
46+
</pre>
47+
48+
4. Open `serverless.yml` and update the configuration (i.e. service name, provider details, etc.):
49+
50+
<pre>
51+
service: <b>mynewservice</b>
52+
53+
provider:
54+
name: google
55+
runtime: go113
56+
project: <b>my-gcp-project-name</b>
57+
credentials: ~/.gcloud/keyfile.json # https://www.serverless.com/framework/docs/providers/google/guide/credentials/
58+
59+
plugins:
60+
- serverless-google-cloudfunctions
61+
62+
package:
63+
exclude:
64+
- .gitignore
65+
- .git/**
66+
67+
functions:
68+
hello:
69+
handler: Hello
70+
events:
71+
- http: path # https://www.serverless.com/framework/docs/providers/google/events/http#http-events
72+
73+
...
74+
</pre>
75+
76+
5. Take a look at [this guide](https://cloud.google.com/functions/docs/writing#structuring_source_code) for ideas on how to structure your source code for different scenarios:
77+
78+
## Deploy
79+
80+
Run the following command from your service directory to build and deploy all functions:
81+
82+
<pre>
83+
cd <b>mynewservice</b> && serverless deploy
84+
</pre>
85+
86+
## Remove Deployment
87+
88+
Run the following command from your service directory to remove the deployment of all functions:
89+
90+
<pre>
91+
cd <b>mynewservice</b> && serverless remove
92+
</pre>
93+
94+
## References
95+
96+
1. [Serverless GCP Golang Example](https://github.com/serverless/examples/tree/master/google-golang-simple-http-endpoint)

fn.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package templateService
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
)
7+
8+
func Hello(w http.ResponseWriter, r *http.Request) {
9+
name := r.URL.Query().Get("name")
10+
if name == "" {
11+
name = "someone"
12+
}
13+
fmt.Fprintf(w, "Hello, %s!", name)
14+
}

fn_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package templateService
2+
3+
import (
4+
"io/ioutil"
5+
"net/http"
6+
"net/http/httptest"
7+
"testing"
8+
)
9+
10+
func TestHello(t *testing.T) {
11+
tests := map[string]struct {
12+
name string
13+
wantStatus int
14+
wantString string
15+
}{
16+
"name specified": {"jdoe", http.StatusOK, "Hello, jdoe!"},
17+
"name not specified": {"", http.StatusOK, "Hello, someone!"},
18+
}
19+
20+
for name, te := range tests {
21+
t.Run(name, func(t *testing.T) {
22+
w := httptest.NewRecorder()
23+
r := httptest.NewRequest(http.MethodGet, "/", nil)
24+
q := r.URL.Query()
25+
q.Add("name", te.name)
26+
r.URL.RawQuery = q.Encode()
27+
28+
Hello(w, r)
29+
30+
rw := w.Result()
31+
defer rw.Body.Close()
32+
if s := rw.StatusCode; s != te.wantStatus {
33+
t.Fatalf("got: %d, want: %d", s, te.wantStatus)
34+
}
35+
b, err := ioutil.ReadAll(rw.Body)
36+
if err != nil {
37+
t.Fatal("failed to read res body")
38+
}
39+
if s := string(b); s != te.wantString {
40+
t.Fatalf("got: %s, want: %s", s, te.wantString)
41+
}
42+
})
43+
}
44+
}

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/project-m45/gcp-golang/myNewService
2+
3+
go 1.13

serverless.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
service: templateservice
2+
3+
provider:
4+
name: google
5+
runtime: go113
6+
project: my-gcp-project-name
7+
credentials: ~/.gcloud/keyfile.json
8+
9+
plugins:
10+
- serverless-google-cloudfunctions
11+
12+
package:
13+
exclude:
14+
- .gitignore
15+
- .git/**
16+
17+
functions:
18+
hello:
19+
handler: Hello
20+
events:
21+
- http: path # https://www.serverless.com/framework/docs/providers/google/events/http#http-events
22+
# NOTE: the following uses an "event" event (pubSub event in this case).
23+
# Please create the corresponding resources in the Google Cloud
24+
# before deploying this service through Serverless
25+
#second:
26+
# handler: event
27+
# events:
28+
# - event:
29+
# eventType: providers/cloud.pubsub/eventTypes/topic.publish
30+
# resource: projects/*/topics/my-topic
31+
# you can define resources, templates etc. the same way you would in a
32+
# Google Cloud deployment configuration
33+
#resources:
34+
# resources:
35+
# - type: storage.v1.bucket
36+
# name: my-serverless-service-bucket
37+
# imports:
38+
# - path: my_template.jinja

0 commit comments

Comments
 (0)