Go + Echo + GORM + PostgreSQL REST API with JWT authentication and Swagger (OpenAPI) documentation.
This project implements the specification in Baasid backend project v1.1.md:
POST /auth/login— returns JWT in response headerAuthorization(Bearer scheme) and JSON{account, name}.POST /goods/add,GET /goods,GET /goods/:id,PUT /goods/:id,DELETE /goods/:id— all require a valid JWT; errors follow the document (401login failure,403insufficient permission / bad token for goods APIs,500system error).
Prerequisites
- Go 1.22+
- PostgreSQL 13+ (uses
gen_random_uuid()) - Optional: swag CLI to regenerate OpenAPI after editing handler comments
Quick start
1. Start PostgreSQL
Either use Docker:
cd Baasid
docker compose up -d
Or point DATABASE_URL at any existing PostgreSQL instance.
2. Create schema + seed data
psql "$DATABASE_URL" -f scripts/create_tables.sql
psql "$DATABASE_URL" -f scripts/seed_data.sql
The server also runs GORM AutoMigrate on startup for convenience, but the SQL scripts are the authoritative schema for submission.
3. Configure environment
cp .env.example .env
# edit .env — at minimum DATABASE_URL and JWT_SECRET
4. Generate Swagger docs (first time, or after changing // @... comments)
go install github.com/swaggo/swag/cmd/swag@latest
make swagger
5. Run the API server
make run
# or: go run ./cmd/server
- Health:
GET http://127.0.0.1:8080/health - Swagger UI:
http://127.0.0.1:8080/swagger/index.html
Default demo credentials (from scripts/seed_data.sql)
| Field | Value |
|---|---|
| account | admin |
| password | password |
Example: login then call goods API
# Login — capture Authorization header (requires curl -D - or manual copy)
curl -i -X POST http://127.0.0.1:8080/auth/login \
-H 'Content-Type: application/json' \
-d '{"account":"admin","password":"password"}'
# Use the token from the Authorization response header:
export TOKEN='paste-here-without-Bearer-prefix-if-you-want'
curl -H "Authorization: Bearer $TOKEN" http://127.0.0.1:8080/goods
Project layout
Baasid/
├── cmd/
│ ├── server/main.go # process entrypoint + global Swagger metadata
│ └── hashpw/main.go # optional bcrypt hash generator for SQL seeds
├── docs/ # generated by `make swagger` (OpenAPI + swagger UI)
├── internal/
│ ├── auth/ # bcrypt + JWT signing / parsing
│ ├── config/ # environment configuration
│ ├── database/ # GORM postgres connection + AutoMigrate
│ ├── dto/ # JSON wire types
│ ├── handlers/ # Echo handlers (thin HTTP layer)
│ ├── middleware/ # JWT middleware
│ ├── models/ # GORM entities
│ ├── repository/ # database access helpers
│ └── router/ # route registration + error handler
├── scripts/
│ ├── create_tables.sql # DDL
│ └── seed_data.sql # INSERT demo rows
├── docker-compose.yml
├── go.mod
├── Makefile
└── README.md
Rotating passwords for seed SQL
go run ./cmd/hashpw 'your-new-password'
# paste output into scripts/seed_data.sql
License
Example project code — use as you wish.