Skip to content
This repository has been archived by the owner on Jan 5, 2021. It is now read-only.

Docker上でGo言語を用いてWebアプリケーションを作る

Notifications You must be signed in to change notification settings

tasshi-playground/docker_webapp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 

Repository files navigation

docker_webapp

JSON API on Docker

Description

Docker上でGo言語を用いてJSON APIを作成しました。
Postgresと連結して名前とメールアドレスを管理します。
20180702: Reviewを元に改善を行いました
20190223: Docker multi-stage buildsに変更しました。ローカルのgolang環境が必要なくなりました

ディレクトリ構成

.
├── README.md
├── docker-compose.yml   # docker-compose設定ファイル
└── webapp               # WebAPIコンテナ
    ├── CRUD             # PostgresのCRUD操作用Go言語パッケージ
    │   └── CRUD.go
    ├── Dockerfile
    ├── Makefile
    └── webapp.go

Requirement

#依存ソフトウェア
docker  
docker-compose

#依存コンテナ
alpine:latest
golang:alpine
postgres:alpine

Usage

サーバー側

#ビルド
$ docker-compose build

#コンテナ起動
$ docker-compose up -d

#コンテナ終了
$ docker-compose down

Hello World!!

httpリクエスト

$ curl -XGET -H 'Content-Type:application/json' http://localhost:8080/

レスポンス(HTTP ステータスコード 200)

{
    "message": "Hello World!!"
}

Create

httpリクエスト

$ curl -XPOST -H 'Content-Type:application/json' http://localhost:8080/users -d '{"name": "test", "email": "hoge@example.com" }'

レスポンス(HTTP ステータスコード 200)

{
    "id": 1,
    "name": "test",
    "email": "hoge@example.com",
    "created_at": "2018-05-27T23:30:46.061325+09:00",
    "updated_at": "2018-05-27T23:30:46.061325+09:00"
}

Update

httpリクエスト

$ curl -XPUT -H 'Content-Type:application/json' http://localhost:8080/users/1 -d '{"name": "koudaiii", "email": "hoge@example.com" }'

レスポンス(HTTP ステータスコード 200)

{
    "id": 1,
    "name": "koudaiii",
    "email": "hoge@example.com",
    "created_at": "2018-05-27T23:30:46.061325+09:00",
    "updated_at": "2018-05-27T23:31:14.140414+09:00"
}

Read

httpリクエスト

$ curl -XGET -H 'Content-Type:application/json' http://localhost:8080/users/1

レスポンス(HTTP ステータスコード 200)

{
    "id": 1,
    "name": "koudaiii",
    "email": "hoge@example.com",
    "created_at": "2018-05-27T23:30:46.061325+09:00",
    "updated_at": "2018-05-27T23:31:14.140414+09:00"
}

Read (All)

httpリクエスト

$ curl -XGET -H 'Content-Type:application/json' http://localhost:8080/users

レスポンス(HTTP ステータスコード 200)

[
    {
        "id": 1,
        "name": "koudaiii",
        "email": "hoge@example.com",
        "created_at": "2018-05-27T23:30:46.061325+09:00",
        "updated_at": "2018-05-27T23:31:14.140414+09:00"
    }
]

Delete

httpリクエスト

$ curl -XDELETE -H 'Content-Type:application/json' http://localhost:8080/users/1

レスポンス(HTTP ステータスコード 200)