Skip to content

vohrr/http_server_go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go HTTP Server (Chirpy)

Context: This repository was created as an educational project to gain familiarity with building REST APIs in Go.

This is a RESTful API server built natively using Go's standard library net/http router. It implements a social-media-like backend that supports user accounts, secure authentication, posting short messages ("chirps"), and receiving external webhooks.

Features

  • User Management: Create and update user accounts.
  • Secure Authentication: Secure password hashing with Argon2id and stateless authentication using JSON Web Tokens (JWT) with distinct access, refresh, and revocation flows.
  • Chirps: Endpoints to create, read, and securely delete short messages.
  • Admin & Metrics: Tracks site hits and provides endpoints to monitor server health, check metrics, and reset the state (useful for development).
  • Webhooks: Handles external webhook events (integrated with "Polka").
  • Database Integration: Interacts with a PostgreSQL database using fast, type-safe queries generated by sqlc.

Tech Stack

  • Language: Go 1.25+
  • Routing: Standard Library net/http (leveraging modern HTTP method routing)
  • Database: PostgreSQL
  • Query Engine: sqlc for generating type-safe database code from SQL
  • Authentication: golang-jwt and argon2id
  • Configuration: godotenv for .env management

Project Structure

  • api/: Contains the HTTP handlers grouped by domain (users/, chirps/, admin/, polka/, and general authentication).
  • internal/database/: Auto-generated database interaction code via sqlc.
  • sql/: Contains the raw database schemas and SQL queries used by sqlc to generate the internal database package.
  • main.go: Application entrypoint, database initialization, route registration, and HTTP server configuration.
  • go.mod / go.sum: Dependency management.

Getting Started

Prerequisites

  • Go 1.25 or later
  • A running PostgreSQL instance
  • (Optional) sqlc CLI if you intend to modify the database queries.

Setup

  1. Clone the repository:

    git clone https://github.com/vohrr/http_server_go.git
    cd http_server_go
  2. Configure Environment Variables: Create a .env file in the root directory based on the following template:

    DB_URL=postgres://user:password@localhost:5432/dbname?sslmode=disable
    SECRET=your_jwt_secret_key
    POLKA=your_polka_api_key
    PLATFORM=dev
  3. Install Dependencies:

    go mod download
  4. Run the Server:

    go run main.go

The server will start on port 8080. You can access health check metrics at http://localhost:8080/api/healthz.

About

Http Server from scratch written in Go

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors