Skip to content

Latest commit

 

History

8 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

The Wire - Full-Stack Go Blog Platform & API

A production-ready, clean-architecture Blog Platform and RESTful API built in Go (1.22+) with PostgreSQL, secured with custom JWT & bcrypt middleware, coupled with an authentic editorial-styled React Single Page Application (SPA).


🏛️ System Architecture

The backend strictly adopts the Repository Pattern and Dependency Injection for loose coupling, modularity, and high testability:

├── cmd / main.go             # Application entry point, dependency wiring & static SPA server
├── internal/
│   ├── auth/                 # JWT validation middleware & context key management
│   ├── handler/              # HTTP handlers (JSON parsing, response encoding, status codes)
│   ├── model/                # Domain models & database entity structs
│   ├── repository/           # Repository interfaces & PostgreSQL query implementations (pgxpool)
│   ├── routes/               # RESTful HTTP route mappings & middleware wrappers
│   └── security/             # Password hashing (bcrypt), UUID generation, and JWT issuing
└── frontend-react/           # Editorial Minimalist React (Vite) Single Page Application

🛠️ Technology Stack

Backend

  • Language: Go (1.22+) using native enhanced http.ServeMux routing (r.PathValue).
  • Database: PostgreSQL with jackc/pgx/v5 connection pooling (pgxpool).
  • Authentication: Custom JWT middleware (golang-jwt/jwt/v5) with HMAC SHA-256 signatures.
  • Password Security: golang.org/x/crypto/bcrypt salt hashing.
  • Environment: joho/godotenv for configuration management.

Frontend

  • Framework: React 19 + Vite.
  • Design System: Editorial / Newspaper minimalist aesthetic (ivory #f4f4f0 newsprint palette, stark typography, zero-radius structural cards, high-contrast borders).
  • Icons: lucide-react.
  • State Management: React Context API (AuthContext).

📡 API Reference

1. Authentication & Users

Method Endpoint Auth Required Description
POST /register No Creates a new user account with bcrypt-hashed password
POST /login No Verifies credentials and returns a signed JWT token
GET /users/{id} No Retrieves public user profile (excluding password hash)

2. Posts (CRUD)

Method Endpoint Auth Required Description
GET /posts?page=1 No Lists paginated blog posts (10 per page)
POST /posts Yes (Bearer Token) Publishes a new blog entry
GET /posts/{id} No Retrieves a single post by ID
PUT /posts/{id} Yes (Bearer Token) Updates an existing post (author only)
DELETE /posts/{id} Yes (Bearer Token) Deletes an existing post (author only)

3. Comments

Method Endpoint Auth Required Description
GET /posts/{id}/comments No Retrieves all comments for a specific post
POST /posts/{id}/comments Yes (Bearer Token) Adds a new comment to a post
DELETE /comments/{id} Yes (Bearer Token) Deletes a comment by ID

4. Creator Follow System

Method Endpoint Auth Required Description
POST /users/{id}/follow Yes (Bearer Token) Follows a target user
DELETE /users/{id}/follow Yes (Bearer Token) Unfollows a target user

🗄️ Database Schema

CREATE TABLE users (
    id VARCHAR(36) PRIMARY KEY,
    username VARCHAR(30) NOT NULL UNIQUE,
    password_hash VARCHAR(255) NOT NULL,
    creation_date TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE posts (
    post_id VARCHAR(36) PRIMARY KEY,
    user_id VARCHAR(36) NOT NULL,
    content VARCHAR(40000) NOT NULL,
    creation_date TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT fk_posts_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);

CREATE TABLE comments (
    comment_id VARCHAR(36) PRIMARY KEY,
    post_id VARCHAR(36) NOT NULL,
    user_id VARCHAR(36) NOT NULL,
    comment_body VARCHAR(40000) NOT NULL,
    comment_date TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT fk_comments_post FOREIGN KEY (post_id) REFERENCES posts(post_id) ON DELETE CASCADE,
    CONSTRAINT fk_comments_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);

CREATE TABLE follows (
    follower_id VARCHAR(36) NOT NULL,
    following_user_id VARCHAR(36) NOT NULL,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (follower_id, following_user_id),
    CONSTRAINT fk_follows_follower FOREIGN KEY (follower_id) REFERENCES users(id) ON DELETE CASCADE,
    CONSTRAINT fk_follows_following FOREIGN KEY (following_user_id) REFERENCES users(id) ON DELETE CASCADE,
    CONSTRAINT chk_cannot_follow_self CHECK (follower_id <> following_user_id)
);

🚀 Getting Started

Prerequisites

1. Configuration

Create a .env file in the root directory:

PORT=8080
DATABASE_URL=postgres://your_user:your_password@localhost:5432/blogpost?sslmode=disable
JWT_SECRET=your_super_secret_signing_key

2. Install & Build Frontend

cd frontend-react
npm install
npm run build
cd ..

3. Run the Application

# Download dependencies & start the server
go mod tidy
go run main.go

Visit http://localhost:8080 in your web browser.

4. Running Frontend in Dev Mode (with Hot-Reload)

cd frontend-react
npm run dev

Visit http://localhost:5173.


🧪 Automated Testing

Run the automated end-to-end backend test suite:

python3 test.py

This validates User Registration, Login, Token Generation, Post Creation, Post Listing, Comment Streams, User Profiles, and Post Deletion.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages