Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

89 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Forum

A web-based discussion forum built with Go and SQLite.

Description

Forum allows users to register accounts, log in, and participate in discussions through a simple web interface. The application uses:

  • Go for the backend server
  • SQLite for data storage
  • HTML Templates for rendering pages
  • bcrypt for secure password hashing
  • Cookie-based authentication sessions

Features Implemented

Authentication

  • User registration
  • Form validation during registration
  • Password hashing using bcrypt
  • User login
  • Password verification using bcrypt
  • Session token generation
  • Session storage in SQLite
  • Session cookie creation
  • Error handling for invalid credentials

Database

The database schema includes:

  • Users
  • Sessions
  • Posts
  • Comments
  • Reactions
  • Categories
  • Post Categories

Database tables are automatically created during startup using the schema defined in:

database/schema.sql

Server Routes

Route Method Description
/ GET Home page
/register GET, POST User registration
/login GET, POST User login

Project Structure

forum/
├── database/
│   ├── database.go
│   └── schema.sql
│
├── handlers/
│   ├── register.go
│   └── login.go
│
├── templates/
│   ├── index.html
│   ├── regiter.html
│   └── login.html
│
├── forum.db
├── main.go
├── go.mod
├── go.sum
└── README.md

Database Schema

Users

Stores registered user accounts.

CREATE TABLE users (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  full_name TEXT,
  user_name TEXT UNIQUE NOT NULL,
  email TEXT UNIQUE NOT NULL,
  password TEXT NOT NULL,
  role TEXT DEFAULT 'member',
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

Sessions

Stores active authentication sessions.

CREATE TABLE sessions (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  token TEXT UNIQUE NOT NULL,
  user_id INTEGER NOT NULL,
  expired_at DATETIME NOT NULL,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE
);

Posts

Stores forum posts.

CREATE TABLE posts (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  title TEXT NOT NULL,
  body TEXT NOT NULL,
  user_id INTEGER NOT NULL,
  status TEXT DEFAULT 'active',
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE
);

Comments

Stores comments attached to posts.

Reactions

Stores likes and dislikes for posts and comments.

Categories

Stores available forum categories.

Post Categories

Associates posts with categories.


Installation

Clone the repository:

git clone https://learn.zone01kisumu.ke/git/luiwere/forum
cd forum

Install dependencies:

go mod tidy

Run the application:

go run .

The server starts on:

http://localhost:8080

Dependencies

  • github.com/mattn/go-sqlite3
  • golang.org/x/crypto/bcrypt

Install manually if required:

go get github.com/mattn/go-sqlite3
go get golang.org/x/crypto/bcrypt

Authentication Flow

Registration

  1. User submits registration form
  2. Password is hashed using bcrypt
  3. User information is stored in SQLite
  4. User is redirected to login page

Login

  1. User submits email and password
  2. User is retrieved from the database
  3. Password is verified using bcrypt
  4. Session token is generated
  5. Session is stored in the database
  6. Session cookie is sent to the browser
  7. User is redirected to the home page

Current Status

Completed

  • Database initialization
  • SQLite integration
  • Schema creation and migration
  • User registration
  • Password hashing with bcrypt
  • User login
  • Session creation
  • Cookie generation
  • Route setup
  • HTML template rendering

In Progress

  • Session validation middleware
  • Logout functionality
  • User profile handling
  • Post creation
  • Comments system
  • Reactions (likes/dislikes)
  • Categories and filtering
  • Authorization for protected routes

Prerequisites

  • Docker installed and the daemon running
  • Optional: permission to run Docker without sudo (user in docker group) or use sudo prefix
  • (Optional) Podman for rootless containers

Build the Docker image

Run from the project root (where the Dockerfile is) docker build -t forum:latest .

Run the container

  • docker run --rm -p 8080:8080 forum:latest

Run detached

docker run -d --name forum -p 8080:8080 forum:latest

view logs:

docker logs -f forum

stop:

docker stop forum

Persist the SQLite database (recommended)

Mount host file (overwrites image DB)

docker run -d --name forum -p 8080:8080
-v "$(pwd)/forum.db:/app/forum.db"
forum:latest

Or mount database folder read-write

docker run -d --name forum -p 8080:8080
-v "$(pwd)/database:/app/database"
-v "$(pwd)/forum.db:/app/forum.db"
forum:latest

Running without sudo

  • sudo usermod -aG docker $user && newgrp docker

AuthorsG

lwere rrouwelng rowairi dngisa


About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages