Skip to content

Commit

Permalink
Enhance workflow (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
raulfdm authored Dec 16, 2023
1 parent a59e358 commit 616ba1d
Show file tree
Hide file tree
Showing 14 changed files with 79 additions and 51 deletions.
24 changes: 15 additions & 9 deletions apps/api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# It needs to be node. Prisma still depends on something from node
# to generate the client.
FROM node:slim as base
FROM node:20-slim as base

# Install OpenSSL. Required for prisma to work.
RUN apt-get update -y && apt-get install -y openssl
Expand All @@ -13,10 +13,14 @@ RUN npm install -g [email protected]
FROM base as installer
# install with devDependencies to be used later
RUN mkdir -p /temp/dev

COPY package.json bun.lockb /temp/dev/
COPY /apps/api/package.json /temp/dev/apps/api/package.json
COPY /apps/website/package.json /temp/dev/apps/website/package.json
RUN cd /temp/dev && bun install --frozen-lockfile --ignore-scripts

RUN --mount=type=cache,id=buncache,target=/root/.bun/install/cache\
cd /temp/dev &&\
bun install --frozen-lockfile --ignore-scripts

# install with --production (exclude devDependencies)
RUN mkdir -p /temp/prod
Expand All @@ -25,30 +29,32 @@ COPY /apps/api/package.json /temp/prod/apps/api/package.json
# We need to include the website package.json because otherwise it will throw
# an error due to a difference between lockfiles
COPY /apps/website/package.json /temp/prod/apps/website/package.json
RUN cd /temp/prod && bun install --frozen-lockfile --production --ignore-scripts
RUN --mount=type=cache,id=buncache,target=/root/.bun/install/cache\
cd /temp/prod &&\
bun install --frozen-lockfile --ignore-scripts --production

# ------------------------------ #
FROM base as prerelease
ENV NODE_ENV=production

COPY --from=installer /temp/dev/node_modules node_modules

COPY . .

RUN bun run build --filter=taco-api
RUN cd ./apps/api && bunx prisma generate

# ------------------------------ #
FROM base AS release

ENV NODE_ENV=production

COPY --from=installer /temp/prod/node_modules /app/node_modules
# Includes the prisma client generated
COPY --from=prerelease /app/node_modules/.prisma /app/node_modules/.prisma
# Includes the database and schema
COPY --from=prerelease /app/apps/api/src/infrastructure/prisma /app/src/infrastructure/prisma
COPY --from=prerelease /app/apps/api/dist/app.js .
COPY --from=prerelease /app/apps/api/prisma /app/prisma
COPY --from=prerelease /app/apps/api/tsconfig.json /app/tsconfig.json
COPY --from=prerelease /app/apps/api/dist /app/dist
COPY --from=prerelease /app/apps/api/package.json .

# run the app
EXPOSE 4000
ENTRYPOINT [ "bun", "run", "app.js" ]
ENTRYPOINT bun run start
7 changes: 4 additions & 3 deletions apps/api/Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# It needs to be node. Prisma still depends on something from node
# to generate the client.
FROM node:slim as base
FROM node:20-slim as base

# Install OpenSSL. Required for prisma to work.
RUN apt-get update -y && apt-get install -y openssl
Expand All @@ -13,10 +13,11 @@ RUN npm install -g [email protected]
COPY package.json bun.lockb ./
COPY /apps/api/package.json /app/apps/api/package.json
COPY /apps/website/package.json /app/apps/website/package.json
RUN bun install --ignore-scripts

RUN --mount=type=cache,id=buncache,target=/root/.bun/install/cache\
bun install --frozen-lockfile --ignore-scripts

COPY . .

RUN cd ./apps/api && bunx prisma generate
EXPOSE 4000
ENTRYPOINT bun run dev --filter=taco-api
7 changes: 3 additions & 4 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
"lint": "biome lint src",
"lint:ci": "biome ci src",
"db:migrate": "NODE_ENV=development prisma migrate dev",
"db:generate": "NODE_ENV=development prisma generate",
"db:generate": "prisma generate",
"db:seed": "NODE_ENV=development prisma db seed",
"db:reset": "NODE_ENV=development prisma migrate reset",
"prepare": "bun run db:generate"
"db:reset": "NODE_ENV=development prisma migrate reset"
},
"dependencies": {
"@elysiajs/cors": "0.7.2",
Expand All @@ -42,6 +41,6 @@
},
"prisma": {
"seed": "bun run ./src/infrastructure/seed/index.ts",
"schema": "./src/infrastructure/prisma/schema.prisma"
"schema": "./prisma/schema.prisma"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

generator client {
provider = "prisma-client-js"
output = "../dist/generated"
}

datasource db {
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion apps/api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,5 @@ new Elysia()
} as any),
)
.listen(env.PORT, ({ port }) => {
console.log(`🚀 Server ready at http://localhost:${port}`);
console.log(`🚀 Server ready at http://localhost:${port}/graphql`);
});
3 changes: 3 additions & 0 deletions apps/api/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
"paths": {
"@/*": [
"src/*"
],
"@prisma/client": [
"dist/generated"
]
},
"types": [
Expand Down
36 changes: 20 additions & 16 deletions apps/website/src/content/docs/en/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,21 @@ title: "Getting Started"

Before you start the initial setup, you need to have 2 runtimes installed:

- [NodeJS](https://nodejs.org/en) version 18 or higher
- [NodeJS](https://nodejs.org/en) version 20 or higher
- [Bun](https://bun.sh/) version 1.0.18 or higher

Although Bun is the primary runtime, one of the dependencies ([Prisma](https://www.prisma.io/)) still requires Node to generate the initial files.

:::tip

If you already have Node installed, you can install `bun` via npm:

```bash
npm install -g [email protected]
```

:::

## Configuration

You can either fork this project or clone it, the choice is yours.
Expand All @@ -31,30 +41,24 @@ This means that to run the server from the project root, you need to run:
bun run dev --filter=taco-api
```

Or, if you prefer, navigate to the API folder:

```bash
cd apps/api
```

And run the commands directly from the project directory:

```bash
bun run dev
```

In both cases, after running the `dev` command, the API will be running at `http://localhost:4000/graphql`.

## Running the Server with Docker

If you want a fully configured environment, it's also possible to run the project via Docker.

For this, at the project root, run the command:

```bash
docker compose -f ./apps/api/docker-compose.yml up --build
docker-compose -f ./apps/api/docker-compose.yml up
```

:::note
The docker-compose is configured to run the API in development mode. To deploy as a docker image, visit the section [Deploy > Docker](/deploy-docker)
:::

## Running the Documentation

If you want to edit the documentation, in the project's root directory, run the command:

```bash
bun run dev --filter=website
```
38 changes: 22 additions & 16 deletions apps/website/src/content/docs/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,21 @@ title: "Começando"

Antes de fazer a configuração inicial, você precisa ter 2 runtimes instalados:

- [NodeJS](https://nodejs.org/en) versão 18 ou superior
- [NodeJS](https://nodejs.org/en) versão 20 ou superior
- [Bun](https://bun.sh/) versão 1.0.18 ou superior

Apesar do Bun ser o runtime principal, uma das dependências ([Prisma](https://www.prisma.io/)) ainda precisa do Node para gerar os arquivos iniciais.

:::tip

Caso já tenha o Node instalado, você pode o `bun` via npm:

```bash
npm install -g [email protected]
```

:::

## Configuração

Você pode tanto fazer um fork deste projeto quanto cloná-lo, fica a seu critério.
Expand All @@ -31,30 +41,26 @@ Isso significa que para rodar o servidor através da raiz do projeto, você prec
bun run dev --filter=taco-api
```

Ou, se preferir, navegue até a pasta da API:

```bash
cd apps/api
```

E rode os comandos diretamente do diretório do projeto:

```bash
bun run dev
```

Em ambos os casos, depois de rodar o comando `dev`, a API estará rodando em `http://localhost:4000/graphql`.
Esse comando vai gerar os arquivos necessarios e iniciar o servidor HTTP da API no endereço [http://localhost:4000/graphql](http://localhost:4000/graphql)

## Rodando o servidor com Docker

Caso você queira um ambiente totalmente configurado, também é possível rodar o projeto via Docker.
Caso queira um ambiente totalmente configurado sem precisar instalar nada, é possível rodar o projeto via docker.

Para isso, na raiz do projeto, rode o comando:

```bash
docker compose -f ./apps/api/docker-compose.yml up --build
docker-compose -f ./apps/api/docker-compose.yml up
```

:::note
O docker-compose está configurado para rodar a API em modo de desenvolvimento. Para fazer o deploy como uma imagem docker, acesse a seção [Deploy > Docker](/deploy-docker)
:::

## Rodando a documentação

Caso queira editar a documentação, na raíz do projeto rode o comando:

```bash
bun run dev --filter=website
```
Binary file modified bun.lockb
Binary file not shown.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
"build": "turbo build",
"build:taco-image": "docker ",
"dev": "turbo dev",
"dev:docker": "docker-compose -f ./apps/api/docker-compose.yml up",
"start": "turbo start",
"lint": "turbo lint",
"lint:ci": "turbo lint:ci",
"db:generate": "turbo db:generate",
"image:build": "bun run scripts/build-image.ts",
"image:deploy": "bun run image:build && docker push raulfdm/taco-api"
},
Expand Down
10 changes: 8 additions & 2 deletions turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@
"$schema": "https://turbo.build/schema.json",
"pipeline": {
"build": {
"outputs": ["dist/**"]
"dependsOn": ["db:generate"],
"outputs": ["dist/app.js"],
"env": ["NODE_ENV=production"]
},
"db:generate": {},
"dev": {
"dependsOn": ["db:generate"],
"env": ["NODE_ENV=development"]
},
"dev": {},
"lint": {},
"lint:ci": {},
"start": {
Expand Down

0 comments on commit 616ba1d

Please sign in to comment.