A lightweight OPD (Outpatient Department) management API written in Go. This repository implements the backend server, database queries (via sqlc), and utilities for configuration and migrations.
This tailored README reflects the repository layout and build/run steps for this project as found in the repository (Go 1.24, Gin, sqlc + MySQL).
- What this is
- Tech stack & key dependencies
- Repository layout
- Prerequisites
- Configuration / environment variables
- Database: schema, queries, and sqlc
- Build & run (local)
- Testing
- Development notes
- Contributing
- Contact
hospitalOPD is a Go-based HTTP API server for managing outpatient department workflows (patients, appointments/specialities, staff). The server is implemented with Gin and uses generated database code from sqlc to access a MySQL database.
The application entrypoint is main.go, which loads configuration, connects to the database, initializes the store and API server, then starts the HTTP server.
- Language: Go (go 1.24.1 — declared in go.mod)
- HTTP framework: github.com/gin-gonic/gin
- Database: MySQL (github.com/go-sql-driver/mysql)
- DB query generator: sqlc (configured in
sqlc.yaml) - Config: github.com/spf13/viper
- UUIDs: github.com/google/uuid
See go.mod for a full dependency list.
- main.go — application entrypoint
- api/ — HTTP handlers and server initialization (server.go, user.go, hospital.go, speciality.go, check_up_time.go, client.go, ...)
- db/
- query/ — SQL query files used by sqlc
- migration/ — SQL migration files for schema
- sqlc/ — auto-generated code produced by sqlc
- util/ — configuration and helpers (config loader used in main.go)
- sqlc.yaml — sqlc configuration targeting MySQL
- go.mod / go.sum
- Go 1.24 (or compatible)
- MySQL server (for local development you can use a local mysql instance or Docker)
- sqlc (if you need to re-generate db/sqlc)
- git
Optional:
- Docker / docker-compose if you add containerization later
The app loads configuration via a util loader (viper). The repository expects environment variables / configuration for the DB and server address. Typical variables used by this project include (verify exact keys in db/util):
Example .env (adapt values as needed)
# Database
DB_DRIVER=mysql
DB_SOURCE=user:password@tcp(localhost:3306)/hospitalopd?parseTime=true
# Server
SERVER_ADDRESS=0.0.0.0:8080
Note: The code references fields like config.DBDriver, config.DBSource, and config.ServerAddress in main.go. Use those exact keys when checking the config struct in db/util.
- sqlc is configured in
sqlc.yamlto use MySQL, with:- queries in
db/query/ - schema files in
db/migration/ - generated Go package output at
db/sqlc/
- queries in
Typical workflow:
- Apply migrations to create the database schema. If migration SQL files are in
db/migration/, apply them with MySQL CLI or a migration tool (e.g., goose, migrate):- Example (mysql client):
mysql -u user -p hospitalopd < db/migration/000001_init.sql
- Example (mysql client):
- Generate typed DB access code if you change SQL or schema:
- Install sqlc (https://sqlc.dev) and run: sqlc generate
- This will update files under
db/sqlc/.
- Ensure
DB_SOURCE/DB_DRIVERpoint to the running MySQL instance.
From the repository root:
-
Set environment variables (or use a .env loader). Example:
- export DB_DRIVER=mysql
- export DB_SOURCE='user:password@tcp(localhost:3306)/hospitalopd?parseTime=true'
- export SERVER_ADDRESS='0.0.0.0:8080'
-
(Optional) Generate sqlc code if you made SQL changes:
- sqlc generate
-
Run the server:
- go run main.go or build a binary:
- go build -o hospitalopd .
- ./hospitalopd
The server binds to config.ServerAddress (e.g., :8080) as loaded by the config loader.
Run unit tests and package tests:
- go test ./...
For behavior/integration tests you may need a running MySQL instance or a test database.
- Config is loaded via a util package (viper). Inspect
db/utilto see exact keys and file-based config options. - API routes, handlers, and request models are in the
api/package. - SQL defined in
db/query/*.sqlis compiled by sqlc — prefer editing/adding .sql files there and re-runningsqlc generate. - Consider adding:
- a Makefile with common targets (build, run, test, sqlc)
- Dockerfile / docker-compose for a reproducible dev environment
- CI (GitHub Actions) to run
go testand vet/lint checks
- Fork the repo.
- Create a topic/feature branch:
git checkout -b feat/your-feature. - Add tests for new functionality and run the test suite.
- Open a pull request with a clear description of the changes.
Security note: Do not commit real patient data or secrets into the repository. Use environment variables or a secrets manager for production deployments.
Maintainer: KothariMansi — https://github.com/KothariMansi