このドキュメントでは、Go Gin Docker サンプルアプリケーションが提供するREST APIの詳細仕様を説明します。
- ベースURL:
http://localhost:8080 - レスポンス形式: JSON
- 文字エンコーディング: UTF-8
- HTTPメソッド: GET のみ(このサンプルでは)
| エンドポイント | メソッド | 説明 | 認証 |
|---|---|---|---|
/health |
GET | ヘルスチェック | 不要 |
/users |
GET | 全ユーザー取得 | 不要 |
/users/:id |
GET | 特定ユーザー取得 | 不要 |
/api/info |
GET | API情報取得 | 不要 |
サーバーの稼働状況を確認するためのエンドポイントです。
GET /health
成功時 (200 OK)
{
"status": "ok",
"message": "サーバーは正常に動作しています"
}curl -X GET http://localhost:8080/healthシステムに登録されている全ユーザーの一覧を取得します。
GET /users
成功時 (200 OK)
{
"status": "success",
"data": [
{
"id": 1,
"name": "田中太郎",
"age": 25
},
{
"id": 2,
"name": "佐藤花子",
"age": 30
},
{
"id": 3,
"name": "鈴木一郎",
"age": 28
}
],
"count": 3
}| フィールド | 型 | 説明 |
|---|---|---|
status |
string | 処理結果のステータス |
data |
array | ユーザー情報の配列 |
data[].id |
integer | ユーザーID |
data[].name |
string | ユーザー名 |
data[].age |
integer | 年齢 |
count |
integer | 取得したユーザー数 |
curl -X GET http://localhost:8080/users指定されたIDのユーザー情報を取得します。
GET /users/:id
| パラメータ | 型 | 必須 | 説明 |
|---|---|---|---|
id |
integer | ✓ | 取得したいユーザーのID |
成功時 (200 OK)
{
"status": "success",
"data": {
"id": 1,
"name": "田中太郎",
"age": 25
}
}ユーザーが見つからない場合 (404 Not Found)
{
"status": "error",
"message": "ユーザーが見つかりません"
}無効なIDの場合 (400 Bad Request)
{
"status": "error",
"message": "無効なユーザーIDです"
}# 存在するユーザーを取得
curl -X GET http://localhost:8080/users/1
# 存在しないユーザーを取得(404エラー)
curl -X GET http://localhost:8080/users/999
# 無効なIDを指定(400エラー)
curl -X GET http://localhost:8080/users/abcこのAPIの基本情報と利用可能なエンドポイント一覧を取得します。
GET /api/info
成功時 (200 OK)
{
"api_name": "Gin Docker Sample API",
"version": "1.0.0",
"description": "Go GinとDockerを使ったサンプルREST API",
"endpoints": [
"GET /health - ヘルスチェック",
"GET /users - 全ユーザー取得",
"GET /users/:id - 特定ユーザー取得",
"GET /api/info - API情報取得"
]
}| フィールド | 型 | 説明 |
|---|---|---|
api_name |
string | API名 |
version |
string | APIバージョン |
description |
string | APIの説明 |
endpoints |
array | 利用可能なエンドポイント一覧 |
curl -X GET http://localhost:8080/api/info全てのエラーレスポンスは以下の形式で返されます:
{
"status": "error",
"message": "エラーメッセージ"
}| ステータスコード | 説明 | 発生条件 |
|---|---|---|
| 200 | OK | 正常処理 |
| 400 | Bad Request | 無効なリクエストパラメータ |
| 404 | Not Found | 指定されたリソースが見つからない |
| 500 | Internal Server Error | サーバー内部エラー |
アプリケーションには以下のテストデータが含まれています:
[
{
"id": 1,
"name": "田中太郎",
"age": 25
},
{
"id": 2,
"name": "佐藤花子",
"age": 30
},
{
"id": 3,
"name": "鈴木一郎",
"age": 28
}
]// 全ユーザー取得
fetch('http://localhost:8080/users')
.then(response => response.json())
.then(data => {
console.log('ユーザー一覧:', data.data);
console.log('ユーザー数:', data.count);
})
.catch(error => console.error('エラー:', error));
// 特定ユーザー取得
fetch('http://localhost:8080/users/1')
.then(response => response.json())
.then(data => {
if (data.status === 'success') {
console.log('ユーザー情報:', data.data);
} else {
console.error('エラー:', data.message);
}
});import requests
# 全ユーザー取得
response = requests.get('http://localhost:8080/users')
if response.status_code == 200:
data = response.json()
print(f"ユーザー数: {data['count']}")
for user in data['data']:
print(f"ID: {user['id']}, 名前: {user['name']}, 年齢: {user['age']}")
# 特定ユーザー取得
user_id = 1
response = requests.get(f'http://localhost:8080/users/{user_id}')
if response.status_code == 200:
data = response.json()
user = data['data']
print(f"ユーザー情報: {user['name']} ({user['age']}歳)")
elif response.status_code == 404:
print("ユーザーが見つかりません")package main
import (
"encoding/json"
"fmt"
"net/http"
)
type User struct {
ID int `json:"id"`
Name string `json:"name"`
Age int `json:"age"`
}
type UsersResponse struct {
Status string `json:"status"`
Data []User `json:"data"`
Count int `json:"count"`
}
func main() {
// 全ユーザー取得
resp, err := http.Get("http://localhost:8080/users")
if err != nil {
panic(err)
}
defer resp.Body.Close()
var usersResp UsersResponse
json.NewDecoder(resp.Body).Decode(&usersResp)
fmt.Printf("ユーザー数: %d\n", usersResp.Count)
for _, user := range usersResp.Data {
fmt.Printf("ID: %d, 名前: %s, 年齢: %d\n", user.ID, user.Name, user.Age)
}
}- 通常のリクエスト: < 10ms
- ヘルスチェック: < 5ms
現在のサンプルではレート制限は実装されていませんが、本番環境では以下の制限を推奨します:
- 1秒あたり100リクエスト
- 1時間あたり10,000リクエスト
現在のサンプルではCORSは設定されていません。フロントエンドアプリケーションから利用する場合は、適切なCORS設定を追加してください。