Skip to content

Commit c9d8716

Browse files
committed
feat: add sample app
add sample app for traffic add sample app for traffic
1 parent 25e85ad commit c9d8716

12 files changed

+2058
-2
lines changed

.gitignore

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,135 @@ go.work.sum
2323

2424
# env file
2525
.env
26+
27+
# Logs
28+
logs
29+
*.log
30+
npm-debug.log*
31+
yarn-debug.log*
32+
yarn-error.log*
33+
lerna-debug.log*
34+
.pnpm-debug.log*
35+
36+
# Diagnostic reports (https://nodejs.org/api/report.html)
37+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
38+
39+
# Runtime data
40+
pids
41+
*.pid
42+
*.seed
43+
*.pid.lock
44+
45+
# Directory for instrumented libs generated by jscoverage/JSCover
46+
lib-cov
47+
48+
# Coverage directory used by tools like istanbul
49+
coverage
50+
*.lcov
51+
52+
# nyc test coverage
53+
.nyc_output
54+
55+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
56+
.grunt
57+
58+
# Bower dependency directory (https://bower.io/)
59+
bower_components
60+
61+
# node-waf configuration
62+
.lock-wscript
63+
64+
# Compiled binary addons (https://nodejs.org/api/addons.html)
65+
build/Release
66+
67+
# Dependency directories
68+
node_modules/
69+
jspm_packages/
70+
71+
# Snowpack dependency directory (https://snowpack.dev/)
72+
web_modules/
73+
74+
# TypeScript cache
75+
*.tsbuildinfo
76+
77+
# Optional npm cache directory
78+
.npm
79+
80+
# Optional eslint cache
81+
.eslintcache
82+
83+
# Optional stylelint cache
84+
.stylelintcache
85+
86+
# Microbundle cache
87+
.rpt2_cache/
88+
.rts2_cache_cjs/
89+
.rts2_cache_es/
90+
.rts2_cache_umd/
91+
92+
# Optional REPL history
93+
.node_repl_history
94+
95+
# Output of 'npm pack'
96+
*.tgz
97+
98+
# Yarn Integrity file
99+
.yarn-integrity
100+
101+
# dotenv environment variable files
102+
.env
103+
.env.development.local
104+
.env.test.local
105+
.env.production.local
106+
.env.local
107+
108+
# parcel-bundler cache (https://parceljs.org/)
109+
.cache
110+
.parcel-cache
111+
112+
# Next.js build output
113+
.next
114+
out
115+
116+
# Nuxt.js build / generate output
117+
.nuxt
118+
dist
119+
120+
# Gatsby files
121+
.cache/
122+
# Comment in the public line in if your project uses Gatsby and not Next.js
123+
# https://nextjs.org/blog/next-9-1#public-directory-support
124+
# public
125+
126+
# vuepress build output
127+
.vuepress/dist
128+
129+
# vuepress v2.x temp and cache directory
130+
.temp
131+
.cache
132+
133+
# Docusaurus cache and generated files
134+
.docusaurus
135+
136+
# Serverless directories
137+
.serverless/
138+
139+
# FuseBox cache
140+
.fusebox/
141+
142+
# DynamoDB Local files
143+
.dynamodb/
144+
145+
# TernJS port file
146+
.tern-port
147+
148+
# Stores VSCode versions used for testing VSCode extensions
149+
.vscode-test
150+
151+
# yarn v2
152+
.yarn/cache
153+
.yarn/unplugged
154+
.yarn/build-state.yml
155+
.yarn/install-state.gz
156+
.pnp.*
157+
./app/node_modules

README.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,40 @@ services:
107107
2. use docker compose command to start
108108
```shell
109109
docker compose up -d
110-
```
110+
```
111+
112+
## add web app connect with mongodb
113+
114+
This app will directly load data from mongodb
115+
116+
```shell
117+
mkdir app
118+
```
119+
120+
[server.js](./app/server.js)
121+
122+
## target
123+
124+
![deploy-app-with-mongo](deploy-app-with-mongo.png)
125+
126+
## add new container setup on docker-compose.yml
127+
128+
```yaml
129+
app:
130+
build:
131+
context: .
132+
dockerfile: ./app/Dockerfile
133+
target: prod
134+
container_name: web-app
135+
image: web-app
136+
networks:
137+
- mongo-network
138+
environment:
139+
MONGO_DB_USERNAME: admin
140+
MONGO_DB_PWD: supersecret
141+
ports:
142+
- 3000:3000
143+
depends_on:
144+
mongodb:
145+
condition: service_healthy
146+
```

app/.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
README.md
2+
Dockerfile

app/.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
20.10.0

app/Dockerfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM node:20.10.0 AS base
2+
RUN mkdir /app
3+
WORKDIR /app
4+
COPY ./app /app
5+
RUN npm i -g pnpm
6+
RUN pnpm install --prod --frozen-lockfile
7+
FROM node:20.10.0-alpine AS prod
8+
RUN mkdir /app
9+
RUN mkdir -p /app/node_modules
10+
WORKDIR /app
11+
USER node
12+
COPY --from=base /app/server.js /app/index.html /app/
13+
COPY --from=base /app/node_modules /app/node_modules
14+
ENTRYPOINT [ "node", "server.js" ]

app/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# app
2+
3+
This is the web app that will directly load data from mongodb
4+
5+
## architecture
6+
7+
This app divided into following parts:
8+
9+
1. web client part
10+
11+
2. api server part
12+
13+
## description
14+
15+
* for web client part:
16+
17+
1. html for render ui
18+
19+
2. js for load data from api server
20+
21+
* for api server part:
22+
23+
logic for listne web request
24+
25+
logic for loaded data from mongodb
26+
27+
28+

app/index.html

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Mongo db loader</title>
7+
<style>
8+
.container {
9+
margin: 40px auto;
10+
width: 80%;
11+
}
12+
hr {
13+
width: 400px;
14+
margin-left: 0;
15+
}
16+
h3 {
17+
display: inline-block;
18+
}
19+
#static {
20+
color: red;
21+
}
22+
#dynamic {
23+
color: green;
24+
}
25+
</style>
26+
<script>
27+
(async function init(){
28+
const cont = document.getElementById('container');
29+
const response = await fetch('http://localhost:3000/fetch-data', {
30+
method: 'GET',
31+
headers: {
32+
'Accept': 'application/json',
33+
'Content-Type': 'application/json'
34+
}
35+
});
36+
const jsonResponse = await response.json();
37+
document.getElementById('dynamic').textContent = jsonResponse.data;
38+
})()
39+
</script>
40+
</head>
41+
<body>
42+
<div class="container" id="container">
43+
<h1>Welcome to this Super sophisticated App</h1>
44+
<span>Some static data:</span><h3 id="static">this is hard-coded in index.html</h3>
45+
<hr/>
46+
<span>Some dynamic data:</span><h3 id="dynamic"></h3>
47+
<hr/>
48+
</div>
49+
</body>
50+
</html>

app/package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "deploying-with-docker",
3+
"version": "1.0.0",
4+
"description": "app with mongo",
5+
"main": "server.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"start": "node server.js"
9+
},
10+
"keywords": [],
11+
"author": "gson liang",
12+
"license": "ISC",
13+
"dependencies": {
14+
"body-parser": "^1.20.2",
15+
"express": "^4.19.2",
16+
"mongodb": "^4.16.0"
17+
}
18+
}

0 commit comments

Comments
 (0)