Open8

Hummingbirdを試す

nabeyangnabeyang

UUIDのサンプル値を出す。Ctrl+Dでreplは停止できる。

% swift repl  
Welcome to Apple Swift version 6.0.3 (swiftlang-6.0.3.1.10 clang-1600.0.30.1).
Type :help for assistance.
  1> import Foundation
  2> UUID().uuidString
$R0: String = "2D7B3E3E-E21E-4A1A-86DB-96F24CE2F5DD"
nabeyangnabeyang

存在しないidでtodoを得ようとする。

curl -i http://localhost:8080/todos/2D7B3E3E-E21E-4A1A-86DB-96F24CE2F5DD

期待通り、204が返ってくる。

HTTP/1.1 204 No Content
Date: Sun, 19 Jan 2025 04:51:09 GMT
Server: Todos
nabeyangnabeyang
  @Sendable func create(request: Request, context: some RequestContext) async throws -> Todo {
    let request = try await request.decode(as: CreateRequest.self, context: context)
    return try await repository.create(
      title: request.title, order: request.order, urlPrefix: "http://localhost:8080/todos/")
  }

↑の状態でTodoを作る

 % curl -i -X POST localhost:8080/todos -d'{"title": "Read chapter on testing applications"}'
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 187
Date: Sun, 19 Jan 2025 05:07:11 GMT
Server: Todos

{"completed":false,"title":"Read chapter on testing applications","url":"http:\/\/localhost:8080\/todos\/110EC3CC-FD34-47FC-AD61-731F3E9ED76E","id":"110EC3CC-FD34-47FC-AD61-731F3E9ED76E"}%

作ったTodoを取得する

curl http://localhost:8080/todos/110EC3CC-FD34-47FC-AD61-731F3E9ED76E                     
{"id":"110EC3CC-FD34-47FC-AD61-731F3E9ED76E","url":"http:\/\/localhost:8080\/todos\/110EC3CC-FD34-47FC-AD61-731F3E9ED76E","title":"Read chapter on testing applications","completed":false}%  
nabeyangnabeyang

EditedResponseでレスポンスステータスを調整後の結果

 % curl -i -X POST localhost:8080/todos -d'{"title": "Read chapter on testing applications"}'
HTTP/1.1 201 Created
Content-Type: application/json; charset=utf-8
Content-Length: 187
Date: Sun, 19 Jan 2025 05:19:51 GMT
Server: Todos

{"completed":false,"title":"Read chapter on testing applications","id":"6BC6673A-6F2F-43B9-92F9-24E8F84DAE32","url":"http:\/\/localhost:8080\/todos\/6BC6673A-6F2F-43B9-92F9-24E8F84DAE32"}%
nabeyangnabeyang

作成

% curl -i -X POST localhost:8080/todos -d'{"title": "todo 1"}'
HTTP/1.1 201 Created
Content-Type: application/json; charset=utf-8
Content-Length: 157
Date: Sun, 19 Jan 2025 05:45:26 GMT
Server: Todos

{"title":"todo 1","id":"85B696A9-2DA1-47C6-A184-B87553EA97E2","url":"http:\/\/localhost:8080\/todos\/85B696A9-2DA1-47C6-A184-B87553EA97E2","completed":false}%

更新

$ curl -i -X PATCH localhost:8080/todos/85B696A9-2DA1-47C6-A184-B87553EA97E2 -d'{"title": "todo 2"}'
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 157
Date: Sun, 19 Jan 2025 05:46:59 GMT
Server: Todos

{"url":"http:\/\/localhost:8080\/todos\/85B696A9-2DA1-47C6-A184-B87553EA97E2","id":"85B696A9-2DA1-47C6-A184-B87553EA97E2","completed":false,"title":"todo 2"}%       

一覧取得

$ curl -i http://localhost:8080/todos                                                              
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 159
Date: Sun, 19 Jan 2025 05:47:08 GMT
Server: Todos

[{"title":"todo 2","url":"http:\/\/localhost:8080\/todos\/85B696A9-2DA1-47C6-A184-B87553EA97E2","id":"85B696A9-2DA1-47C6-A184-B87553EA97E2","completed":false}]%
nabeyangnabeyang

作成

% curl -i -X POST localhost:8080/todos -d'{"title": "todo 1"}'                                     
HTTP/1.1 201 Created
Content-Type: application/json; charset=utf-8
Content-Length: 157
Date: Sun, 19 Jan 2025 06:12:09 GMT
Server: Todos

{"title":"todo 1","url":"http:\/\/localhost:8080\/todos\/EDB13ABF-7924-4C51-904E-5A54F9285644","completed":false,"id":"EDB13ABF-7924-4C51-904E-5A54F9285644"}%

一覧取得

$ curl -i -X GET localhost:8080/todos
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 159
Date: Sun, 19 Jan 2025 06:12:23 GMT
Server: Todos

[{"id":"EDB13ABF-7924-4C51-904E-5A54F9285644","title":"todo 1","url":"http:\/\/localhost:8080\/todos\/EDB13ABF-7924-4C51-904E-5A54F9285644","completed":false}]%

削除

$ curl -i -X DELETE localhost:8080/todos/EDB13ABF-7924-4C51-904E-5A54F9285644
HTTP/1.1 200 OK
Content-Length: 0
Date: Sun, 19 Jan 2025 06:12:47 GMT
Server: Todos

一覧取得

$ curl -i -X GET localhost:8080/todos                                       
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 2
Date: Sun, 19 Jan 2025 06:12:51 GMT
Server: Todos

[]%
nabeyangnabeyang

全件削除

3件todo作成

$ curl -i -X POST localhost:8080/todos -d'{"title": "todo 1"}'               
HTTP/1.1 201 Created
Content-Type: application/json; charset=utf-8
Content-Length: 157
Date: Sun, 19 Jan 2025 06:21:16 GMT
Server: Todos

{"url":"http:\/\/localhost:8080\/todos\/8771EF69-A90C-409D-9AFF-3F003B6F0D26","id":"8771EF69-A90C-409D-9AFF-3F003B6F0D26","title":"todo 1","completed":false}%
$ curl -i -X POST localhost:8080/todos -d'{"title": "todo 2"}'
HTTP/1.1 201 Created
Content-Type: application/json; charset=utf-8
Content-Length: 157
Date: Sun, 19 Jan 2025 06:21:18 GMT
Server: Todos

{"completed":false,"title":"todo 2","url":"http:\/\/localhost:8080\/todos\/A4C0B133-50BE-4503-AC7A-0C43A0595A01","id":"A4C0B133-50BE-4503-AC7A-0C43A0595A01"}%
$ curl -i -X POST localhost:8080/todos -d'{"title": "todo 3"}'
HTTP/1.1 201 Created
Content-Type: application/json; charset=utf-8
Content-Length: 157
Date: Sun, 19 Jan 2025 06:21:19 GMT
Server: Todos

{"url":"http:\/\/localhost:8080\/todos\/BE9992B1-3245-4511-93E0-0FE2203D84DD","completed":false,"id":"BE9992B1-3245-4511-93E0-0FE2203D84DD","title":"todo 3"}%

一覧取得

$ curl -i -X GET localhost:8080/todos                                        
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 475
Date: Sun, 19 Jan 2025 06:21:24 GMT
Server: Todos

[{"url":"http:\/\/localhost:8080\/todos\/8771EF69-A90C-409D-9AFF-3F003B6F0D26","title":"todo 1","id":"8771EF69-A90C-409D-9AFF-3F003B6F0D26","completed":false},{"id":"A4C0B133-50BE-4503-AC7A-0C43A0595A01","completed":false,"title":"todo 2","url":"http:\/\/localhost:8080\/todos\/A4C0B133-50BE-4503-AC7A-0C43A0595A01"},{"id":"BE9992B1-3245-4511-93E0-0FE2203D84DD","title":"todo 3","url":"http:\/\/localhost:8080\/todos\/BE9992B1-3245-4511-93E0-0FE2203D84DD","completed":false}]%

全件削除

$ curl -i -X DELETE localhost:8080/todos                                     
HTTP/1.1 200 OK
Content-Length: 0
Date: Sun, 19 Jan 2025 06:21:37 GMT
Server: Todos

一覧取得

$ curl -i -X GET localhost:8080/todos   
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 2
Date: Sun, 19 Jan 2025 06:21:39 GMT
Server: Todos

[]%                  
nabeyangnabeyang
$ docker ps
CONTAINER ID   IMAGE                  COMMAND                   CREATED          STATUS                   PORTS                    NAMES
ddd1d7ea76f3   postgres:14.4-alpine   "docker-entrypoint.s…"   18 minutes ago   Up 8 minutes (healthy)   0.0.0.0:5433->5432/tcp   infra-db_test-1
44ce37341cad   postgres:14.4-alpine   "docker-entrypoint.s…"   18 minutes ago   Up 8 minutes             0.0.0.0:5432->5432/tcp   infra-db-1

bashを立ち上げる

$ docker exec -it infra-db-1 /bin/bash
bash-5.1#

psqlを起動

bash-5.1# psql -U pg postgres
psql (14.4)
Type "help" for help.

postgres=#