A web-based discussion forum built with Go and SQLite.
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
- 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
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
| Route | Method | Description |
|---|---|---|
/ |
GET | Home page |
/register |
GET, POST | User registration |
/login |
GET, POST | User login |
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
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
);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
);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
);Stores comments attached to posts.
Stores likes and dislikes for posts and comments.
Stores available forum categories.
Associates posts with categories.
Clone the repository:
git clone https://learn.zone01kisumu.ke/git/luiwere/forum
cd forumInstall dependencies:
go mod tidyRun the application:
go run .The server starts on:
http://localhost:8080
github.com/mattn/go-sqlite3golang.org/x/crypto/bcrypt
Install manually if required:
go get github.com/mattn/go-sqlite3
go get golang.org/x/crypto/bcrypt- User submits registration form
- Password is hashed using bcrypt
- User information is stored in SQLite
- User is redirected to login page
- User submits email and password
- User is retrieved from the database
- Password is verified using bcrypt
- Session token is generated
- Session is stored in the database
- Session cookie is sent to the browser
- User is redirected to the home page
- 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
- Session validation middleware
- Logout functionality
- User profile handling
- Post creation
- Comments system
- Reactions (likes/dislikes)
- Categories and filtering
- Authorization for protected routes
- Docker installed and the daemon running
- Optional: permission to run Docker without sudo (user in
dockergroup) or usesudoprefix - (Optional) Podman for rootless containers
Run from the project root (where the Dockerfile is) docker build -t forum:latest .
- docker run --rm -p 8080:8080 forum:latest
docker run -d --name forum -p 8080:8080 forum:latest
docker logs -f forum
docker stop forum
docker run -d --name forum -p 8080:8080
-v "$(pwd)/forum.db:/app/forum.db"
forum:latest
docker run -d --name forum -p 8080:8080
-v "$(pwd)/database:/app/database"
-v "$(pwd)/forum.db:/app/forum.db"
forum:latest
- sudo usermod -aG docker $user && newgrp docker
lwere rrouwelng rowairi dngisa