o
odinpkg.dev
packages / library / coffees_odin

coffees_odin

c4e3ff9library

No description provided.

No license · updated 9 months ago

Coffee Server - Odin Web Application

A web server application built with the Odin programming language, featuring PostgreSQL database integration and JWT authentication.

Table of Contents

Prerequisites

  • Odin Compiler: The Odin programming language compiler
  • PostgreSQL C Libraries (libpq): Required for database connectivity
  • Docker & Docker Compose: For running the PostgreSQL database
  • Make: For using the Makefile commands

Installation

Installing Odin

macOS (Homebrew)

brew install odin

Linux

# Clone the Odin repository
git clone https://github.com/odin-lang/Odin
cd Odin

# Build the compiler
make

# Add to PATH (add this to your ~/.bashrc or ~/.zshrc)
export PATH=$PATH:/path/to/Odin

Windows

  1. Download the latest release from Odin Releases
  2. Extract the archive to a directory (e.g., C:\Odin)
  3. Add the Odin directory to your system PATH

Verify Installation

odin version

Installing PostgreSQL C Libraries

The application requires the PostgreSQL C libraries (libpq) to connect to the database.

macOS (Homebrew)

# Install PostgreSQL (includes libpq)
brew install libpq

# Note the installation path (typically /opt/homebrew/Cellar/libpq/<version>/)
brew list libpq

After installation, you need to set the library paths. The Makefile handles this automatically, but if you need to set them manually:

export LIBRARY_PATH="/opt/homebrew/Cellar/libpq/17.6/lib:$LIBRARY_PATH"
export CPATH="/opt/homebrew/Cellar/libpq/17.6/include:$CPATH"

Note: Adjust the version number (17.6) to match your installed version.

Linux (Debian/Ubuntu)

sudo apt-get update
sudo apt-get install libpq-dev postgresql-client

Linux (RHEL/CentOS/Fedora)

sudo dnf install libpq-devel postgresql

Windows

  1. Download and install PostgreSQL from postgresql.org
  2. Add the PostgreSQL lib and include directories to your system PATH

Project Setup

  1. Clone the repository (if not already done):

    cd /path/to/coffee_server
  2. Configure environment variables:

    The project includes a .env file with the following configuration:

    BINARY=odin-coffee
    PORT=8080
    POSTGRES_USER=postgres
    POSTGRES_PASSWORD=password
    POSTGRES_DB=odin-coffee-db
    DB_PORT=5432
    HOST=localhost
    

    Modify these values as needed for your environment.

  3. Ensure PostgreSQL library paths are correct:

    Check the Makefile and update the libpq paths if your installation is in a different location:

    # Find your libpq installation
    brew list libpq  # macOS
    # or
    dpkg -L libpq-dev  # Linux

Running the Application

Using Docker Compose

Docker Compose is used to run the PostgreSQL database in a container.

Start the database:

docker compose up -d

This command will:

  • Start a PostgreSQL 14.2 container
  • Expose the database on port 5432
  • Store data persistently in ./db/cached-db-data/postgres/
  • Use the environment variables from your .env file

Stop the database:

docker compose down

Check database status:

docker compose ps

View database logs:

docker compose logs dev_db

Using the Makefile

The Makefile provides convenient commands for running the application.

Run the application:

make run

This command will:

  1. Export the necessary library paths for PostgreSQL
  2. Compile and run the Odin application

Connect to the database (via psql):

make connect_to_db

This command connects to the running PostgreSQL container using the psql client.

Additional Commands

Building manually

If you need to build without running:

export LIBRARY_PATH="/opt/homebrew/Cellar/libpq/17.6/lib:$LIBRARY_PATH"
export CPATH="/opt/homebrew/Cellar/libpq/17.6/include:$CPATH"
odin build .

Running manually

./coffee_server

Database migrations

If you need to run migrations, you can use goose (if installed):

goose -dir ./migrations postgres "user=postgres dbname=odin-coffee-db sslmode=disable" up

Environment Variables

Key environment variables used by the application:

Variable Description Default
BINARY Container name for Docker odin-coffee
PORT Application port 8080
POSTGRES_USER Database user postgres
POSTGRES_PASSWORD Database password password
POSTGRES_DB Database name odin-coffee-db
DB_PORT Database port 5432
HOST Database host localhost
JWT_SECRET Secret key for JWT tokens (see .env)
APP_ENV Application environment development

Project Structure

coffee_server/
├── main.odin              # Main application entry point
├── database.odin          # Database connection and queries
├── Makefile              # Build and run commands
├── docker-compose.yml    # PostgreSQL container configuration
├── .env                  # Environment variables
├── helpers/              # Helper functions
├── services/             # Business logic services
├── types/                # Type definitions
├── migrations/           # Database migrations
└── odin-http/            # HTTP server library

Troubleshooting

Library path issues

If you encounter errors about missing PostgreSQL libraries:

  1. Verify libpq installation:

    brew list libpq  # macOS
  2. Update the paths in the Makefile to match your installation

  3. Set the environment variables manually:

    export LIBRARY_PATH="/path/to/libpq/lib:$LIBRARY_PATH"
    export CPATH="/path/to/libpq/include:$CPATH"

Database connection issues

  1. Ensure Docker is running:

    docker ps
  2. Check if the database container is healthy:

    docker compose ps
  3. Verify the database is accepting connections:

    make connect_to_db

Port conflicts

If port 5432 or 3000 is already in use, update the .env file with different port numbers.

License

[Your License Here]

Contributing

[Your Contributing Guidelines Here]