Skip to content

Sample HTTP server built with Go. It demonstrates user authentication using JWT, chirp posting with full CRUD operations, and administrative endpoints for metrics and system resets. Featuring integration with PostgreSQL, Goose for migrations, and sqlc for generating type-safe code, a practical example of building production-ready web services in Go

Notifications You must be signed in to change notification settings

marcuschui2022/go-http-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

54 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Chirpy

Chirpy is a sample HTTP server built with Go. It supports user authentication, chirp posting, and includes administrative endpoints for metrics and system resets.


Table of Contents


Features

  • User creation, login, and update using JWT authentication.
  • Post chirps (short messages) with creation, retrieval, and deletion endpoints.
  • Refreshing and revoking JWT tokens.
  • Admin endpoints to check the server metrics and reset the hit counter.
  • Webhook support for external events.

Requirements

  • Go 1.23
  • PostgreSQL
  • Goose for handling database migrations
  • sqlc for handling generate type-safe code from SQL

Installation

  1. Clone the repository:

    git clone https://github.com/marcuschui2022/go-http-server.git
    cd go-http-server
  2. Install dependencies:

    go mod tidy
  3. Setup the PostgreSQL database and create a database named chirpy.

  4. Set up environment variables:

    Copy the example below into a .env file at the root of the project.


Environment Variables

Create a .env file at the root with the following configuration (replace placeholders with your actual values):

PLATFORM=dev
DB_URL=postgres://<USER>:<PASSWORD>@localhost:5432/chirpy
JWT_SECRET=<your_jwt_secret>
POLKA_KEY=<your_api_key>

Database Migrations

Database migrations are managed with goose. To apply or revert migrations, follow these steps:

  1. Navigate to the SQL schema directory:

    cd sql/schema
  2. Migrate Up:

    goose postgres "postgres://<USER>:<PASSWORD>@localhost:5432/chirpy" up
  3. Migrate Down:

    goose postgres "postgres://<USER>:<PASSWORD>@localhost:5432/chirpy" down
  4. Write queries in SQL file :

    cd sql/queries
    touch <name>.sql

5generate type-safe code form SQL:

sqlc generate

Running the Server

From the project root, run:

go run .

By default, the server listens on port 8080. You can change this in the source if needed.


API Endpoints

User Endpoints

  • Create User: POST /api/users
  • Update User: PUT /api/users
  • Login: POST /api/login
  • Refresh Token: POST /api/refresh
  • Revoke Token: POST /api/revoke

Chirp Endpoints

  • Create Chirp: POST /api/chirps
  • Retrieve Chirps: GET /api/chirps
    • Optional query parameter author_id (UUID) to filter chirps by user.
    • Optional query parameter sort to specify the order in which results are returned. Use asc for ascending order (default) or desc for descending order.
  • Retrieve Single Chirp: GET /api/chirps/{chirpID}
  • Delete Chirp: DELETE /api/chirps/{chirpID}

Admin Endpoints

  • Reset Hits: POST /admin/reset (only available in dev mode)
  • Metrics: GET /admin/metrics

Webhook Endpoint

  • Polka Webhook: POST /api/polka/webhooks

Testing

Run the test suite with the following command:

go test ./...

License

This project is licensed under the MIT License.

About

Sample HTTP server built with Go. It demonstrates user authentication using JWT, chirp posting with full CRUD operations, and administrative endpoints for metrics and system resets. Featuring integration with PostgreSQL, Goose for migrations, and sqlc for generating type-safe code, a practical example of building production-ready web services in Go

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published