Update for new deployment with Caddy and docker networks

This commit is contained in:
2025-07-09 03:46:33 +00:00
parent 31b4ee1ebf
commit fc2ef36a0c
17 changed files with 94 additions and 57 deletions

View File

@ -2,11 +2,11 @@ HOST=0.0.0.0
PORT=80
LOG_LEVEL=WARN
DEBUG=false
SQLALCHEMY_DATABASE_URI=postgresql+psycopg://postgres:123456@db:5432/petty{{ name }}
SQLALCHEMY_DATABASE_URI=postgresql+psycopg://postgres:123456@postgres:5432/petty{{ name }}
MODULE_NAME=barker.main
PROJECT_NAME=barker
MAX_WORKERS=4
REDIS_HOST=redis
REDIS_HOST=valkey
REDIS_PORT=6379
SECRET_KEY={{ secret_key }}
MIDDLEWARE_SECRET_KEY={{ middleware_key }}

View File

@ -0,0 +1,24 @@
{{ host }} {
# Match and proxy API routes
@apiRoutes {
path_regexp ^/(api|token|refresh|db-image)
}
handle @apiRoutes {
reverse_proxy @apiRoutes {{ host_directory }}:80
}
# Match requests that end with .js, .css, .ico, or .html
@staticFiles {
path_regexp \.(js|css|ico|html)$
}
handle @staticFiles {
rewrite * /static{uri}
reverse_proxy {{ host_directory }}:80
}
# All other frontend routes → /static/index.html
handle {
rewrite * /static/index.html
reverse_proxy {{ host_directory }}:80
}
}

View File

@ -9,5 +9,6 @@
- "{{ var_file }}"
roles:
- network
- barker
- nginx
- caddy

View File

@ -30,10 +30,7 @@
state: started
restart_policy: "unless-stopped"
env_file: "/var/lib/{{ host_directory }}/.env"
links:
- "postgres:db"
- "redis:redis"
published_ports:
- "127.0.0.1:{{ host_port }}:80"
volumes:
- "/var/lib/{{ host_directory }}/frontend:/frontend"
networks:
- name: "{{ docker_network }}"
# volumes:
# - "/var/lib/{{ host_directory }}/frontend:/frontend"

View File

@ -0,0 +1,7 @@
caddy_container: caddy
caddyfile_path: /var/lib/caddy/conf/Caddyfile
# snippet_block: |
# {{ host }} {
# reverse_proxy {{ barker_container }}:80
# }

View File

@ -0,0 +1,7 @@
---
# Handlers for Nginx role
- name: Restart caddy container
docker_container:
name: "{{ caddy_container }}"
state: started
restart: true

View File

@ -0,0 +1,26 @@
---
- name: Read snippet from template file
set_fact:
snippet_block: "{{ lookup('files', 'Caddyfile.j2') }}"
- name: Read current Caddyfile
ansible.builtin.slurp:
path: "{{ caddyfile_path }}"
register: caddyfile_raw
- name: Decode Caddyfile content
set_fact:
caddyfile_content: "{{ caddyfile_raw['content'] | b64decode }}"
- name: Check if snippet already exists
set_fact:
snippet_present: "{{ snippet_block in caddyfile_content }}"
- name: Add snippet if missing
ansible.builtin.blockinfile:
path: "{{ caddyfile_path }}"
marker: "# {mark} Ansible managed Caddy snippet for {{ host }}"
block: "{{ snippet_block }}"
create: yes
when: not snippet_present
notify: Restart caddy container

View File

@ -0,0 +1,11 @@
---
# Tasks for Mosquitto role
- name: Ensure 'iot' Docker network exists
docker_network:
name: "{{ docker_network }}"
state: present
connected:
- caddy
- valkey
- postgres
appends: yes

View File

@ -1,2 +0,0 @@
---
# Default variables for Nginx role

View File

@ -1,6 +0,0 @@
---
# Handlers for Nginx role
- name: Reload Nginx
service:
name: nginx
state: reloaded

View File

@ -1,24 +0,0 @@
---
# Tasks for Nginx role
- name: Check if Nginx conf file exists
stat:
path: "/etc/nginx/sites-available/{{ http_conf }}"
register: status
- name: No need to reload Nginx
debug:
msg: "No need to reload Nginx as sites-available entries have already been created"
- name: Set Nginx conf file
when: status.stat.exists == false
template:
src: "files/nginx.conf.j2"
dest: "/etc/nginx/sites-available/{{ http_conf }}"
- name: Enable new site
when: status.stat.exists == false
file:
src: "/etc/nginx/sites-available/{{ http_conf }}"
dest: "/etc/nginx/sites-enabled/{{ http_conf }}"
state: link
notify: Reload Nginx

View File

@ -3,7 +3,6 @@ name: chd
secret_key: d9e4facec94d7e0bf3d63ca03b1d78d834b158627b6593274f7fe27f6aed6db4
middleware_key: 8d5f28b083
http_host: "knox.greatbear.in"
http_conf: "knox.greatbear.in.conf"
host_port: "8337"
host: knox.greatbear.in
host_directory: "barker-{{ name }}"
docker_network: "{{ host_directory}}_net"

View File

@ -13,7 +13,4 @@ tag: latest
image_name: "{{ registry }}/barker:{{ tag }}"
# http_host: "knox.hopsngrains.com"
# http_conf: "knox.hopsngrains.com.conf"
# host_port: "8338"
# host_directory: "barker-{{ name }}"

View File

@ -3,7 +3,6 @@ name: hin
secret_key: 029858927beda45d76317729824ae500d0938f04d4ca955f3afa95061c9a9298
middleware_key: c4b0b98ff9
http_host: "knox.mozimo.in"
http_conf: "knox.mozimo.in.conf"
host_port: "8335"
host: knox.mozimo.in
host_directory: "barker-{{ name }}"
docker_network: "{{ host_directory}}_net"

View File

@ -3,7 +3,6 @@ name: mhl
secret_key: 8b7f704ad1bbee3caa683a57440a6a371937fe5f64a7712a6a15bf3165c3c598
middleware_key: 1aa5487223
http_host: "knox.hngmohali.com"
http_conf: "knox.hngmohali.com.conf"
host_port: "8336"
host: knox.hngmohali.com
host_directory: "barker-{{ name }}"
docker_network: "{{ host_directory}}_net"

View File

@ -3,7 +3,6 @@ name: pkl
secret_key: bd6e5dee0f3b8a6f0db50f7aa08e91d55b2ae5ab6df126defa37e80602481002
middleware_key: 1d34ef6597
http_host: "knox.hopsngrains.com"
http_conf: "knox.hopsngrains.com.conf"
host_port: "8338"
host: knox.hopsngrains.com
host_directory: "barker-{{ name }}"
docker_network: "{{ host_directory}}_net"

View File

@ -1,6 +1,7 @@
import uvicorn
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from starlette.middleware.sessions import SessionMiddleware
from .core.config import settings
@ -116,6 +117,8 @@ app.include_router(merge_move.router, prefix="/api", tags=["voucher"])
app.include_router(split.router, prefix="/api", tags=["voucher"])
app.include_router(change.router, prefix="/api/voucher", tags=["voucher"])
app.mount("/static", StaticFiles(directory="static"), name="static")
def init() -> None:
uvicorn.run(app, host=settings.HOST, port=settings.PORT)