Skip to content

Commit ff6f62c

Browse files
Update *
1 parent 3544119 commit ff6f62c

File tree

2 files changed

+209
-0
lines changed

2 files changed

+209
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
### Reverse Proxy
2+
3+
#### Traefik
4+
5+
Take a look at the [traefik](https://github.com/traefik/traefik) implementation:
6+
7+
```yaml
8+
services:
9+
secured-signal:
10+
image: ghcr.io/codeshelldev/secured-signal-api:latest
11+
container_name: secured-signal
12+
environment:
13+
API__URL: http://signal-api:8080
14+
SETTINGS__VARIABLES__RECIPIENTS:
15+
'[+123400002,+123400003,+123400004]'
16+
SETTINGS__VARIABLES__NUMBER: "+123400001"
17+
API__TOKENS: '[LOOOOOONG_STRING]'
18+
labels:
19+
- traefik.enable=true
20+
- traefik.http.routers.signal-api.rule=Host(`signal-api.mydomain.com`)
21+
- traefik.http.routers.signal-api.entrypoints=websecure
22+
- traefik.http.routers.signal-api.tls=true
23+
- traefik.http.routers.signal-api.tls.certresolver=cloudflare
24+
- traefik.http.routers.signal-api.service=signal-api-svc
25+
- traefik.http.services.signal-api-svc.loadbalancer.server.port=8880
26+
- traefik.docker.network=proxy
27+
restart: unless-stopped
28+
networks:
29+
proxy:
30+
backend:
31+
aliases:
32+
- secured-signal-api
33+
34+
networks:
35+
backend:
36+
proxy:
37+
external: true
38+
```
39+
40+
#### NGINX Proxy
41+
42+
This is the [NGINX](https://github.com/nginx/nginx) `docker-compose.yaml` file:
43+
44+
```yaml
45+
services:
46+
secured-signal:
47+
image: ghcr.io/codeshelldev/secured-signal-api:latest
48+
container_name: secured-signal-api
49+
environment:
50+
API__URL: http://signal-api:8080
51+
SETTINGS__VARIABLES__RECIPIENTS: "[+123400002,+123400003,+123400004]"
52+
SETTINGS__VARIABLES__NUMBER: "+123400001"
53+
API__TOKENS: "[LOOOOOONG_STRING]"
54+
restart: unless-stopped
55+
networks:
56+
backend:
57+
aliases:
58+
- secured-signal-api
59+
60+
nginx:
61+
image: nginx:latest
62+
container_name: secured-signal-proxy
63+
volumes:
64+
- ./nginx.conf:/etc/nginx/conf.d/default.conf
65+
# Load SSL certificates: cert.key, cert.crt
66+
- ./certs:/etc/nginx/ssl
67+
ports:
68+
- "443:443"
69+
- "80:80"
70+
restart: unless-stopped
71+
networks:
72+
frontend:
73+
backend:
74+
75+
networks:
76+
backend:
77+
frontend:
78+
```
79+
80+
Create a `nginx.conf` file in the `docker-compose.yaml` folder and mount it to `etc/nginx/conf.d/default.conf`:
81+
82+
```conf
83+
server {
84+
# Allow SSL on Port 443
85+
listen 443 ssl;
86+
87+
# Add allowed hostnames which nginx should respond to
88+
# `_` for any
89+
server_name localhost;
90+
91+
ssl_certificate /etc/nginx/ssl/cert.crt;
92+
ssl_certificate_key /etc/nginx/ssl/cert.key;
93+
94+
location / {
95+
# Use whatever network alias you set in the docker-compose file
96+
proxy_pass http://secured-signal-api:8880;
97+
proxy_set_header Host $host;
98+
proxy_set_header X-Real-IP $remote_addr;
99+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
100+
proxy_set_header X-Forwarded-Host $host;
101+
proxy_set_header X-Fowarded-Proto $scheme;
102+
}
103+
}
104+
105+
# Redirect HTTP to HTTPs
106+
server {
107+
listen 80;
108+
server_name localhost;
109+
return 301 https://$host$request_uri;
110+
}
111+
```
112+
113+
Lastly add your `cert.key` and `cert.crt` into your `certs/` folder and mount it to `/etc/nginx/ssl`.

replace-placeholders.sh

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/bin/bash
2+
3+
# Usage: ./replace_placeholders.sh source_file destination_file
4+
5+
template() {
6+
local content="$1"
7+
8+
# Extract unique placeholders: match { { file.xxx } } with arbitrary spaces
9+
local placeholders
10+
placeholders=$(printf '%s' "$content" | grep -oP '\{\s*{\s*file\.(.*)\s*}\s*}' | sed -E 's/\{\s*\{\s*file\.//;s/\s*\}\s*\}//' | sort -u)
11+
12+
for placeholder in $placeholders; do
13+
local file_content
14+
15+
if [[ -f "$placeholder" ]]; then
16+
file_content=$(<"$placeholder")
17+
18+
if [[ "$placeholder" == *.template.* ]]; then
19+
# Template further
20+
file_content=$(template "$file_content")
21+
fi
22+
else
23+
file_content="File not found: '$placeholder'."
24+
fi
25+
26+
# Escape special characters
27+
local escaped_content
28+
local escaped_placeholder
29+
escaped_content=$(printf '%s' "$file_content" | perl -pe 's/([\\\/\$])/\\$1/g; s/\n/\\n/g;')
30+
escaped_placeholder=$(printf '%s' "$placeholder" | perl -pe 's/([\\\/])/\\$1/g; s/\n/\\n/g;')
31+
32+
content=$(printf '%s' "$content" | perl -pe "s/{\s*{\s*file\.${escaped_placeholder}\s*}\s*}/$escaped_content/g")
33+
done
34+
35+
printf '%s' "$content"
36+
}
37+
38+
write_file() {
39+
local file_content="$1"
40+
local dest_file="$2"
41+
42+
# Write to destination file
43+
printf '%s\n' "$file_content" > "$dest_file"
44+
}
45+
46+
template_file() {
47+
local source_file="$1"
48+
local dest_file="$2"
49+
50+
# Read the entire source file into a variable
51+
local file_content
52+
file_content=$(<"$source_file")
53+
54+
local templated_content
55+
templated_content=$(template "$file_content")
56+
57+
write_file "$templated_content" "$dest_file"
58+
59+
echo "'$source_file' complete. Output written to '$dest_file'."
60+
}
61+
62+
SOURCE="$1"
63+
DEST_FILE="$2"
64+
65+
if [[ -f "$SOURCE" ]]; then
66+
template_file "$SOURCE_FILE" "$DEST_FILE"
67+
elif [[ -d "$SOURCE" ]]; then
68+
i=0
69+
while IFS= read -r source_file; do
70+
file_content=$(<"$source_file")
71+
72+
first_line=$(printf '%s\n' "$file_content" | head -n 1)
73+
74+
dest_file=$(echo "$first_line" | sed -n 's/^[[:space:]]*>>\([[:graph:]]\+\)[[:space:]]*$/\1/p')
75+
76+
if [[ -n "$dest_file" ]]; then
77+
file_content=$(printf '%s' "$file_content" | tail -n +2)
78+
79+
templated_content=$(template "$file_content")
80+
81+
write_file "$templated_content" "$dest_file"
82+
83+
echo "'$source_file' complete. Output written to '$dest_file'."
84+
85+
(( i++ ))
86+
fi
87+
done < <(find "$SOURCE" -type f -name '*.template.md')
88+
89+
if [[ $i -eq 0 ]]; then
90+
echo "Source '$SOURCE' does not contain any '*.template.md'!"
91+
exit 1
92+
fi
93+
else
94+
echo "Source '$SOURCE' does not exist!"
95+
exit 1
96+
fi

0 commit comments

Comments
 (0)