Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ When utilizing the `nitric new` command to initiate a new project, the available
| [nitric-koa](./v1/nitric-koa/) | A secure URL upload demonstrating usage with Nitric and KOA framework | Existing API Frameworks, Storage |
| [nitric-hono](./v1/nitric-hono/) | A secure URL upload demonstrating usage with Nitric and Hono | Existing API Frameworks, Storage |
| [nitric-fastify](./v1/nitric-fastify/) | A secure URL upload demonstrating usage with Nitric and fastify | Existing API Frameworks, Storage |
| [nitric-monorepo](./v1/nitric-monorepo/) | Several monorepo examples that contains a Next.js app, a nitric API and shared packages | APIs |
| [websockets](./v1/websockets/) | A basic websockets example | APIs, Key Value Stores |
| [real-time-chat](./v1/realtime-chat-app/) | A realtime chat using Websockets, Next.js, Nitric and Clerk Auth | APIs, WebSockets, Frontend, Auth |
| [scheduled-report](./v1/scheduled-report/) | A scheduled report generated with Google Sheets and shared with Google Drive | APIs, Schedules |
Expand Down
10 changes: 10 additions & 0 deletions v1/nitric-monorepo/with-berry/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true

[*.{js,json,yml}]
charset = utf-8
indent_style = space
indent_size = 2
4 changes: 4 additions & 0 deletions v1/nitric-monorepo/with-berry/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.yarn/** linguist-vendored
/.yarn/releases/* binary
/.yarn/plugins/**/* binary
/.pnp.* binary linguist-generated
16 changes: 16 additions & 0 deletions v1/nitric-monorepo/with-berry/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions

# Swap the comments on the following lines if you wish to use zero-installs
# In that case, don't forget to run `yarn config set enableGlobalCache false`!
# Documentation here: https://yarnpkg.com/features/caching#zero-installs

#!.yarn/cache
.pnp.*
node_modules

**/dist
894 changes: 894 additions & 0 deletions v1/nitric-monorepo/with-berry/.yarn/releases/yarn-berry.cjs

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions v1/nitric-monorepo/with-berry/.yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
yarnPath: ".yarn/releases/yarn-berry.cjs"
nodeLinker: node-modules
67 changes: 67 additions & 0 deletions v1/nitric-monorepo/with-berry/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<p align="center">
<a href="https://nitric.io">
<img src="https://raw.githubusercontent.com/nitrictech/nitric/main/docs/assets/nitric-logo.svg" width="120" alt="Nitric Logo"/>
</a>
</p>

<p align="center">
A fast & fun way to build portable cloud-native applications
</p>

<p align="center">
<img alt="GitHub release (latest SemVer)" src="https://img.shields.io/github/v/release/nitrictech/nitric?sort=semver">
<a href="https://twitter.com/nitric_io">
<img alt="Twitter Follow" src="https://img.shields.io/twitter/follow/nitric_io?label=Follow&style=social">
</a>
<a href="https://nitric.io/chat"><img alt="Discord" src="https://img.shields.io/discord/955259353043173427?label=discord"></a>
</p>

## Project Description

