:PROPERTIES:
:ID:       9a71daa4-d9d4-4706-98f1-72e3d4eda9bc
:END:
#+title: Server Backend
#+filetags: :server:java:api:

* Introduction
The original backend was written using FastAPI and has now been archived.

In replacement, I have written a Java Spring Boot backend that handles all of the API calls the domain ~zainezq.com~ uses.

Here is the repository for the backend:

[[https://gitea.zainezq.com/zaine/org_backend][Link to the backend repository]]


* Structure

The backend is structured as a typical Spring Boot application. It consists of several packages that handle different aspects of the application:
- **Controllers**: These classes handle incoming HTTP requests and map them to appropriate service methods.
- **Services**: These classes contain the business logic of the application.
- **Repositories**: These classes interact with the database to perform CRUD operations.
- **Models**: These classes represent the data structures used in the application.

The full list of API endpoints can be viewed using Swagger UI, which is available at the ~/swagger-ui.html~ endpoint once the application is running.

* Running the Application

The application is built using Maven. To run the application, you can utilise the ~Makefile~ provided in the repository.

The project is packaged as a Docker container, making it easy to deploy. You can build and run the Docker container using the following commands:

#+begin_src bash

    docker compose build org_backend

    docker compose up -d org_backend

    # Alternatively:
    docker compose down && docker compose build --no-cache && docker compose up -d

    # To check logs

    docker logs -f org_backend

#+end_src

* Bruno
Bruno is used for testing the backend API endpoints. It is a simple HTTP client that allows you to send requests to the backend and view the responses. I used the OpenAPI specification to generate Bruno tests for each endpoint.

* Future?
** Integration with ~notes.zainezq.com~
As the API is primarily used by ~zainezq.com~, I want to think of ways that ~notes.zainezq.com~ could also use it. This website is used to store all my notes, and they are exported using ~org-publish~ in Emacs. Perhaps I could write an Emacs Lisp package that interacts with the backend API to store and retrieve notes.

Flow of how a note would be written:
1. User writes a note in ~notes.zainezq.com~ as org mode.
2. When the note is saved, a POST request is sent to the backend API with the note content.
3. The backend API stores the note in the database.
4. This will trigger a function that republishes the notes website to include the new note.

**Need to think more about this.**

* Update

The org-backend is no longer hosted using docker, it is now using systemd. The backend is still built using Maven, but it is now deployed as a standalone application rather than a Docker container.

Here are the commands needed:

#+begin_src bash

  # To build the application
  mvn clean package -DskipTests

  # To run the application
  java -jar target/org_backend-1.0.0-SNAPSHOT.jar

  # systemctl commands
  sudo systemctl start org_backend
  sudo systemctl stop org_backend
  sudo systemctl restart org_backend
  sudo systemctl status org_backend

  # To check logs
  journalctl -u org_backend -f

  #+end_src
