Skip to content

Commit 15bb46f

Browse files
authored
Merge branch 'master' into dotnet-templates
2 parents 63249b7 + 2ce17e3 commit 15bb46f

File tree

6 files changed

+188
-0
lines changed

6 files changed

+188
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ serverless install -u https://github.com/serverless/examples/tree/master/folder-
9090
| [Azure Nodejs](https://github.com/serverless/examples/tree/master/azure-node-line-bot) <br/> Azure Functions sample for the Serverless framework | nodeJS |
9191
| [Azure Node Simple Http Endpoint](https://github.com/serverless/examples/tree/master/azure-node-simple-http-endpoint) <br/> An example of making http endpoints with the Azure Functions Serverless Framework plugin | nodeJS |
9292
| [Azure Nodejs](https://github.com/serverless/examples/tree/master/azure-node-telegram-bot) <br/> Azure Functions sample for the Serverless framework | nodeJS |
93+
| [Google Golang Simple Http Endpoint](https://github.com/serverless/examples/tree/master/google-golang-simple-http-endpoint) <br/> Example demonstrates how to setup a simple HTTP GET endpoint with golang | golang |
9394
| [Google Node Simple Http Endpoint](https://github.com/serverless/examples/tree/master/google-node-simple-http-endpoint) <br/> An example of making http endpoints with the Google Cloud Functions Serverless Framework plugin. | nodeJS |
9495
| [Gcp Node Typescript Simple](https://github.com/serverless/examples/tree/master/google-node-typescript-http-endpoint) <br/> Simple HTTP example for GCP functions by Serverless framework with Typescript | nodeJS |
9596
| [Google Python Simple Http Endpoint](https://github.com/serverless/examples/tree/master/google-python-simple-http-endpoint) <br/> Example demonstrates how to setup a simple HTTP GET endpoint with python | python |
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
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<!--
2+
title: 'GCF Simple HTTP Endpoint example in golang'
3+
description: This example demonstrates how to setup a simple golang HTTP GET endpoint on GCP Cloud Functions. When you ping the endpoint we've set up you'll see the time returned for the given request type.
4+
layout: Doc
5+
framework: v1
6+
platform: 'Google Cloud'
7+
language: golang
8+
authorLink: 'https://github.com/sebito91'
9+
authorName: 'Sebastian Borza'
10+
authorAvatar: 'https://avatars0.githubusercontent.com/u/3159454?v=4&s=140'
11+
-->
12+
13+
# Simple HTTP Endpoint Example
14+
15+
This example demonstrates how to setup a simple golang HTTP GET endpoint. When you fetch the endpoint we've set up here you'll see
16+
the time returned for the given request type.
17+
18+
## Use Cases
19+
20+
- Wrapping an existing internal or external endpoint/service
21+
22+
## Development
23+
24+
The GCF golang runtime has a few requirements when defining your solution, namely:
25+
26+
- Your function must be exported (e.g `Hello`)
27+
- You must define a non-main package, which can have an arbitrary name: `package p`
28+
- Your function code may not contain `package main` or a `func main()`
29+
30+
Since this is an alpha runtime provided by GCF the docs are not yet available for consumption. We'll update this README
31+
once those instructions are made public.
32+
33+
## Deploy
34+
35+
In order to deploy the you endpoint simply run
36+
37+
```bash
38+
serverless deploy -v
39+
```
40+
41+
The expected result should be similar to:
42+
43+
```bash
44+
Serverless: Uploading artifacts...
45+
Serverless: Artifacts successfully uploaded...
46+
Serverless: Updating deployment...
47+
Serverless: Checking deployment update progress...
48+
..............
49+
Serverless: Done...
50+
Service Information
51+
service: golang-simple-http-endpoint
52+
project: <project_name>
53+
stage: dev
54+
region: <region>
55+
56+
Deployed functions
57+
currentTime
58+
https://<region>-<project_name>.cloudfunctions.net/endpoint
59+
```
60+
61+
## Usage
62+
63+
You can now invoke the Cloud Function directly and even see the resulting log via
64+
65+
```bash
66+
serverless invoke --function hello
67+
```
68+
69+
The expected result should be similar to:
70+
71+
```bash
72+
Serverless: 6xthowrso4u2 {"message":"Go Serverless v1! Your function executed hello successfully!"}
73+
```
74+
75+
And to check out the logs directly from sls, you can run the following:
76+
77+
```bash
78+
serverless logs --function hello
79+
...
80+
Serverless: Displaying the 10 most recent log(s):
81+
82+
2018-11-21T17:44:58.450200631Z: Function execution took 7 ms, finished with status code: 200
83+
2018-11-21T17:44:58.443618562Z: Function execution started
84+
2018-11-21T17:43:40.651063187Z: Function execution took 8 ms, finished with status code: 200
85+
2018-11-21T17:43:40.644123421Z: Function execution started
86+
2018-11-21T17:43:35.688702419Z: Function execution took 25 ms, finished with status code: 200
87+
2018-11-21T17:43:35.664212161Z: Function execution started
88+
2018-11-21T17:40:46.166468516Z: Function execution took 6 ms, finished with status code: 200
89+
2018-11-21T17:40:46.161422666Z: Function execution started
90+
2018-11-21T17:33:27.004019351Z: Function execution took 10 ms, finished with status code: 200
91+
2018-11-21T17:33:26.994600775Z: Function execution started
92+
```
93+
94+
Finally you can send an HTTP request directly to the endpoint using a tool like curl:
95+
96+
```bash
97+
curl https://<region>-<project_name>.cloudfunctions.net/Hello
98+
```
99+
100+
**NOTE:** notice that the request terminates with your golang exported function name, not the serverless function
101+
name you've defined.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package hello
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"net/http"
7+
)
8+
9+
// Hello is a simple HTTP handler that addresses HTTP requests to the /hello endpoint
10+
func Hello(w http.ResponseWriter, r *http.Request) {
11+
var buf bytes.Buffer
12+
13+
body, err := json.Marshal(map[string]interface{}{
14+
"message": "Go Serverless v1! Your function executed hello successfully!",
15+
})
16+
if err != nil {
17+
w.WriteHeader(http.StatusNotFound)
18+
w.Write([]byte(err.Error()))
19+
return
20+
}
21+
json.HTMLEscape(&buf, body)
22+
23+
w.WriteHeader(http.StatusOK)
24+
w.Header().Set("Content-Type", "application/json")
25+
w.Header().Set("MyCompany-Func-Reply", "hello-handler")
26+
w.Write(buf.Bytes())
27+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "google-golang-simple-http-endpoint",
3+
"version": "0.0.1",
4+
"description": "Example demonstrates how to setup a simple HTTP GET endpoint with golang",
5+
"author": "Sebastian Borza <[email protected]>",
6+
"license": "MIT",
7+
"main": "handler.py",
8+
"scripts": {
9+
"test": "echo \"Error: no test specified\" && exit 1"
10+
},
11+
"dependencies": {
12+
"serverless-google-cloudfunctions": "^2.1.0"
13+
}
14+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
service: golang-simple-http-endpoint
2+
3+
frameworkVersion: ">=1.33.0 <2.0.0"
4+
5+
package:
6+
exclude:
7+
- node_modules/**
8+
- .gitignore
9+
- .git/**
10+
11+
plugins:
12+
- serverless-google-cloudfunctions
13+
14+
# The GCF credentials can be a little tricky to set up. Luckily we've documented this for you here:
15+
# https://serverless.com/framework/docs/providers/google/guide/credentials/
16+
#
17+
# NOTE: the golang runtime is currently in alpha state, you must have access from google to use the alpha toolchain
18+
provider:
19+
name: google
20+
runtime: go111 # currently both vendored and go.mod repos are supported
21+
project: sborza-91 # replace with your project name here
22+
credentials: ~/.gcloud/slsframework.json # path must be absolute, change to whichever keyfile you need
23+
24+
functions:
25+
hello:
26+
handler: Hello
27+
events:
28+
- http: path

0 commit comments

Comments
 (0)