|
| 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. |
0 commit comments