Skip to content

Go言語のサンプルWebサーバ。構造を考えるために作成。

Notifications You must be signed in to change notification settings

SRsawaguchi/go_sample_server_memoapi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GoのWebAPIサーバサンプル

Go言語でWebサーバを実装する際の基本形を作ってみました。
単純なメモを管理するサーバです。

データベースの起動

  • initdb配下のファイルでデータベースが初期化される
docker compose up

サーバの起動

※データベースが起動されている状態で実行。

make app.start
  • Ctrl+cとタイプして終了。

テスト

ユニットテスト

make test.unit

インテグレーションテスト

※データベースが起動されている状態で実行。
※サーバは起動していなくてもOK。

make test.integration

エンドポイント

GET /memo

メモの一覧を取得する。

curl http://localhost:8080/memo
サンプルレスポンス
{
  "message": "success",
  "data": [
    {
      "id": 1,
      "title": "shopping list",
      "content": "yogurt, apple, egg",
      "created_at": "2022-07-03T07:12:17.761385Z",
      "updated_at": "2022-07-03T07:12:17.761385Z",
      "deleted_at": null
    },
    {
      "id": 2,
      "title": "todo",
      "content": "check email",
      "created_at": "2022-07-03T07:12:17.761385Z",
      "updated_at": "2022-07-03T07:12:17.761385Z",
      "deleted_at": null
    },
    {
      "id": 3,
      "title": "blog idea",
      "content": "alias in shell",
      "created_at": "2022-07-03T07:12:17.761385Z",
      "updated_at": "2022-07-03T07:12:17.761385Z",
      "deleted_at": null
    }
  ]
}

GET /memo/:memo_id

:memo_idのメモを取得する。

curl http://localhost:8080/memo/1
サンプルレスポンス
{
  "message": "success",
  "data": {
    "id": 1,
    "title": "shopping list",
    "content": "yogurt, apple, egg",
    "created_at": "2022-07-03T07:12:17.761385Z",
    "updated_at": "2022-07-03T07:12:17.761385Z",
    "deleted_at": null
  }
}

POST /memo

新しいメモを追加する。
新しいメモの情報はJSONでPOSTする。

curl -X POST \
     -H "Content-Type: application/json" \
     -d '{"title": "New Memo!!", "content": "Hello, World!!"}' \
     http://localhost:8080/memo  

サンプルレスポンス
{
  "message": "success",
  "data": {
    "id": 4,
    "title": "New Memo!!",
    "content": "Hello, World!!",
    "created_at": "2022-07-06T19:39:50.399197+09:00",
    "updated_at": "2022-07-06T19:39:50.399197+09:00",
    "deleted_at": null
  }
}

About

Go言語のサンプルWebサーバ。構造を考えるために作成。

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published