AdSense

網頁

2022/5/23

Golang TODO list REST API練習

使用Go建立TODO List後端REST API。


github

TODO List的CRUD REST API。

環境:

  • Go 1.18
  • HttpRouter v1.3.0
  • PostgreSQL 14
  • Liquibase 4.10.0


啟動

啟動PostgreSQL後在專案的liquibase目錄以命令列執行liquibase update在PostgreSQL資料庫建立todo資料表及初始資料然後啟動Go應用程式。

~/../go-demo/liquibase$ liquibase update


測試

用curl輸入以下命令測試。

~$ curl -X GET "http://localhost:8080/todo/1"
{"id":1,"description":"Workout","created_at":"2022-05-23T17:46:54.228359Z","deleted":false}

~$ curl -X GET "http://localhost:8080/todo?page=1"
{"page":[{"id":1,"description":"Workout","created_at":"2022-05-23T17:46:54.228359Z","deleted":false},{"id":2,"description":"Buy grocery","created_at":"2022-05-23T17:46:54.228359Z","deleted":false},{"id":3,"description":"Write blog","created_at":"2022-05-23T17:46:54.228359Z","deleted":false}],"total":3,"pages":1}

~$ curl -X POST "http://localhost:8080/todo" -H 'content-type: application/json' -d '{"description": "Send emal"}'
{"id":4,"description":"Send emal","created_at":"2022-05-23T17:48:32.123259Z","deleted":false}

~$ curl -X PUT "http://localhost:8080/todo" -H 'content-type: application/json' -d '{"id": 4, "description": "Eat breakfast"}'
{"id":4,"description":"Eat breakfast","created_at":"2022-05-23T17:48:32.123259Z","updated_at":"2022-05-23T17:49:00.058262Z","deleted":false}

~$ curl -X DELETE "http://localhost:8080/todo" -H 'content-type: application/json' -d '{"id": 4}'
{"id":4,"description":"Eat breakfast","created_at":"2022-05-23T17:48:32.123259Z","updated_at":"2022-05-23T17:49:00.058262Z","deleted":true}

~$ curl -X GET "http://localhost:8080/todo?page=1"
{"page":[{"id":1,"description":"Workout","created_at":"2022-05-23T17:46:54.228359Z","deleted":false},{"id":2,"description":"Buy grocery","created_at":"2022-05-23T17:46:54.228359Z","deleted":false},{"id":3,"description":"Write blog","created_at":"2022-05-23T17:46:54.228359Z","deleted":false}],"total":3,"pages":1}


沒有留言:

AdSense