Skip to content

xwei01/drogon-chat-api

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build and install drogon (forked to allow passing the model class to the controller as a template argument):

git clone https://github.com/xD8A/drogon
cd drogon
git submodule update --init
mkdir build
cd build
cmake ..
make && sudo make install

Create the project and setup it (./{config.json,main.cc}):

drogon_ctl create project drogon-chat-api && cd ./drogon-chat-api
echo 'configure_file(config.json config.json COPYONLY)' >> CMakeLists.txt

Create tables (./schema.sql) and customize models (./models/model.json):

sqlite3 app.db < schema.sql
drogon_ctl create model ./models/
echo 'configure_file(app.db app.db COPYONLY)'>> CMakeLists.txt

Install dependencies

mkdir libs && (cd ./libs \
  && git submodule add https://github.com/trusch/libbcrypt.git \
  && git submodule add https://github.com/Thalhammer/jwt-cpp.git)
cat << 'EOF' >> CMakeLists.txt

add_subdirectory(libs/libbcrypt)
target_link_libraries(${PROJECT_NAME} PRIVATE bcrypt)
target_include_directories(${PROJECT_NAME}
        PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}
        ${CMAKE_CURRENT_SOURCE_DIR}/libs/libbcrypt/include) 
EOF
cat << 'EOF' >> CMakeLists.txt

add_subdirectory(libs/jwt-cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE jwt-cpp)
target_include_directories(${PROJECT_NAME}
        PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}
        ${CMAKE_CURRENT_SOURCE_DIR}/libs/jwt-cpp/include)
EOF

Create filter

(cd ./filters && drogon_ctl create filter LoginFilter)

Custom:

  • models (see ./models/UsersExt.{h,cc});
  • filters (see ./filters/LoginFilter.{h,cc});
  • controllers (see ./controllers/UsersCtrl.{h,cc}).

Create admin user:

sqlite3 app.db \
"INSERT INTO users (name, email, password_hash)"\
" VALUES ('admin', '[email protected]', '"\
`python3 -c 'import bcrypt;print(bcrypt.hashpw(b"password", bcrypt.gensalt()).decode())'`\
"');"

Build and run.

Fail (401):

curl -i -H "Content-Type: application/json" http://localhost:8080/users

Success:

JWT_TOKEN=`curl -s -H "Content-Type: application/json" \
  -X POST \
  -d '{"name":"admin","password":"password"}' \
  http://localhost:8080/login \
| python3 -c "import sys, json; print(json.load(sys.stdin)['token'])"` \
&& curl -i -H "Content-Type: application/json" -H "Authorization: Bearer$JWT_TOKEN" \
  http://localhost:8080/users

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 98.0%
  • CMake 1.4%
  • PLpgSQL 0.6%