使用 C++ crow, RESTful API和PostgreSQL實作一個使用者管理Web應用程式。
- 作業系統: Ubnutu 16.04.1 LTS
- 資料庫: PostgreSQL 9.5.5
- 編譯器 IDE:
- Visual Studio Code 1.8.1
- CMake 3.5.3
- C++11
- 依賴庫:
- C++ Restful API: crow
- Boost 1.59.0
- libtcmalloc-minimal4
- c++ PostgreSQL Client API : libpqxx 4.0.2
- C client API : libpq
- C++ Restful API: crow
- 測試客戶端: [RESTClient] (http://restclient.net/)
請先在本機安裝好PostgreSQL, 啟動此服務端將自動建立表格players, 資料表欄位如以下所示: (id是PostgreSQL自動產生)
以下為Players JSON的範例:
{
"id" : 1,
"username" : "carol",
"password" : "123456",
"userdata" : "123456"
}
新增使用者時, 請不要填id, 新增成功服務端將返回id, 如下圖所示:
功能 | URL | HTTP Method | Request | Response(status) | Response Body |
---|---|---|---|---|---|
新增使用者 | /api/users | POST | Players JSON | 201 (已新增), 404 (失敗) | Players JSON |
取得使用者資訊 | /api/users/:id | GET | -- | 200 (成功), 404 (無效ID) | Players JSON |
更新使用者資訊 | /api/users/:id | PUT | Players JSON | 200 (成功), 404 (無效ID) | -- |
刪除使用者 | /api/users/:id | DELETE | -- | 200 (成功), 404 (無效ID) | -- |
取得使用者列表 | /api/users/ | GET | -- | 200 (成功), 404 (無效ID) | Players JSON |
請將依賴庫[crow]放到目錄 thirdparty/inc/底下, 目錄如以下所示:
- lobby
- CMakeLists.txt
- source
- pq_conn_pool.cpp
- lobby_server.cpp
- include
- pq_conn_pool.h
- db_config.h
- thirdparty
- inc
- crow.h
- crow
- inc
用CMake編譯此範例
mkdir build
cd build
cmake ..
make
執行服務端 - 使用者管理
./LobbyServer
- [Using HTTP Methods for RESTful Services] (http://www.restapitutorial.com/lessons/httpmethods.html)
- [使用 Flask-RESTful 设计 RESTful API] (http://www.pythondoc.com/flask-restful/second.html#id1)
- [Designing a RESTful API with Python and Flask] (https://blog.miguelgrinberg.com/post/designing-a-restful-api-with-python-and-flask)
- [libpqxx 4.0.2 - pqxx::result Class Reference] (http://pqxx.org/devprojects/libpqxx/doc/4.0/html/Reference/a00082.html)
- [postgreSQL function for last inserted ID] (http://stackoverflow.com/questions/2944297/postgresql-function-for-last-inserted-id)
- [Crow is C++ microframework for web. (inspired by Python Flask)] (https://github.com/ipkn/crow)
- [Clean project management with CMake] (http://newbiz.github.io/tutorials/2011/01/27/Clean-project-management-with-CMake.html)