Commit 23314771 authored by Markus Mößler's avatar Markus Mößler
Browse files

updated docs for tornado stage 01

parent ebad7d60
Loading
Loading
Loading
Loading
Loading
+1 −75
Original line number Diff line number Diff line

# Tornado Backend (Stage 01) - Else
# Tornado Backend (Stage 01) - Details

This document explains the architecture of the current stage of the Tornado-based blogging web application.

@@ -135,80 +135,6 @@ The database schema is defined in `schema.sql` (presumably includes tables like
- **Scalable Deployment**:
  - Dockerized environment ensures consistency and ease of deployment.

## Visualization of This Stage

```mermaid
graph TD
    subgraph Application
        TornadoServer["Tornado Server"]
        Templates["HTML Templates (Jinja-like)"]
        StaticAssets["Static Assets (CSS/JS)"]
    end

    subgraph Database
        PostgreSQL["PostgreSQL Database"]
    end

    subgraph Docker
        BlogService["Blog Service (Python/Tornado)"]
        DatabaseService["Database Service (PostgreSQL)"]
    end

    User["User"] -->|HTTP Requests| TornadoServer
    TornadoServer -->|Renders| Templates
    TornadoServer -->|Serves| StaticAssets
    TornadoServer -->|Async Queries| PostgreSQL
    TornadoServer -->|Renders| Responses

    BlogService -->|Links To| DatabaseService

    TornadoServer -->|Port 8888| BlogService
    PostgreSQL -->|Port 5432| DatabaseService
```

---

This architecture provides a solid foundation for building a more complex web application. Future enhancements might include additional user roles, improved UI, or REST API support.

## Additional Notes

Enter the postgres database container using,

```bash
docker exec -it tornado-backend_postgres_1 psql -U blog -d blog
```

1) Check the entries of the authors table,

```bash
SELECT * from authors;
```

Which results in, e.g., 

```psql
blog=# SELECT * from authors;
 id |         email         |     name      |                       hashed_password                        
----+-----------------------+---------------+--------------------------------------------------------------
  1 | first-blogger@blog.de | First Blogger | $2b$12$G0JKWQT7Tx1DoVqjze2mruM.kxmhP0EeiltKesZT5ZpqWlNnu3dOe
(1 row)
```

2) Check the entries of the entries table,

```psql
SELECT * from entries;
```

Which results in, e.g., 

```psql
blog=# SELECT * from entries;
 id | author_id |            slug             |            title            |                    markdown                    |                         html     
                     |         published          |          updated           
----+-----------+-----------------------------+-----------------------------+------------------------------------------------+----------------------------------
---------------------+----------------------------+----------------------------
  1 |         1 | first-post-by-first-blogger | First post by first blogger | This is the first post by the first blogger... | <p>This is the first post by the 
first blogger...</p> | 2024-12-06 17:23:18.595355 | 2024-12-06 17:23:18.595355
(1 row)
```
 No newline at end of file
+27 −8
Original line number Diff line number Diff line
@@ -53,6 +53,13 @@ blog:
  command: --db_host=postgres
```

This configuration does not define any volume for the PostgreSQL service so if the container is removed or recreated the data will be lost. To make the data persistent use add a volume mapping to the postgres service in the docker compose file, e.g., 

```yml
    volumes:
      - postgres_data:/var/lib/postgresql/data
```

Note, the exposer of port 3306 is probably a mistake. The PostgrSQL default port is 5432 which is also used in `./tornado-backend/blog.py` to connect to the database.

Note also, the python image used installs tornado version 6.2, thus the `set_secure_cookie` and `get_secure_cookie` instead of `set_signed_cookie` and `get_signed_cookie` used in `./tornado-backend/blog.py`. The naming of these methods changed with tornado version 6.3. This is another mistake in the blog demo on GitHub. ([see](https://www.tornadoweb.org/en/stable/web.html#))
@@ -61,24 +68,36 @@ Note also, the python image used installs tornado version 6.2, thus the `set_sec

```mermaid
graph TD
    subgraph Blog Service
        TornadoServer["Tornado Server"]
    %% External user
    User["User (Web Browser)"]

    %% Docker network
    subgraph Docker_Network["Docker Network"]
      
      %% Blog Container
      subgraph BlogContainer["Blog Container"]
        TornadoServer["🐍 Tornado Web Server"]
        Templates["HTML Templates (Jinja-like)"]
        StaticAssets["Static Assets (CSS/JS)"]
      end

    subgraph Database Service 
        PostgreSQL["PostgreSQL Database"]
      %% Database Container
      subgraph DBContainer["PostgreSQL Container"]
        PostgreSQL["🐘 PostgreSQL Database"]
        Volume["📦 Data Volume (Not Persistent Storage Here!)"]
      end
    end

    User["User"] -->|HTTP Requests<br>Port 8888| TornadoServer
    %% Connections
    User -->|HTTP Requests<br>Port 8888| TornadoServer
    TornadoServer -->|Renders| Templates
    TornadoServer -->|Serves| StaticAssets
    TornadoServer -->|Async Queries<br>Port 5432| PostgreSQL
    PostgreSQL -->|Reads/Writes| Volume
```

Note, Tornado is used as web server and web framework e.g. with frontend and backend
Note, Tornado is used as web server and web framework e.g. as frontend and backend

### More Details

* More details on the general architecture of a tornado web app can be found [here](https://www.tornadoweb.org/en/stable/guide/structure.html)
More details on the general architecture of a tornado web app can be found [here](https://www.tornadoweb.org/en/stable/guide/structure.html)
+18 −7
Original line number Diff line number Diff line
@@ -7,26 +7,31 @@
cd tornado-backend/
```

## Step 2) Build multi-container app
## Step 2) Start multi-container app

```bash
docker-compose up --build
docker-compose up
```

## Step 3) Check container status and restart blog container

Check container status,

```bash
docker-compose ps
```

which results in, e.g.,

```bash
NAME                         IMAGE               COMMAND                  SERVICE             CREATED             STATUS              PORTS
tornado-backend-postgres-1   postgres:10.3       "docker-entrypoint.s…"   postgres            52 seconds ago      Up 52 seconds       5432/tcp, 0.0.0.0:32768->3306/tcp, :::32768->3306/tc
```

The blog container will most likely not start the first time. Restart the blog container,
The blog container will most likely not start at the first time. Restart the blog container,

```bash
docker-compose restart blog
[+] Restarting 1/1
 ✔ Container tornado-backend-blog-1  Started                                                                                           0.2s 
```

## Step 4) Use the blog web app
@@ -42,7 +47,7 @@ docker-compose restart blog

## Step 5) Enter the postgres container and check the entries

Enter the postgres database container using,
Enter the postgres database container using psql,

```bash
docker exec -it tornado-backend-postgres-1 psql -U blog -d blog
@@ -63,7 +68,7 @@ which results in e.g.,
(1 row)
```

Check the entries of the entries table
Check the entries of the entries table,

```psql
SELECT * from entries;
@@ -81,6 +86,12 @@ t blogger...</p> | 2024-12-18 22:30:51.994281 | 2024-12-18 22:30:51.994281
(1 row)
```

Exit the psql terminal,

```psql
\q
```

## Step 6) Stop the container

```bash