Skip to content

Commit cc522c8

Browse files
committed
Mass update, restructure all folders
1 parent 7981388 commit cc522c8

38 files changed

+111
-108
lines changed

.vscode/launch.json

-46
This file was deleted.

.vscode/settings.json

-2
This file was deleted.

.vscode/tasks.json

-15
This file was deleted.

ReadMe.md

+14-11
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
# Demo hệ thống Microservice
22

33
Hệ thống sẽ gồm có các container như sau:
4-
1. Nginx đóng vai trò reverse proxy: thầy Cường
5-
2. Node app cổng 8000 kết nối vào CSDL Mongo DB: cô Linh
6-
3. Golang app cổng 8001 kết nối vào CSDL Postgresql: chú Long
7-
4. ASP.net MVC Core cổng 8002 kết nối vào CSDL Microsoft SQL Server 2017: thầy Huy
4+
1. Nginx đóng vai trò reverse proxy, host: api_gateway
5+
2. Node.js REST, host api_customer, phục vụ ở cổng 8000, kết nối vào CSDL Mongo DB
6+
3. Golang REST, host api_book, phục vụ ở cổng 8001, kết nối vào CSDL Postgresql host go_db
7+
4. ASP.net MVC Core REST, host course,cổng 8002 kết nối vào CSDL Microsoft SQL Server 2017, host
88

99
```
1010
+-----------------+ +-------------+
1111
/customer/| | | |
12-
+---------> nodeapp:8000 +-----------> mongoDB |
12+
+---------> Node.js:8000 +-----------> mongoDB |
1313
| | | | |
1414
| +-----------------+ +-------------+
1515
|
1616
+-------------+ | +-----------------+ +-------------+
1717
| | |/book/ | | | |
18-
| Nginx Proxy +------------> goapp:8001 +-----------> PostgreSQL |
18+
| Nginx Proxy +------------> Golang:8001 +-----------> PostgreSQL |
1919
| | | | | | |
2020
+-------------+ | +-----------------+ +-------------+
2121
|
2222
| +-----------------+ +-------------+
2323
|/blog/ | | | |
24-
+---------> asp.net:8002 +-----------> MS-SQL2017 |
24+
+---------> Asp.net:8002 +-----------> MS-SQL2017 |
2525
| | | |
2626
+-----------------+ +-------------+
2727
X
@@ -36,13 +36,13 @@ Hệ thống sẽ gồm có các container như sau:
3636
Git Repo
3737
+
3838
|
39-
+---+nginx
39+
+---+gateway
4040
|
41-
+---+node
41+
+---+customer
4242
|
43-
+---+go
43+
+---+book
4444
|
45-
+---+net
45+
+---+course
4646
|
4747
+---+documents
4848
|
@@ -55,7 +55,10 @@ Git Repo
5555

5656
```
5757
git clone https://github.com/TechMaster/DemoMicroservice.git
58+
docker-compose up -d
5859
```
60+
Lệnh trên sẽ khởi động gateway và các REST service.
61+
5962
Kiểm tra NodeApp container chạy ok không thì vào thư mục node, chạy file build.sh nếu có dữ
6063
liệu trả về là ok
6164
```

go/ReadMe.md renamed to book/ReadMe.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Trong file docker_compose.yml tạo ra một Postgresql server có host name là
55
# Chạy thử
66
Kéo source code về, build và run bằng docker-compose:
77
```
8-
cd go/api_db
8+
cd book/api_book
99
docker-compose up -d
1010
curl http://localhost:8001/books
1111
```
@@ -16,7 +16,7 @@ curl http://localhost:8001/books
1616
- Bước 2: copy file binary biên dịch bước 1 vào container sử dụng image gọn hơn là alpine:latest
1717
Tham khảo [Docker multi stage build](https://docs.docker.com/engine/userguide/eng-image/multistage-build/)
1818
- Truyền tham số động vào container chứ không viết cứng nhắc trong file mã nguồn go.
19-
Đoạn code trong file go/api_db/api/vendor/database/init.go rất khó bảo trì
19+
Đoạn code trong file go/api_book/api/vendor/database/init.go rất khó bảo trì
2020
```go
2121
connectionParams := "dbname=" + dbname + " user=docker password=docker sslmode=disable host=go_db"
2222
```
File renamed without changes.

go/api_db/api/api.go renamed to book/api_book/api/api.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func indexPostHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Param
3838

3939
func indexHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
4040
setCors(w)
41-
fmt.Fprintf(w, "This is the api")
41+
fmt.Fprintf(w, "<h1>Book REST API by Golang</h1>")
4242
}
4343