Creates a server app that contains an API, this app utilizes [yarn Berry workspaces](https://yarnpkg.com/features/workspaces) to share packages. A custom runtime is used to build the service image, see the [Dockerfile](./apps/server/Dockerfile) for details.

## Usage

### Step 1: Install Nitric

Follow the steps in the [installation guide](https://nitric.io/docs/installation)

### Step 2: Run your Nitric project locally

```bash
yarn install
```

```bash
cd apps/server
nitric start
```

## About Nitric

[Nitric](https://nitric.io) is a framework for rapid development of cloud-native and serverless applications. 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
- Build reliable distributed apps that use events and/or queues
- Securely store and retrieve secrets
- Read and write files from buckets

## Documentation

The full documentation is available at [nitric.io/docs](https://nitric.io/docs).

We're completely open-source and encourage [code contributions](https://nitric.io/docs/contributions).

## Get in touch

- Ask questions in [GitHub discussions](https://github.com/nitrictech/nitric/discussions)

- Find us on [Twitter](https://twitter.com/nitric_io)

- Send us an [email](mailto:maintainers@nitric.io)

- Jump into our [Discord server](https://nitric.io/chat)
6 changes: 6 additions & 0 deletions v1/nitric-monorepo/with-berry/apps/server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules/
.nitric/
.npmrc
git.store
lib
.env
36 changes: 36 additions & 0 deletions v1/nitric-monorepo/with-berry/apps/server/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
FROM node:alpine AS builder
ARG PACKAGE_SCOPE
ARG HANDLER

# Python and make are required by certain native package build processes in NPM packages.
RUN --mount=type=cache,sharing=locked,target=/etc/apk/cache \
apk --update-cache add git g++ make py3-pip

# # Set working directory
WORKDIR /app

# node_modules is ignored in .dockerignore
COPY . .

RUN yarn workspaces focus ${PACKAGE_SCOPE} --production

# Create a variable for the app path
ARG APP_PATH=/app/apps/${PACKAGE_SCOPE}

# Build the app using ncc, and exclude the prisma client for prisma support https://github.com/prisma/prisma/issues/16901#issuecomment-1362940774
RUN yarn dlx -p @vercel/ncc -p typescript ncc build ${APP_PATH}/${HANDLER} -m --v8-cache -o ${APP_PATH}/lib/ -e .prisma/client -e @prisma/client -t

FROM node:alpine AS runner
ARG PACKAGE_SCOPE
WORKDIR /app

RUN apk update && \
apk add --no-cache ca-certificates && \
update-ca-certificates

COPY --from=builder /app/apps/${PACKAGE_SCOPE}/lib .

# prisma fix for docker installs: https://github.com/prisma/docs/issues/4365
RUN test -d ./prisma && yarn dlx -p prisma prisma generate || echo "";

ENTRYPOINT ["node", "index.js"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/node_modules/
13 changes: 13 additions & 0 deletions v1/nitric-monorepo/with-berry/apps/server/nitric.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: server
services:
- match: services/*.ts
runtime: custom
type: ""
start: yarn dev:services $SERVICE_PATH
runtimes:
custom:
# All services that specify the 'custom' runtime will be built using this dockerfile
dockerfile: ./Dockerfile
context: ../../
args:
PACKAGE_SCOPE: "server"
19 changes: 19 additions & 0 deletions v1/nitric-monorepo/with-berry/apps/server/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "server",
"version": "0.0.0",
"private": true,
"scripts": {
"dev:services": "nodemon -r dotenv/config"
},
"dependencies": {
"@nitric/sdk": "^1.2.1",
"@repo/cors": "workspace:*",
"@repo/logger": "workspace:*"
},
"devDependencies": {
"@types/node": "^20.11.24",
"dotenv": "^16.4.5",
"nodemon": "^3.1.4",
"typescript": "^5.5.3"
}
}
20 changes: 20 additions & 0 deletions v1/nitric-monorepo/with-berry/apps/server/services/hello.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { api } from "@nitric/sdk";
import { cors } from "@repo/cors";
import { log } from "@repo/logger";

// Define a new service called 'hello'
const main = api("main", {
middleware: [cors],
});

main.get("/message/:name", async (ctx) => {
const { name } = ctx.req.params;

log(`Received request for ${name}`);

return ctx.res.json({ message: `hello ${name}` });
});

main.get("/status", async (ctx) => {
return ctx.res.json({ ok: true });
});
7 changes: 7 additions & 0 deletions v1/nitric-monorepo/with-berry/apps/server/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"esModuleInterop": true,
"target": "ESNext",
"moduleResolution": "node"
}
}
8 changes: 8 additions & 0 deletions v1/nitric-monorepo/with-berry/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "with-berry",
"packageManager": "yarn@4.3.1",
"workspaces": [
"apps/*",
"packages/*"
]
}
13 changes: 13 additions & 0 deletions v1/nitric-monorepo/with-berry/packages/cors/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "@repo/cors",
"version": "0.0.0",
"private": true,
"main": "./src/index.ts",
"dependencies": {
"@nitric/sdk": "^1.2.1"
},
"devDependencies": {
"@repo/tsconfig": "workspace:*",
"typescript": "^5.5.3"
}
}
26 changes: 26 additions & 0 deletions v1/nitric-monorepo/with-berry/packages/cors/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { HttpMiddleware } from "@nitric/sdk";

export const optionsHandler: HttpMiddleware = (ctx) => {
ctx.res.headers["Content-Type"] = ["text/html; charset=ascii"];
ctx.res.status = 200;
ctx.res.body = "OK";

return ctx;
};

export const cors: HttpMiddleware = (ctx, next) => {
ctx.res.headers["Access-Control-Allow-Origin"] = ["*"];
ctx.res.headers["Access-Control-Allow-Headers"] = [
"Origin, Content-Type, Accept, Authorization",
];
ctx.res.headers["Access-Control-Allow-Methods"] = [
"GET, PUT, POST, OPTIONS, DELETE",
];
ctx.res.headers["Access-Control-Allow-Credentials"] = ["true"];

if (next) {
return next(ctx);
}

return ctx;
};
9 changes: 9 additions & 0 deletions v1/nitric-monorepo/with-berry/packages/cors/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "@repo/tsconfig/base.json",
"compilerOptions": {
"outDir": "./dist",
"rootDir": "./src"
},
"include": ["src"],
"exclude": ["dist", "build", "node_modules"]
}
10 changes: 10 additions & 0 deletions v1/nitric-monorepo/with-berry/packages/logger/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "@repo/logger",
"version": "0.0.0",
"private": true,
"main": "./src/index.ts",
"devDependencies": {
"@repo/tsconfig": "workspace:*",
"typescript": "^5.5.3"
}
}
3 changes: 3 additions & 0 deletions v1/nitric-monorepo/with-berry/packages/logger/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const log = (message: string) => {
console.log(`[LOG] ${message}`);
};
9 changes: 9 additions & 0 deletions v1/nitric-monorepo/with-berry/packages/logger/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "@repo/tsconfig/base.json",
"compilerOptions": {
"outDir": "./dist",
"rootDir": "./src"
},
"include": ["src"],
"exclude": ["dist", "build", "node_modules"]
}
21 changes: 21 additions & 0 deletions v1/nitric-monorepo/with-berry/packages/tsconfig/base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"display": "Default",
"compilerOptions": {
"composite": false,
"declaration": true,
"declarationMap": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"inlineSources": false,
"isolatedModules": true,
"module": "NodeNext",
"moduleResolution": "NodeNext",
"noUnusedLocals": false,
"noUnusedParameters": false,
"preserveWatchOutput": true,
"skipLibCheck": true,
"strict": true
},
"exclude": ["node_modules"]
}
5 changes: 5 additions & 0 deletions v1/nitric-monorepo/with-berry/packages/tsconfig/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "@repo/tsconfig",
"version": "0.0.0",
"private": true
}