Go言語で実装されたタスク管理APIサーバーです。
- Go 1.21+
- MySQL
- gorilla/mux (ルーティング)
- go-sql-driver/mysql (MySQLドライバ)
- rs/cors (CORS対応)
| エンドポイント |
メソッド |
説明 |
/api/tasks |
GET |
タスク一覧取得 |
/api/tasks/{id} |
GET |
タスク詳細取得 |
/api/tasks |
POST |
タスク作成 |
/api/tasks/{id} |
PUT |
タスク更新 |
/api/tasks/{id} |
DELETE |
タスク削除 |
| カラム |
型 |
説明 |
| id |
INT |
主キー (AUTO_INCREMENT) |
| task |
VARCHAR(255) |
タスク内容 |
| limit_date |
DATE |
期限日 |
| status |
VARCHAR(20) |
ステータス (pending/completed) |
| created_at |
DATETIME |
作成日時 |
| updated_at |
DATETIME |
更新日時 |
| 変数名 |
デフォルト値 |
説明 |
| DB_HOST |
localhost |
MySQLホスト |
| DB_PORT |
3306 |
MySQLポート |
| DB_USER |
root |
MySQLユーザー |
| DB_PASSWORD |
password |
MySQLパスワード |
| DB_NAME |
taskdb |
データベース名 |
- データベースを作成
- 依存関係をインストール
- 環境変数を設定して起動
export DB_HOST=localhost
export DB_PASSWORD=yourpassword
go run main.go
サーバーが http://localhost:8080 で起動します。
go build -o task-api main.go
./task-api
curl -X POST http://localhost:8080/api/tasks \
-H "Content-Type: application/json" \
-d '{"task": "Buy groceries", "limit_date": "2024-12-31"}'
curl http://localhost:8080/api/tasks
curl -X PUT http://localhost:8080/api/tasks/1 \
-H "Content-Type: application/json" \
-d '{"task": "Buy groceries", "limit_date": "2024-12-31", "status": "completed"}'
curl -X DELETE http://localhost:8080/api/tasks/1