4444
func setCors(w http.ResponseWriter) {
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

customer/index.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const express = require('express')
2+
const http = require('http')
3+
// Constants
4+
const PORT = 8000
5+
const HOST = '0.0.0.0'
6+
7+
// App
8+
const app = express()
9+
10+
app.use(express.static('public'))
11+
app.get('/', (req, res) => {
12+
res.send('<h1>Customer REST API by Node.js</h1>')
13+
})
14+
15+
app.get('/list', (req, res) => {
16+
res.json([{
17+
"first_name": "Giana",
18+
"last_name": "Stovine",
19+
"email": "[email protected]"
20+
}, {
21+
"first_name": "Corrianne",
22+
"last_name": "Pickin",
23+
"email": "[email protected]"
24+
}, {
25+
"first_name": "Alano",
26+
"last_name": "Pettiford",
27+
"email": "[email protected]"
28+
}, {
29+
"first_name": "Barton",
30+
"last_name": "Cotmore",
31+
"email": "[email protected]"
32+
}, {
33+
"first_name": "Elijah",
34+
"last_name": "Ivamy",
35+
"email": "[email protected]"
36+
}])
37+
})
38+
const server = http.createServer(app)
39+
server.listen(PORT, HOST, ()=> {
40+
41+
})
42+
console.log(`Running on http://${HOST}:${PORT}`)
File renamed without changes.
File renamed without changes.

docker-compose.yml

+34-7
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,48 @@ version: '3'
22
services:
33
nginx:
44
image: 'nginx:alpine'
5-
container_name: 'nginx'
5+
container_name: 'api_gateway'
66
depends_on:
7-
- nodeapp
7+
- api_customer
8+
- api_book
89
volumes:
9-
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
10+
- ./gateway/default.conf:/etc/nginx/conf.d/default.conf
11+
- ./gateway/index.html:/usr/share/nginx/html/index.html
1012
ports:
1113
- '80:80'
1214
networks:
1315
- proxy
14-
nodeapp:
15-
image: 'nodeapp'
16-
container_name: 'nodeapp'
16+
api_customer:
17+
container_name: 'api_customer'
18+
build:
19+
context: ./customer/
20+
image: api_customer:latest
1721
ports:
1822
- '8000:8000'
1923
networks:
2024
- proxy
25+
api_book: # Golang REST service
26+
container_name: 'api_book'
27+
build:
28+
context: ./book/api_book/api/
29+
image: api_book:latest
30+
depends_on:
31+
- go_db
32+
ports:
33+
- '8001:8001'
34+
networks:
35+
- proxy
36+
- db
37+
go_db:
38+
image: 'postgres:alpine'
39+
container_name: 'go_db'
40+
ports:
41+
- '5432'
42+
environment:
43+
- POSTGRES_USER=docker
44+
- POSTGRES_PASSWORD=docker
45+
networks:
46+
- db
2147
networks:
22-
proxy:
48+
proxy:
49+
db:
File renamed without changes.

nginx/default.conf renamed to gateway/default.conf

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ server {
88

99

1010
location /customer/ { #always at slash at the end
11-
proxy_pass http://nodeapp:8000/; #add slash after port
11+
proxy_pass http://api_customer:8000/; #add slash after port
1212
}
1313

1414
location /book/ {
15-
proxy_pass http://goapp:8001/;
15+
proxy_pass http://api_book:8001/;
1616
}
1717
}

gateway/index.html

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Demo Microservice</title>
6+
</head>
7+
<body>
8+
<h1>Nginx làm API Gate (reverse proxy) cho các REST API phía sau</h1>
9+
10+
<ul>
11+
<li><a href="http://localhost">Trang chủ</a></li>
12+
<li><a href="http://localhost/customer/list">localhost/customer/list</a> trỏ đến <a href="http://localhost:8000/list">localhost:8000/list</a></li>
13+
<li><a href="http://localhost/book/books">localhoost/book/books</a> trở đến <a href="http://localhost:8001/books">localhost:8001/books</a></li>
14+
</ul>
15+
</body>
16+
</html>

node/index.js

-22
This file was deleted.

0 commit comments

Comments
 (0)