Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

96 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Chirpy

A simple REST server written from scratch in Golang to practice the basics of developing an API service. The server simulates a locally running "BlueSky/Twitter-like" API where users, register, login, post, and view messages.

Guided project using backend developer training site boot.dev.

Concepts covered:

  • GET, POST, PUT, DELETE operations
  • PostgresSQL acceess and storage
  • Database migrations
  • User authentication
  • Endpoint authorization
  • Query parameters
  • Middleware functions
  • 3rd-party webhooks
  • HTTP unit testing

Setup

  1. Install Go 1.23.1+

  2. Install project libraries

    go install

  3. Install SQLC:

    go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest

  4. Install Goose:

    go install github.com/pressly/goose/v3/cmd/goose@latest

  5. Install the Delve debugger: https://github.com/go-delve/delve/tree/master/Documentation/installation

  6. (Optional) Install an API client (ex: Postman)

Usage

Preliminary

Create DB

  1. Install PostgresSQL (Mac):

    brew install postgresql@15

  2. Run in the background:

    brew services start postgresql@15

  3. Use a client to connect (ex: psql):

    psql postgres

  4. Create the blank database:

    CREATE DATABASE chirpy;

Create the .env file

  1. Create a file named .env in the root directory and add it to .gitgnore. DO NOT COMMIT .env.

  2. In .env, add the following variables:

    DB_URL="postgres://<username>:<password>@localhost:5432/chirpy?sslmode=disable"
    PLATFORM="dev"
    JWT_SECRET="TODO: Steps to generate"
    POLKA_API_KEY="<random, alphanumeric 32-char fake api key>"

Start server

  1. From the root directory:

     go run .
    
  2. Run POST http://localhost:8080/api/reset to delete *all users and posts, clearing the database

    • Can also use during development testing

Send requests

  1. Send requests to http://localhost:8080/<endpoint>
  2. Use http://localhost:8080/admin/reset to clear the database

Endpoints

See the OpenApi 3.1 specifications (manually created).

Development Notes

Database Schema Changes

  1. Create new Goose migration file, place in sql/schema.

  2. Run migration:

    cd sql/schema
    goose postgres <postgress connection string> up
    
    # Rollback last migration (if needed)
    goose postgres <postgress connection string> down

Database Queries

Use SQLC to generate GO functions from SQL queries:

  1. Add plain SQL queries here: sql/schema

  2. Generate Go functions:

    cd <base directory>
    sqlc generate
  3. Access queries from the apiConfig struct:

    func (cfg *apiConfig) myHandler() http.HandlerFunc {
        return func(w http.ResponseWriter, r *http.Request) {
    
            result, err := cfg.db.MySQLFunction(...)
    
        }
    }

See the existing handler functions for more examples.

Handlers

Handlers are set in main.go with implementation in seperate files.

Debugging

  1. Start the Delve debugger:

    dlv debug . --headless --listen=:12345 --continue --accept-multiclient
  2. Connect to the debugger

    • Working VSCode launch.json config:
    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Connect to external session",
                "type": "go",
                "debugAdapter": "dlv-dap",
                "request": "attach",
                "mode": "remote",
                "port": 12345
                // "host": "127.0.0.1", // can skip for localhost
            }
        ]
    }
  3. Set breakpoints in code

  4. Send requests to server (ex: using Postman)

  5. Step debug

Tests

  • Unit tests: go test ./...

Potential enhancements

  • Implement code test coverage
  • Experiment with fuzz testing with Go's testing libraries
  • Local Front-end interface
  • Deployment to cloud service; publicly accessible

About

Very basic CRUD server to learn web server basics, written in Go

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages