diff --git a/v1/multi-language/README.md b/v1/multi-language/README.md new file mode 100644 index 0000000..848403f --- /dev/null +++ b/v1/multi-language/README.md @@ -0,0 +1,40 @@ +

+ +## Nitric Multi-Language Example + +An example of a polyglot nitric mono repo written in Typescript and Golang, that demonstrates interservice communication via Topics. + +![Architecture Diagram](architecture.png) + +## Running this project + +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. + +You'll also want to make sure the project's required dependencies have been installed for the golang and typescript projects. + +```bash +# install node dependencies +cd ts +npm install +``` + +```bash +# install go dependencies +cd go +go mod tidy +``` + +```bash +# run locally (from project root) +nitric start +``` + +## Learn more about Nitric + +Nitric provides detailed and intuitive [documentation](https://nitric.io/docs) and [guides](https://nitric.io/docs/getting-started) to help you get started quickly. + +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). + + + + diff --git a/v1/multi-language/architecture.png b/v1/multi-language/architecture.png new file mode 100644 index 0000000..918132d Binary files /dev/null and b/v1/multi-language/architecture.png differ diff --git a/v1/multi-language/go/.gitignore b/v1/multi-language/go/.gitignore new file mode 100644 index 0000000..380c102 --- /dev/null +++ b/v1/multi-language/go/.gitignore @@ -0,0 +1,17 @@ +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# 'nitric run' log directory +.nitric/ + +git.store \ No newline at end of file diff --git a/v1/multi-language/go/README.md b/v1/multi-language/go/README.md new file mode 100644 index 0000000..b274c2c --- /dev/null +++ b/v1/multi-language/go/README.md @@ -0,0 +1,36 @@ +

