Skip to content

Commit f19d9a9

Browse files
author
Stephen Hinett
committed
updates: to more dockerfiles and added an nginx proxy. Still not got the
frontend talking to backend yet tho
1 parent 2cb07fa commit f19d9a9

File tree

10 files changed

+138
-10
lines changed

10 files changed

+138
-10
lines changed

backend/docker-compose.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
services:
2-
api:
2+
backend:
33
container_name: api
44
build:
55
context: ./

backend/main.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"log"
56
"net/http"
67
)
78

9+
type response struct {
10+
Message string `json:"message"`
11+
}
12+
813
func main() {
914
mux := http.NewServeMux()
15+
1016
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
11-
w.Write([]byte("Hello, to the backend!"))
17+
w.Header().Set("Content-Type", "application/json")
18+
json.NewEncoder(w).Encode(response{Message: "Hello, to the backend!"})
19+
})
20+
21+
mux.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
22+
w.Header().Set("Content-Type", "application/json")
23+
json.NewEncoder(w).Encode(response{Message: "Hello, to the backend!"})
1224
})
1325

1426
log.Println("Starting server on :9000")

docker-compose.yml

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
services:
2+
nginx:
3+
container_name: proxy
4+
image: nginx:stable-alpine
5+
depends_on:
6+
- "client"
7+
- "api"
8+
volumes:
9+
- ./docker/nginx/config:/etc/nginx/conf.d/
10+
- ./docker/nginx/includes:/etc/nginx/includes/
11+
networks:
12+
- site-network
13+
14+
client:
15+
depends_on:
16+
- "api"
17+
extends:
18+
file: ./frontend/docker-compose.yml
19+
service: frontend
20+
networks:
21+
- site-network
22+
23+
api:
24+
extends:
25+
file: ./backend/docker-compose.yml
26+
service: backend
27+
networks:
28+
- site-network
29+
30+
networks:
31+
site-network:
32+
name: site-network
33+
driver: bridge

docker/nginx/config/default.conf

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
server {
2+
listen 8080;
3+
listen [::]:8080;
4+
server_name localhost;
5+
6+
# SSL configuration
7+
#
8+
# listen 443 ssl;
9+
# listen [::]:443 ssl;
10+
#
11+
# Note: You should disable gzip for SSL traffic.
12+
# See: https://bugs.debian.org/773332
13+
#
14+
# Read up on ssl_ciphers to ensure a secure configuration.
15+
# See: https://bugs.debian.org/765782
16+
#
17+
# Self signed certs generated by the ssl-cert package
18+
# Don't use them in a production server!
19+
#
20+
# include snippets/snakeoil.conf;
21+
22+
location / {
23+
root /usr/share/nginx/html;
24+
index index.html index.htm;
25+
include /etc/nginx/includes/proxy.conf;
26+
proxy_pass http://client:3000;
27+
}
28+
29+
location /api {
30+
include /etc/nginx/includes/proxy.conf;
31+
proxy_pass http://api:9000;
32+
}
33+
}

docker/nginx/includes/proxy.conf

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
proxy_set_header Host $host;
2+
proxy_set_header X-Real-IP $remote_addr;
3+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
4+
proxy_set_header X-Forwarded-Proto $scheme;
5+
proxy_buffering off;
6+
proxy_request_buffering off;
7+
proxy_http_version 1.1;
8+
proxy_intercept_errors on;

frontend/.gitkeep

Whitespace-only changes.

frontend/docker-compose.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ services:
55
context: ./
66
dockerfile: docker/Dockerfile.dev
77
ports:
8-
- "8080:8080"
8+
- "3000:3000"
99
- "33333:33333"
1010
volumes:
1111
- ./:/client
1212
- /client/node_modules/
1313
- /client/.parcel-cache/
14+
# command: [ "parcel", "ui/index.html -p 8080 --hmr-port 33333" ]
1415
command: [ "npm", "start" ]

frontend/docker/Dockerfile.dev

+3
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ COPY ./package.json ./package-lock.json ./
99
RUN npm install --slient
1010

1111
COPY . .
12+
13+
EXPOSE 33333
14+
# EXPOSE 8080

frontend/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"browserlist": "> 0.5%, last 2 versions, not dead",
66
"source": "ui/index.html",
77
"scripts": {
8-
"start": "parcel ui/index.html -p 8080 --hmr-port 33333",
8+
"start": "parcel ui/index.html -p 3000 --hmr-port=33333",
99
"build": "parcel build"
1010
},
1111
"dependencies": {

frontend/src/index.tsx

+44-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,46 @@
1-
import React from 'react'
2-
import ReactDOM from 'react-dom'
1+
import React, { useEffect, useState } from 'react'
32

4-
const App: React.FunctionComponent<{}> = () => (
5-
<h1>Hello, to the frontend</h1>
6-
)
3+
import { createRoot } from 'react-dom/client'
74

8-
ReactDOM.render(<App />, document.getElementById('root'))
5+
const container = document.getElementById('root');
6+
const root = createRoot(container!);
7+
8+
const App: React.FunctionComponent<{}> = () => {
9+
const [error, setError] = useState(null);
10+
const [isLoaded, setIsLoaded] = useState(false);
11+
const [items, setItems] = useState([]);
12+
13+
useEffect(() => {
14+
fetch("http://localhost:9000/api", {
15+
headers: {
16+
'Content-Type': 'application/json',
17+
'Accept': 'application/json'
18+
}
19+
}).then(
20+
res => res.json()
21+
).then(
22+
(result) => {
23+
console.log(result);
24+
},
25+
(err) => {
26+
setIsLoaded(true)
27+
setError(err)
28+
}
29+
)
30+
}, [])
31+
32+
if (error) {
33+
return <div>Error: {error.message}</div>;
34+
} else if (!isLoaded) {
35+
return <div>Loading...</div>;
36+
} else {
37+
return (
38+
<div className='some-element'>
39+
<h1>Hello, to the frontend!</h1>
40+
<p>{items.map(item => (item.name))}</p>
41+
</div>
42+
)
43+
}
44+
}
45+
46+
root.render(<App />)

0 commit comments

Comments
 (0)