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.
- 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.
- 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
.envmanagement
api/: Contains the HTTP handlers grouped by domain (users/,chirps/,admin/,polka/, and general authentication).internal/database/: Auto-generated database interaction code viasqlc.sql/: Contains the raw database schemas and SQL queries used bysqlcto generate the internal database package.main.go: Application entrypoint, database initialization, route registration, and HTTP server configuration.go.mod/go.sum: Dependency management.
- Go 1.25 or later
- A running PostgreSQL instance
- (Optional)
sqlcCLI if you intend to modify the database queries.
-
Clone the repository:
git clone https://github.com/vohrr/http_server_go.git cd http_server_go -
Configure Environment Variables: Create a
.envfile 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
-
Install Dependencies:
go mod download
-
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.