Skip to content

Commit acb6528

Browse files
authored
Merge pull request #327 from DefangLabs/linda-nextjs-cv
Add Nextjs CV (from market.dev) sample
0 parents  commit acb6528

File tree

16 files changed

+311
-0
lines changed

16 files changed

+311
-0
lines changed

.devcontainer/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
FROM mcr.microsoft.com/devcontainers/typescript-node:22-bookworm

.devcontainer/devcontainer.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"build": {
3+
"dockerfile": "Dockerfile",
4+
"context": ".."
5+
},
6+
"features": {
7+
"ghcr.io/defanglabs/devcontainer-feature/defang-cli:1.0.4": {},
8+
"ghcr.io/devcontainers/features/docker-in-docker:2": {},
9+
"ghcr.io/devcontainers/features/aws-cli:1": {}
10+
}
11+
}

.github/workflows/deploy.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Deploy
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
deploy:
10+
environment: playground
11+
runs-on: ubuntu-latest
12+
permissions:
13+
contents: read
14+
id-token: write
15+
16+
steps:
17+
- name: Checkout Repo
18+
uses: actions/checkout@v4
19+
20+
- name: Deploy
21+
uses: DefangLabs/defang-github-action@v1.1.0

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Sveltekit & MongoDB
2+
3+
[![1-click-deploy](https://defang.io/deploy-with-defang.png)](https://portal.defang.dev/redirect?url=https%3A%2F%2Fgithub.com%2Fnew%3Ftemplate_name%3Dsample-sveltekit-mongodb-template%26template_owner%3DDefangSamples)
4+
5+
This is a project that demonstrate both client side component rendering and hydration as well as serverside rendering with external API route configuration. Furthermore, there is also a mongodb connection (not hosted on the atlas) to cache the queried results.
6+
7+
## NOTE
8+
9+
This sample showcases how you could deploy a full-stack application with Defang and Sveltekit. However, it deploys mongodb as a defang service. Defang [services](https://12factor.net/processes) are ephemeral and should not be used to run stateful workloads in production as they will be reset on every deployment. For production use cases you should use a managed database like RDS, Aiven, or others. In the future, Defang will help you provision and connect to managed databases.
10+
11+
## Essential Setup Files
12+
13+
1. Download [Defang CLI] (https://github.com/DefangLabs/defang)
14+
2. (optional) If you are using [Defang BYOC] (https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html) authenticated your AWS account.
15+
3. (optional for local development) [Docker CLI] (https://docs.docker.com/engine/install/)
16+
17+
## Prerequisite
18+
19+
1. Download [Defang CLI] (https://github.com/DefangLabs/defang)
20+
2. (optional) If you are using [Defang BYOC](https://docs.defang.io/docs/concepts/defang-byoc) make sure you have properly
21+
3. [Docker CLI] (https://docs.docker.com/engine/install/)
22+
23+
4. [NodeJS] (https://nodejs.org/en/download/package-manager)
24+
25+
## Development
26+
27+
For development, we use a local container. This can be seen in the compose.yaml and /src/routes/api/songs/+server.js file and the server.js file where we create a pool of connections. To run the sample locally after clonging the respository, you can run on docker by doing
28+
29+
1. docker compose up --build
30+
31+
## A Step-by-Step Guide
32+
33+
1. Open the terminal and type `defang login`
34+
2. Type `defang compose up` in the CLI
35+
3. Your app should be up and running with Defang in minutes!
36+
37+
---
38+
39+
Title: SvelteKit & MongoDB
40+
41+
Short Description: A full-stack application using SvelteKit for the frontend and MongoDB for the database.
42+
43+
Tags: SvelteKit, MongoDB, Full-stack, Node.js, JavaScript
44+
45+
Languages: nodejs

app/.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
engine-strict=true

app/Dockerfile

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Use the slim version of Node.js v20 on Debian Bookworm as the base image
2+
FROM node:20-bookworm-slim
3+
4+
RUN apt-get update -qq \
5+
&& apt-get install -y curl \
6+
&& apt-get clean \
7+
&& rm -rf /var/lib/apt/lists/*
8+
9+
# Set the working directory
10+
WORKDIR /app
11+
12+
# Copy package.json and package-lock.json
13+
COPY package*.json ./
14+
15+
# Install dependencies
16+
RUN npm install
17+
18+
# Copy the rest of the application code
19+
COPY . .
20+
21+
# Set environment variable for MongoDB URI
22+
ARG MONGODB_URI
23+
ENV MONGODB_URI=$MONGODB_URI
24+
25+
# Build the SvelteKit application
26+
RUN npm run build
27+
28+
# Expose the port the app runs on
29+
EXPOSE 3000
30+
31+
# Command to run the app
32+
CMD ["node", "build"]

app/package.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "sveltekit",
3+
"version": "0.0.1",
4+
"private": true,
5+
"scripts": {
6+
"dev": "vite dev",
7+
"build": "vite build",
8+
"preview": "vite preview"
9+
},
10+
"devDependencies": {
11+
"@sveltejs/adapter-auto": "^3.0.0",
12+
"@sveltejs/adapter-node": "^5.0.1",
13+
"@sveltejs/kit": "^2.5.8",
14+
"@sveltejs/vite-plugin-svelte": "^3.0.0",
15+
"svelte": "^4.2.7",
16+
"svelte-preprocess": "^5.1.4",
17+
"vite": "^5.2.11"
18+
},
19+
"type": "module",
20+
"dependencies": {
21+
"mongodb": "^6.6.1",
22+
"node-fetch": "^3.3.2"
23+
}
24+
}

app/src/app.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<link rel="icon" href="%sveltekit.assets%/favicon.png" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1" />
7+
%sveltekit.head%
8+
</head>
9+
<body data-sveltekit-preload-data="hover">
10+
<div style="display: contents">%sveltekit.body%</div>
11+
</body>
12+
</html>

app/src/lib/db.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { MongoClient } from 'mongodb';
2+
3+
const uri = process.env.MONGODB_URI || 'mongodb://localhost:27017/musicdb';
4+
const client = new MongoClient(uri);
5+
6+
let cachedClient = null;
7+
let cachedDb = null;
8+
9+
export async function connectToDatabase() {
10+
if (cachedClient && cachedDb) {
11+
return { client: cachedClient, db: cachedDb };
12+
}
13+
14+
// Connect the client
15+
await client.connect();
16+
const db = client.db(); // Connects to the specified database in the URI
17+
18+
// Ensure the collections exist (this step is optional because MongoDB will create collections on first use)
19+
await db.createCollection('songs').catch(() => {});
20+
await db.createCollection('searches').catch(() => {});
21+
22+
cachedClient = client;
23+
cachedDb = db;
24+
return { client: cachedClient, db: cachedDb };
25+
}

app/src/lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// place files you want to import through the `$lib` alias in this folder.

0 commit comments

Comments
 (0)