From 17726b330cf8df14635cf4ae19387e1310849e7f Mon Sep 17 00:00:00 2001 From: Karagulamos Date: Wed, 30 Mar 2022 12:55:41 +0100 Subject: [PATCH] feat: added scheduled functions template for Go (#4495) * feat: go scheduled functions template * chore: update contributors field * chore: placeholder for github handle in go mod * chore: remove unnecessary makefile * chore: go version update Co-authored-by: karagulamos --- package.json | 1 + .../.netlify-function-template.js | 5 +++ .../go/scheduled-function/go.mod | 5 +++ .../go/scheduled-function/go.sum | 21 +++++++++++ .../go/scheduled-function/netlify.toml | 11 ++++++ .../src/hourly-schedule/main.go | 36 +++++++++++++++++++ 6 files changed, 79 insertions(+) create mode 100644 src/functions-templates/go/scheduled-function/.netlify-function-template.js create mode 100644 src/functions-templates/go/scheduled-function/go.mod create mode 100644 src/functions-templates/go/scheduled-function/go.sum create mode 100644 src/functions-templates/go/scheduled-function/netlify.toml create mode 100644 src/functions-templates/go/scheduled-function/src/hourly-schedule/main.go diff --git a/package.json b/package.json index 4b3eb056788..a94560be0ef 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "Cole Bosmann", "Dan Croak (https://twitter.com/croaky)", "Dan Loewenherz (https://twitter.com/dwlz)", + "Daniel Woelfel (https://twitter.com/danielwoelfel)", "Dave Ackerman ", "David Calavera (https://twitter.com/calavera)", "David Lemler (https://twitter.com/davidlemlerm)", diff --git a/src/functions-templates/go/scheduled-function/.netlify-function-template.js b/src/functions-templates/go/scheduled-function/.netlify-function-template.js new file mode 100644 index 00000000000..9e35ebdd05d --- /dev/null +++ b/src/functions-templates/go/scheduled-function/.netlify-function-template.js @@ -0,0 +1,5 @@ +module.exports = { + name: 'scheduled-function', + priority: 1, + description: 'Basic implementation of a scheduled function in Go.', +} diff --git a/src/functions-templates/go/scheduled-function/go.mod b/src/functions-templates/go/scheduled-function/go.mod new file mode 100644 index 00000000000..bf815ab97ae --- /dev/null +++ b/src/functions-templates/go/scheduled-function/go.mod @@ -0,0 +1,5 @@ +module github.com/your-github-handle/{{name}} + +go 1.17 + +require github.com/aws/aws-lambda-go v1.28.0 diff --git a/src/functions-templates/go/scheduled-function/go.sum b/src/functions-templates/go/scheduled-function/go.sum new file mode 100644 index 00000000000..4f6d65221e6 --- /dev/null +++ b/src/functions-templates/go/scheduled-function/go.sum @@ -0,0 +1,21 @@ +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/aws/aws-lambda-go v1.28.0 h1:fZiik1PZqW2IyAN4rj+Y0UBaO1IDFlsNo9Zz/XnArK4= +github.com/aws/aws-lambda-go v1.28.0/go.mod h1:jJmlefzPfGnckuHdXX7/80O3BvUUi12XOkbv4w9SGLU= +github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= +github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/urfave/cli/v2 v2.2.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 h1:tQIYjPdBoyREyB9XMu+nnTclpTYkz2zFM+lzLJFO4gQ= +gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/src/functions-templates/go/scheduled-function/netlify.toml b/src/functions-templates/go/scheduled-function/netlify.toml new file mode 100644 index 00000000000..79d4132fcdb --- /dev/null +++ b/src/functions-templates/go/scheduled-function/netlify.toml @@ -0,0 +1,11 @@ +[build] +functions = "functions" + +[build.environment] +GO_VERSION = "1.17" + +[functions] +directory = "src/" + +[functions.hourly-schedule] +schedule = "@hourly" diff --git a/src/functions-templates/go/scheduled-function/src/hourly-schedule/main.go b/src/functions-templates/go/scheduled-function/src/hourly-schedule/main.go new file mode 100644 index 00000000000..996930651b3 --- /dev/null +++ b/src/functions-templates/go/scheduled-function/src/hourly-schedule/main.go @@ -0,0 +1,36 @@ +package main + +import ( + "context" + "encoding/json" + "log" + "net/http" + "time" + + "github.com/aws/aws-lambda-go/events" + "github.com/aws/aws-lambda-go/lambda" +) + +type requestBody struct { + NextRun time.Time `json:"next_run"` +} + +// The schedule for this function is defined inside the netlify.toml file. To learn about scheduled functions +// and supported cron extensions, visit https://ntl.fyi/sched-func. +func handler(ctx context.Context, request events.APIGatewayProxyRequest) (*events.APIGatewayProxyResponse, error) { + requestBody := requestBody{} + + if err := json.Unmarshal([]byte(request.Body), &requestBody); err != nil { + log.Fatalf("cannot parse request body: %s", err.Error()) + } + + log.Printf("Next run at %s\n", requestBody.NextRun.Local()) + + return &events.APIGatewayProxyResponse{ + StatusCode: http.StatusNoContent, + }, nil +} + +func main() { + lambda.Start(handler) +}