Skip to content

Commit e247102

Browse files
authored
add go url shortener example (#44)
1 parent a464fe5 commit e247102

File tree

12 files changed

+410
-3
lines changed

12 files changed

+410
-3
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,10 @@ When utilizing the `nitric new` command to initiate a new project, the available
8383

8484
### Go
8585

86-
| Name | Description | Features |
87-
| ------------------------------ | ---------------- | -------- |
88-
| [go-starter](./v1/go-starter/) | REST API Starter | APIs |
86+
| Name | Description | Features |
87+
| ------------------------------------ | -------------------------- | ------------------------------ |
88+
| [go-starter](./v1/go-starter/) | REST API Starter | APIs |
89+
| [url-shortener](./v1/url-shortener/) | URL Shortener with Pub/Sub | APIs, Topics, Key Value Stores |
8990

9091
## About Nitric
9192

v1/url-shortener/.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
# 'nitric run' log directory
15+
.nitric/
16+
17+
git.store
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "go",
9+
"name": "Debug Nitric App",
10+
"request": "attach",
11+
"cwd": "${workspaceFolder}"
12+
}
13+
]
14+
}

v1/url-shortener/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<p align="center"><a href="https://nitric.io" target="_blank"><img src="https://raw.githubusercontent.com/nitrictech/nitric/main/docs/assets/nitric-logo.svg" height="120"></a></p>
2+
3+
## Project Description
4+
5+
Create a shortened URL that redirects to the full URL and receive notifications when URLs are created.
6+
7+
## About Nitric
8+
9+
This is a [Nitric](https://nitric.io) Golang project, but Nitric is a framework for rapid development of cloud-native and serverless applications in many languages.
10+
11+
Using Nitric you define your apps in terms of the resources they need, then write the code for serverless function based APIs, event subscribers and scheduled jobs.
12+
13+
Apps built with Nitric can be deployed to AWS, Azure or Google Cloud all from the same code base so you can focus on your products, not your cloud provider.
14+
15+
Nitric makes it easy to:
16+
17+
- Create smart [serverless functions and APIs](https://nitric.io/docs/apis)
18+
- Build reliable distributed apps that use [events](https://nitric.io/docs/messaging/topics) and/or [queues](https://nitric.io/docs/messaging/queues)
19+
- Securely store, retrieve and rotate [secrets](https://nitric.io/docs/secrets)
20+
- Read and write files from [buckets](https://nitric.io/docs/storage)
21+
22+
## Learning Nitric
23+
24+
Nitric provides detailed and intuitive [documentation](https://nitric.io/docs) and [guides](https://nitric.io/docs/getting-started) to help you get started quickly.
25+
26+
If you'd rather chat with the maintainers or community, come and join our [Discord](https://nitric.io/chat) server, [GitHub Discussions](https://github.com/nitrictech/nitric/discussions) or find us on [Twitter](https://twitter.com/nitric_io).
27+
28+
## Running this project
29+
30+
To run this project you'll need the [Nitric CLI](https://nitric.io/docs/installation) installed, then you can use the CLI commands to run, build or deploy the project.
31+
32+
```bash
33+
# install dependencies
34+
go mod tidy
35+
36+
# run locally
37+
nitric start
38+
```

v1/url-shortener/go.mod

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module github.com/nitrictech/examples/v1/url-shortener
2+
3+
go 1.21
4+
5+
toolchain go1.21.4
6+
7+
require github.com/nitrictech/go-sdk v1.0.7
8+
9+
require (
10+
github.com/missionMeteora/toolkit v0.0.0-20170713173850-88364e3ef8cc // indirect
11+
github.com/nitrictech/nitric/core v0.0.0-20240704002030-37a4af867ae4 // indirect
12+
github.com/nitrictech/protoutils v0.0.0-20220321044654-02667a814cdf // indirect
13+
golang.org/x/net v0.27.0 // indirect
14+
golang.org/x/sys v0.22.0 // indirect
15+
golang.org/x/text v0.16.0 // indirect
16+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 // indirect
17+
google.golang.org/grpc v1.65.0 // indirect
18+
google.golang.org/protobuf v1.34.2 // indirect
19+
)

v1/url-shortener/go.sum

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
4+
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
5+
github.com/golang/mock v1.7.0-rc.1 h1:YojYx61/OLFsiv6Rw1Z96LpldJIy31o+UHmwAUMJ6/U=
6+
github.com/golang/mock v1.7.0-rc.1/go.mod h1:s42URUywIqd+OcERslBJvOjepvNymP31m3q8d/GkuRs=
7+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
8+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
9+
github.com/missionMeteora/toolkit v0.0.0-20170713173850-88364e3ef8cc h1:/oFlKiuu6L1sIvZ7A363qMhNM+DUQL5WsVe1xIRQnFU=
10+
github.com/missionMeteora/toolkit v0.0.0-20170713173850-88364e3ef8cc/go.mod h1:AtX+JBtXbQ+taj82QFzCSgN5EzM4Bi0YRyS+TVbjENs=
11+
github.com/nitrictech/go-sdk v1.0.7 h1:SQsy522EhKCIc6NIHUMqDA4fDzD3gW0MHqABg1F840c=
12+
github.com/nitrictech/go-sdk v1.0.7/go.mod h1:QmLPatYHvbQ+8ajVDxHplgQIyWWP9SzMXDL+PI/gL2E=
13+
github.com/nitrictech/nitric/core v0.0.0-20240704002030-37a4af867ae4 h1:oQbSOBj7gr1pOZJD6QxmMrvI8DjKMETwV0IcXmv98fs=
14+
github.com/nitrictech/nitric/core v0.0.0-20240704002030-37a4af867ae4/go.mod h1:BQBNISFpA3R6TYyb76Q/c06gCBELCxooiocoq9Z5YAI=
15+
github.com/nitrictech/protoutils v0.0.0-20220321044654-02667a814cdf h1:8MB8W8ylM8sCM2COGfiO39/tB6BTdiawLszaUGCNL5w=
16+
github.com/nitrictech/protoutils v0.0.0-20220321044654-02667a814cdf/go.mod h1:b2lzk2a4o1bvSrSCE6yvTldHuXCJymuDVhdMJGOSslw=
17+
github.com/nxadm/tail v1.4.11 h1:8feyoE3OzPrcshW5/MJ4sGESc5cqmGkGCWlco4l0bqY=
18+
github.com/nxadm/tail v1.4.11/go.mod h1:OTaG3NK980DZzxbRq6lEuzgU+mug70nY11sMd4JXXHc=
19+
github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE=
20+
github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU=
21+
github.com/onsi/gomega v1.28.1 h1:MijcGUbfYuznzK/5R4CPNoUP/9Xvuo20sXfEm6XxoTA=
22+
github.com/onsi/gomega v1.28.1/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ=
23+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
24+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
25+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
26+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
27+
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
28+
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
29+
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
30+
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
31+
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
32+
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
33+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 h1:BwIjyKYGsK9dMCBOorzRri8MQwmi7mT9rGHsCEinZkA=
34+
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY=
35+
google.golang.org/grpc v1.65.0 h1:bs/cUb4lp1G5iImFFd3u5ixQzweKizoZJAwBNLR42lc=
36+
google.golang.org/grpc v1.65.0/go.mod h1:WgYC2ypjlB0EiQi6wdKixMqukr6lBc0Vo+oOgjrM5ZQ=
37+
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
38+
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
39+
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
40+
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
41+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
42+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

v1/url-shortener/golang.dockerfile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
FROM golang:alpine as build
3+
4+
ARG HANDLER
5+
6+
WORKDIR /app/
7+
8+
COPY go.mod *.sum ./
9+
10+
RUN go mod download
11+
12+
COPY . .
13+
14+
# Build the Go App from the provided HANDLER (this will be based on matches in your nitric.yaml fle)
15+
RUN go build -o /bin/main ./${HANDLER}/...
16+
17+
FROM alpine
18+
19+
COPY --from=build /bin/main /bin/main
20+
21+
RUN chmod +x-rw /bin/main
22+
RUN apk update && \
23+
apk add --no-cache tzdata ca-certificates && \
24+
update-ca-certificates
25+
26+
ENTRYPOINT ["/bin/main"]

v1/url-shortener/nitric.aws.yaml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# The nitric provider to use
2+
provider: nitric/[email protected]
3+
# The target aws region to deploy to
4+
# See available regions:
5+
# https://docs.aws.amazon.com/general/latest/gr/lambda-service.html
6+
region: us-east-1
7+
# Optional Configuration Below
8+
9+
# The timezone that deployed schedules will run with
10+
# Format is in tz identifiers:
11+
# https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
12+
# schedule-timezone: Australia/Sydney # Available since v0.27.0
13+
14+
# Import existing AWS Resources
15+
# Currently only secrets are supported
16+
# Available since v0.28.0
17+
# import:
18+
# # A name ARN map of secrets, where the name matches the nitric name of the secret you would like to import
19+
# secrets: # Available since v0.28.0
20+
# # In typescript this would import the provided secret reference for a secret declared as
21+
# # const mySecret = secret('my-secret');
22+
# my-secret: arn:...
23+
24+
# # Apply configuration to nitric APIs
25+
# apis:
26+
# # The nitric name of the API to configure
27+
# my-api:
28+
# # Array of domains to apply to the API
29+
# # The domain or parent domain must have a hosted zone already in Route53
30+
# domains:
31+
# - api.example.com
32+
33+
# # Configure your deployed functions/services
34+
# config:
35+
# # How functions without a type will be deployed
36+
# default:
37+
# # configure a sample rate for telemetry (between 0 and 1) e.g. 0.5 is 50%
38+
# telemetry: 0
39+
# # configure functions to deploy to AWS lambda
40+
# lambda: # Available since v0.26.0
41+
# # set 128MB of RAM
42+
# # See lambda configuration docs here:
43+
# # https://docs.aws.amazon.com/lambda/latest/dg/configuration-function-common.html#configuration-memory-console
44+
# memory: 128
45+
# # set a timeout of 15 seconds
46+
# # See lambda timeout values here:
47+
# # https://docs.aws.amazon.com/lambda/latest/dg/configuration-function-common.html#configuration-timeout-console
48+
# timeout: 15
49+
# # set a provisioned concurrency value
50+
# # For info on provisioned concurrency for AWS Lambda see:
51+
# # https://docs.aws.amazon.com/lambda/latest/dg/configuration-concurrency.html
52+
# provisioned-concurrency: 0
53+
# # Configure VPCs that the lambda can access
54+
# vpc:
55+
# # Array of existing security group ids to apply
56+
# security-group-ids:
57+
# - sg-xxx
58+
# # Array of existing subnet ids to apply
59+
# subnet-ids:
60+
# - subnet-xxx
61+
# # Additional deployment types
62+
# # You can target these types by setting a `type` in your project configuration
63+
# big-service:
64+
# telemetry: 0
65+
# lambda:
66+
# memory: 1024
67+
# timeout: 60
68+
# provisioned-concurrency: 1

v1/url-shortener/nitric.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name: url-shortener-5
2+
services:
3+
- match: services/*
4+
runtime: go
5+
type: ""
6+
start: go run ./$SERVICE_PATH/...
7+
runtimes:
8+
go:
9+
dockerfile: ./golang.dockerfile
10+
args: {}

v1/url-shortener/resources/main.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package resources
2+
3+
import (
4+
"sync"
5+
6+
"github.com/nitrictech/go-sdk/nitric"
7+
)
8+
9+
type Resource struct {
10+
MainApi nitric.Api
11+
UrlKvStore nitric.KvStore
12+
NotifyTopic nitric.SubscribableTopic
13+
}
14+
15+
var (
16+
resource *Resource
17+
resourceOnce sync.Once
18+
)
19+
20+
func Get() *Resource {
21+
resourceOnce.Do(func() {
22+
mainApi, err := nitric.NewApi("main")
23+
if err != nil {
24+
panic(err)
25+
}
26+
27+
resource = &Resource{
28+
MainApi: mainApi,
29+
UrlKvStore: nitric.NewKv("urls"),
30+
NotifyTopic: nitric.NewTopic("notify"),
31+
}
32+
})
33+
34+
return resource
35+
}

0 commit comments

Comments
 (0)