+ +## About Nitric + +> ⚠️ Note: This repo contains features that require a minimum nitric CLI version of [1.52.0](https://github.com/nitrictech/cli/releases/tag/v1.52.0) + +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. + +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. + +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. + +Nitric makes it easy to: + +- Create smart [serverless functions and APIs](https://nitric.io/docs/apis) +- Build reliable distributed apps that use [events](https://nitric.io/docs/messaging/topics) and/or [queues](https://nitric.io/docs/messaging/queues) +- Securely store, retrieve and rotate [secrets](https://nitric.io/docs/secrets) +- Read and write files from [buckets](https://nitric.io/docs/storage) + +## Learning Nitric + +Nitric provides detailed and intuitive [documentation](https://nitric.io/docs) and [guides](https://nitric.io/docs/getting-started) to help you get started quickly. + +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). + +## Running this project + +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. + +```bash +# install dependencies +go mod tidy + +# run locally +nitric start +``` diff --git a/v1/multi-language/go/go.mod b/v1/multi-language/go/go.mod new file mode 100644 index 0000000..90eee69 --- /dev/null +++ b/v1/multi-language/go/go.mod @@ -0,0 +1,19 @@ +module github.com/nitrictech/templates/go-starter + +go 1.21 + +toolchain go1.21.4 + +require github.com/nitrictech/go-sdk v1.0.7 + +require ( + github.com/missionMeteora/toolkit v0.0.0-20170713173850-88364e3ef8cc // indirect + github.com/nitrictech/nitric/core v0.0.0-20240614052744-71e6e05bc47e // indirect + github.com/nitrictech/protoutils v0.0.0-20220321044654-02667a814cdf // indirect + golang.org/x/net v0.26.0 // indirect + golang.org/x/sys v0.21.0 // indirect + golang.org/x/text v0.16.0 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20240624140628-dc46fd24d27d // indirect + google.golang.org/grpc v1.64.0 // indirect + google.golang.org/protobuf v1.34.2 // indirect +) diff --git a/v1/multi-language/go/go.sum b/v1/multi-language/go/go.sum new file mode 100644 index 0000000..aa6f4a5 --- /dev/null +++ b/v1/multi-language/go/go.sum @@ -0,0 +1,42 @@ +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/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= +github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw= +github.com/golang/mock v1.7.0-rc.1 h1:YojYx61/OLFsiv6Rw1Z96LpldJIy31o+UHmwAUMJ6/U= +github.com/golang/mock v1.7.0-rc.1/go.mod h1:s42URUywIqd+OcERslBJvOjepvNymP31m3q8d/GkuRs= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/missionMeteora/toolkit v0.0.0-20170713173850-88364e3ef8cc h1:/oFlKiuu6L1sIvZ7A363qMhNM+DUQL5WsVe1xIRQnFU= +github.com/missionMeteora/toolkit v0.0.0-20170713173850-88364e3ef8cc/go.mod h1:AtX+JBtXbQ+taj82QFzCSgN5EzM4Bi0YRyS+TVbjENs= +github.com/nitrictech/go-sdk v1.0.7 h1:SQsy522EhKCIc6NIHUMqDA4fDzD3gW0MHqABg1F840c= +github.com/nitrictech/go-sdk v1.0.7/go.mod h1:QmLPatYHvbQ+8ajVDxHplgQIyWWP9SzMXDL+PI/gL2E= +github.com/nitrictech/nitric/core v0.0.0-20240614052744-71e6e05bc47e h1:WV7BHlv3V25mKQuyPJ97W4aWCJL8h7MGSa4imTFqZcA= +github.com/nitrictech/nitric/core v0.0.0-20240614052744-71e6e05bc47e/go.mod h1:atiPRiYDBs4Wx3flZ45KnQenUD6eAdpL0lOLZT91yyM= +github.com/nitrictech/protoutils v0.0.0-20220321044654-02667a814cdf h1:8MB8W8ylM8sCM2COGfiO39/tB6BTdiawLszaUGCNL5w= +github.com/nitrictech/protoutils v0.0.0-20220321044654-02667a814cdf/go.mod h1:b2lzk2a4o1bvSrSCE6yvTldHuXCJymuDVhdMJGOSslw= +github.com/nxadm/tail v1.4.11 h1:8feyoE3OzPrcshW5/MJ4sGESc5cqmGkGCWlco4l0bqY= +github.com/nxadm/tail v1.4.11/go.mod h1:OTaG3NK980DZzxbRq6lEuzgU+mug70nY11sMd4JXXHc= +github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= +github.com/onsi/ginkgo v1.16.5/go.mod h1:+E8gABHa3K6zRBolWtd+ROzc/U5bkGt0FwiG042wbpU= +github.com/onsi/gomega v1.28.1 h1:MijcGUbfYuznzK/5R4CPNoUP/9Xvuo20sXfEm6XxoTA= +github.com/onsi/gomega v1.28.1/go.mod h1:9sxs+SwGrKI0+PWe4Fxa9tFQQBG5xSsSbMXOI8PPpoQ= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ= +golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE= +golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= +golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= +golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240624140628-dc46fd24d27d h1:k3zyW3BYYR30e8v3x0bTDdE9vpYFjZHK+HcyqkrppWk= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240624140628-dc46fd24d27d/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY= +google.golang.org/grpc v1.64.0 h1:KH3VH9y/MgNQg1dE7b3XfVK0GsPSIzJwdF617gUSbvY= +google.golang.org/grpc v1.64.0/go.mod h1:oxjF8E3FBnjp+/gVFYdWacaLDx9na1aqy9oovLpxQYg= +google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= +google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/v1/multi-language/go/golang.dockerfile b/v1/multi-language/go/golang.dockerfile new file mode 100644 index 0000000..49a9f38 --- /dev/null +++ b/v1/multi-language/go/golang.dockerfile @@ -0,0 +1,26 @@ + +FROM golang:alpine as build + +ARG HANDLER + +WORKDIR /app/ + +COPY go.mod *.sum ./ + +RUN go mod download + +COPY . . + +# Build the Go App from the provided HANDLER (this will be based on matches in your nitric.yaml fle) +RUN go build -o /bin/main ./${HANDLER}/... + +FROM alpine + +COPY --from=build /bin/main /bin/main + +RUN chmod +x-rw /bin/main +RUN apk update && \ + apk add --no-cache tzdata ca-certificates && \ + update-ca-certificates + +ENTRYPOINT ["/bin/main"] \ No newline at end of file diff --git a/v1/multi-language/go/services/hello/main.go b/v1/multi-language/go/services/hello/main.go new file mode 100644 index 0000000..c072145 --- /dev/null +++ b/v1/multi-language/go/services/hello/main.go @@ -0,0 +1,46 @@ +package main + +import ( + "context" + "fmt" + + "github.com/nitrictech/go-sdk/handler" + "github.com/nitrictech/go-sdk/nitric" +) + +func main() { + helloTypescriptTopic, err := nitric.NewTopic("hello-typescript").Allow(nitric.TopicPublish) + if err != nil { + fmt.Println(err) + return + } + + helloGolangTopic := nitric.NewTopic("hello-golang") + + api, err := nitric.NewApi("main") + if err != nil { + fmt.Println(err) + return + } + + helloGolangTopic.Subscribe(func(ctx *handler.MessageContext, next handler.MessageHandler) (*handler.MessageContext, error) { + fmt.Println("Received message from Typescript") + return next(ctx) + }) + + api.Get("/hello-typescript", func(ctx *handler.HttpContext, next handler.HttpHandler) (*handler.HttpContext, error) { + ctx.Response.Body = []byte("Saying hello to typescript from Golang") + + helloTypescriptTopic.Publish(context.TODO(), map[string]interface{}{ + "message": "Hello from Golang", + }) + + return next(ctx) + }) + + if err := nitric.Run(); err != nil { + fmt.Println(err) + } + + fmt.Println("Service has started") +} diff --git a/v1/multi-language/nitric.yaml b/v1/multi-language/nitric.yaml new file mode 100644 index 0000000..3731fe7 --- /dev/null +++ b/v1/multi-language/nitric.yaml @@ -0,0 +1,13 @@ +name: multi-language +services: + - basedir: ./ts + match: services/*.ts + start: npm run dev:services $SERVICE_PATH + - basedir: ./go + match: services/* + runtime: go + start: go run ./$SERVICE_PATH/... +runtimes: + go: + dockerfile: ./go/golang.dockerfile + args: {} diff --git a/v1/multi-language/ts/.gitignore b/v1/multi-language/ts/.gitignore new file mode 100644 index 0000000..930f792 --- /dev/null +++ b/v1/multi-language/ts/.gitignore @@ -0,0 +1,4 @@ +node_modules/ +.nitric/ +.npmrc +git.store \ No newline at end of file diff --git a/v1/multi-language/ts/package.json b/v1/multi-language/ts/package.json new file mode 100644 index 0000000..3498710 --- /dev/null +++ b/v1/multi-language/ts/package.json @@ -0,0 +1,19 @@ +{ + "name": "typescript-starter", + "version": "1.0.0", + "description": "nitric typescript starter template", + "private": true, + "dependencies": { + "@nitric/sdk": "^1.2.2" + }, + "devDependencies": { + "@types/node": "^22.1.0", + "dotenv": "^16.4.5", + "nodemon": "^3.1.4", + "ts-node": "^10.9.2", + "typescript": "^5.5.4" + }, + "scripts": { + "dev:services": "nodemon -r dotenv/config" + } +} diff --git a/v1/multi-language/ts/services/hello.ts b/v1/multi-language/ts/services/hello.ts new file mode 100644 index 0000000..32be0cc --- /dev/null +++ b/v1/multi-language/ts/services/hello.ts @@ -0,0 +1,16 @@ +import * as nitric from "@nitric/sdk"; + +const api = nitric.api("main"); +const helloTypescriptTopic = nitric.topic("hello-typescript"); +const helloGolangTopic = nitric.topic("hello-golang").allow("publish"); + +api.get("/hello-golang", async (ctx) => { + // Publish a message to the Golang topic + await helloGolangTopic.publish({message: "Hello from Typescript"}); + + ctx.res.body = "Saying hello to Golang from Typescript"; +}) + +helloTypescriptTopic.subscribe(async () => { + console.log("Received a message from Golang"); +}); diff --git a/v1/multi-language/ts/tsconfig.json b/v1/multi-language/ts/tsconfig.json new file mode 100644 index 0000000..71ef296 --- /dev/null +++ b/v1/multi-language/ts/tsconfig.json @@ -0,0 +1,7 @@ +{ + "compilerOptions": { + "esModuleInterop": true, + "target": "ESNext", + "moduleResolution": "node" + } + }