Skip to content

Commit 51cdbfa

Browse files
committed
react sample
1 parent 846aa58 commit 51cdbfa

File tree

20 files changed

+4796
-0
lines changed

20 files changed

+4796
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Note: universal image is not compatible with the devcontainer tools.
2+
# we use the typescript-node image instead here but should switch out per
3+
# project requirements. You can choose a devcontainer that's appropriate
4+
# for your sample from the list below #REMOVE_ME_AFTER_EDITING
5+
6+
# FROM mcr.microsoft.com/devcontainers/go:1.22-bookworm
7+
# FROM mcr.microsoft.com/devcontainers/python:3.12-bookworm
8+
# FROM mcr.microsoft.com/devcontainers/php:8.3-bookworm
9+
# FROM mcr.microsoft.com/devcontainers/ruby:3.2-bookworm
10+
# FROM mcr.microsoft.com/devcontainers/java:11-bookworm
11+
# FROM mcr.microsoft.com/devcontainers/rust:1-bookworm
12+
FROM mcr.microsoft.com/devcontainers/typescript-node:22-bookworm
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+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Deploy
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
deploy:
10+
runs-on: ubuntu-latest
11+
permissions:
12+
contents: read
13+
id-token: write
14+
15+
steps:
16+
- name: Checkout Repo
17+
uses: actions/checkout@v4
18+
19+
- name: Deploy
20+
uses: DefangLabs/[email protected]

samples/react/README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# React
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-react-template%26template_owner%3DDefangSamples)
4+
5+
A minimal React app that uses Vite, running on Defang.
6+
7+
## Prerequisites
8+
9+
1. Download [Defang CLI](https://github.com/DefangLabs/defang)
10+
2. (Optional) If you are using [Defang BYOC](https://docs.defang.io/docs/concepts/defang-byoc) authenticate with your cloud provider account
11+
3. (Optional for local development) [Docker CLI](https://docs.docker.com/engine/install/)
12+
13+
## Development
14+
15+
To run the application locally, you can use the following command (make sure you've got `npm` and [Node.js](https://nodejs.org/en) installed):
16+
17+
```bash
18+
npm run dev
19+
```
20+
21+
To run it using a Docker container, you can use the following command:
22+
23+
```bash
24+
docker compose up --build
25+
```
26+
27+
## Deployment
28+
29+
> [!NOTE]
30+
> Download [Defang CLI](https://github.com/DefangLabs/defang)
31+
32+
### Defang Playground
33+
34+
Deploy your application to the Defang Playground by opening up your terminal and typing:
35+
```bash
36+
defang compose up
37+
```
38+
39+
### BYOC (AWS)
40+
41+
If you want to deploy to your own cloud account, you can use Defang BYOC:
42+
43+
1. [Authenticate your AWS account](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html), and check that you have properly set your environment variables like `AWS_PROFILE`, `AWS_REGION`, `AWS_ACCESS_KEY_ID`, and `AWS_SECRET_ACCESS_KEY`.
44+
2. Run in a terminal that has access to your AWS environment variables:
45+
```bash
46+
defang --provider=aws compose up
47+
```
48+
49+
---
50+
51+
Title: React
52+
53+
Short Description: A minimal React app running on Defang.
54+
55+
Tags: React, Vite, JavaScript, Frontend
56+
57+
Languages: JavaScript

samples/react/app/.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
npm-debug.log

samples/react/app/.gitignore

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
dist-ssr
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
.idea
19+
.DS_Store
20+
*.suo
21+
*.ntvs*
22+
*.njsproj
23+
*.sln
24+
*.sw?

samples/react/app/Dockerfile

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Build stage
2+
FROM node:18-alpine as build
3+
4+
# Set working directory to /app
5+
WORKDIR /app
6+
7+
# Copy package files
8+
COPY package*.json ./
9+
10+
# Install dependencies
11+
RUN npm ci
12+
13+
# Copy the rest of the application code
14+
COPY . .
15+
16+
# Build the app
17+
RUN npm run build
18+
19+
# Production stage
20+
FROM nginx:stable-alpine
21+
22+
# Copy the build output from the build stage
23+
COPY --from=build /app/dist /usr/share/nginx/html
24+
25+
# Copy custom Nginx configuration
26+
COPY nginx.conf /etc/nginx/conf.d/default.conf
27+
28+
# Expose port 5173
29+
EXPOSE 5173
30+
31+
# Start Nginx
32+
CMD ["nginx", "-g", "daemon off;"]

samples/react/app/eslint.config.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import js from '@eslint/js'
2+
import globals from 'globals'
3+
import react from 'eslint-plugin-react'
4+
import reactHooks from 'eslint-plugin-react-hooks'
5+
import reactRefresh from 'eslint-plugin-react-refresh'
6+
7+
export default [
8+
{ ignores: ['dist'] },
9+
{
10+
files: ['**/*.{js,jsx}'],
11+
languageOptions: {
12+
ecmaVersion: 2020,
13+
globals: globals.browser,
14+
parserOptions: {
15+
ecmaVersion: 'latest',
16+
ecmaFeatures: { jsx: true },
17+
sourceType: 'module',
18+
},
19+
},
20+
settings: { react: { version: '18.3' } },
21+
plugins: {
22+
react,
23+
'react-hooks': reactHooks,
24+
'react-refresh': reactRefresh,
25+
},
26+
rules: {
27+
...js.configs.recommended.rules,
28+
...react.configs.recommended.rules,
29+
...react.configs['jsx-runtime'].rules,
30+
...reactHooks.configs.recommended.rules,
31+
'react/jsx-no-target-blank': 'off',
32+
'react-refresh/only-export-components': [
33+
'warn',
34+
{ allowConstantExport: true },
35+
],
36+
},
37+
},
38+
]

samples/react/app/index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" type="image/svg+xml" href="/defang.svg" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>React App</title>
8+
</head>
9+
<body>
10+
<div id="root"></div>
11+
<script type="module" src="/src/main.jsx"></script>
12+
</body>
13+
</html>

samples/react/app/nginx.conf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
server {
2+
listen 5173;
3+
server_name localhost;
4+
root /usr/share/nginx/html;
5+
index index.html;
6+
7+
location / {
8+
try_files $uri $uri/ /index.html;
9+
}
10+
}

0 commit comments

Comments
 (0)