A simple REST API that serves random motivational quotes. Built with Go, Echo framework, and SQLite.
- Get random motivational quotes
- Add new motivational quotes
- Persistent storage with SQLite
- Automatic migration from text file to database
# Build the application
go build -o motivation
# Run the server
./motivationThe server will start on http://localhost:8080
curl http://localhost:8080/motivationResponse: A random motivational quote as plain text
Status Codes:
200 OK- Success404 Not Found- No motivations in database500 Internal Server Error- Database error
curl -X POST -d "Your motivational quote here" http://localhost:8080/motivationResponse: Motivation added successfully
Status Codes:
201 Created- Successfully added400 Bad Request- Empty motivation500 Internal Server Error- Database error
Environment variables:
DB_PATH- Database file path (default:./motivations.db)
Example:
DB_PATH=/var/data/motivations.db ./motivationOn first run, if motivations.txt exists, the application will:
- Migrate all quotes to the SQLite database
- Back up the original file as
motivations.txt.backup
This migration only happens once when the database is empty.
The application uses SQLite with the following schema:
CREATE TABLE motivations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
text TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);You can query the database directly:
sqlite3 motivations.db "SELECT * FROM motivations;"# Install dependencies
go mod download
# Run without building
go run main.go
# Run tests
go test ./...
# Format code
go fmt ./....
├── main.go # HTTP server and handlers
├── db/ # Database package
│ ├── db.go # Connection management
│ ├── migrations.go # Schema definitions
│ ├── repository.go # CRUD operations
│ └── migrate_text.go # Text file migration
└── motivations.db # SQLite database (generated)- Echo v4 - High performance Go web framework
- modernc.org/sqlite - Pure Go SQLite driver
This project is open source and available under the MIT License.