Skip to content

Commit 0367ba5

Browse files
committed
add oat++ with postgres template
1 parent 852dee2 commit 0367ba5

34 files changed

+921
-443
lines changed

CMakeLists.txt

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,68 @@
11
cmake_minimum_required(VERSION 3.1)
22

3-
set(project_name my-project) ## rename your project here
3+
set(project_name example-postgresql) ## rename your project here
44

55
project(${project_name})
66

77
set(CMAKE_CXX_STANDARD 11)
88

99
add_library(${project_name}-lib
10+
src/controller/UserController.hpp
11+
src/db/UserDb.hpp
12+
src/dto/ConfigDto.hpp
13+
src/dto/PageDto.hpp
14+
src/dto/StatusDto.hpp
15+
src/dto/UserDto.hpp
16+
src/service/UserService.cpp
17+
src/service/UserService.hpp
1018
src/AppComponent.hpp
11-
src/controller/MyController.cpp
12-
src/controller/MyController.hpp
13-
src/dto/DTOs.hpp
19+
src/DatabaseComponent.hpp
20+
src/ErrorHandler.cpp
21+
src/ErrorHandler.hpp
22+
src/ServiceComponent.hpp
23+
src/SwaggerComponent.hpp
1424
)
1525

26+
target_include_directories(${project_name}-lib PUBLIC src)
27+
1628
## link libs
1729

18-
find_package(oatpp 1.2.5 REQUIRED)
30+
find_package(oatpp 1.2.5 REQUIRED)
31+
find_package(oatpp-swagger 1.2.5 REQUIRED)
32+
find_package(oatpp-postgresql 1.2.5 REQUIRED)
1933

2034
target_link_libraries(${project_name}-lib
2135
PUBLIC oatpp::oatpp
2236
PUBLIC oatpp::oatpp-test
37+
PUBLIC oatpp::oatpp-swagger
38+
PUBLIC oatpp::oatpp-postgresql
2339
)
2440

25-
target_include_directories(${project_name}-lib PUBLIC src)
41+
add_definitions(
42+
# Path to swagger-ui resources #
43+
-DOATPP_SWAGGER_RES_PATH="${oatpp-swagger_INCLUDE_DIRS}/../bin/oatpp-swagger/res"
44+
45+
# Path to config file #
46+
-DCONFIG_PATH="${CMAKE_CURRENT_LIST_DIR}/resources/config.json"
47+
48+
## Path to database migration scripts
49+
-DDATABASE_MIGRATIONS="${CMAKE_CURRENT_SOURCE_DIR}/sql"
50+
)
2651

52+
#################################################################
2753
## add executables
2854

2955
add_executable(${project_name}-exe
3056
src/App.cpp
31-
test/app/MyApiTestClient.hpp)
57+
)
3258
target_link_libraries(${project_name}-exe ${project_name}-lib)
3359
add_dependencies(${project_name}-exe ${project_name}-lib)
3460

3561
add_executable(${project_name}-test
3662
test/tests.cpp
37-
test/app/TestComponent.hpp
38-
test/app/MyApiTestClient.hpp
39-
test/MyControllerTest.cpp
40-
test/MyControllerTest.hpp
4163
)
42-
4364
target_link_libraries(${project_name}-test ${project_name}-lib)
4465
add_dependencies(${project_name}-test ${project_name}-lib)
4566

46-
set_target_properties(${project_name}-lib ${project_name}-exe ${project_name}-test PROPERTIES
47-
CXX_STANDARD 11
48-
CXX_EXTENSIONS OFF
49-
CXX_STANDARD_REQUIRED ON
50-
)
51-
5267
enable_testing()
5368
add_test(project-tests ${project_name}-test)

Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
FROM lganzzzo/alpine-cmake:latest
22

3+
RUN apk add postgresql-dev
4+
35
ADD . /service
46

57
WORKDIR /service/utility
@@ -13,4 +15,4 @@ RUN make
1315

1416
EXPOSE 8000 8000
1517

16-
ENTRYPOINT ["./my-project-exe"]
18+
ENTRYPOINT ["./example-postgresql-exe"]

Makefile

Lines changed: 0 additions & 34 deletions
This file was deleted.

OAT_PG_EXAMPLE_README.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# example-postgresql [![Build Status](https://dev.azure.com/lganzzzo/lganzzzo/_apis/build/status/oatpp.example-postgresql?branchName=master)](https://dev.azure.com/lganzzzo/lganzzzo/_build/latest?definitionId=17&branchName=master)
2+
3+
A complete example of a "CRUD" service (UserService) built with Oat++ and using oatpp ORM with PostgreSQL.
4+
5+
6+
In this example:
7+
8+
- How to create CRUD endpoints.
9+
- How to use [oatpp ORM](https://oatpp.io/docs/components/orm/) - PostgreSQL example.
10+
- How to document API with Swagger-UI and OpenApi 3.0.0.
11+
12+
More about Oat++:
13+
14+
- [Oat++ Website](https://oatpp.io/)
15+
- [Oat++ Github Repository](https://github.com/oatpp/oatpp)
16+
- [Get Started](https://oatpp.io/docs/start)
17+
18+
## Overview
19+
20+
This project is using the following oatpp modules:
21+
22+
- [oatpp](https://github.com/oatpp/oatpp)
23+
- [oatpp-swagger](https://github.com/oatpp/oatpp-swagger)
24+
- [oatpp-postgresql](https://github.com/oatpp/oatpp-postgresql)
25+
26+
### Project layout
27+
28+
```
29+
|- CMakeLists.txt // projects CMakeLists.txt
30+
|- src/
31+
| |
32+
| |- controller/ // Folder containing Controller where all endpoints are declared
33+
| |- db/ // Folder containing the database client
34+
| |- dto/ // DTOs are declared here
35+
| |- service/ // Service business logic classes (UserService)
36+
| |- ServiceComponent.hpp // Service configuration (port, ObjectMapper, Database)
37+
| |- SwaggerComponent.hpp // Configuration for swagger-ui
38+
| |- AppComponent.hpp // Service configuration is loaded here
39+
| |- DatabaseComponent.hpp // Database config
40+
| |- App.cpp // main() is here
41+
|
42+
|- test/ // test folder
43+
|- utility/install-oatpp-modules.sh // utility script to install required oatpp-modules.
44+
|- resources/config.json // configuration file with configuration profiles
45+
|- Dockerfile // Dockerfile
46+
|- docker-compose.yaml // Docker-compose with this service and postgresql
47+
```
48+
49+
---
50+
51+
## Build and Run
52+
53+
### Using CMake
54+
55+
**Requires**
56+
57+
- This example also requires the PostgreSQL package installed.
58+
- On Alpine `$ apk add postgresql-dev`
59+
- On Ubuntu `$ apt-get install postgresql-server-dev-all`
60+
61+
For more info see [oatpp-postgresql/README.md](https://github.com/oatpp/oatpp-postgresql/blob/master/README.md)
62+
- `oatpp`, `oatpp-swagger` and `oatpp-postgresql` modules installed. You may run `utility/install-oatpp-modules.sh`
63+
script to install required oatpp modules.
64+
65+
```
66+
$ mkdir build && cd build
67+
$ cmake ..
68+
$ make
69+
$ ./example-postgresql-exe # - run application.
70+
```
71+
72+
*PostgreSQL is expected running as for `dev` config profile*
73+
74+
### In Docker
75+
76+
#### Dockerfile
77+
78+
```
79+
$ docker build -t example-postgresql .
80+
$ docker run -p 8000:8000 -e CONFIG_PROFILE='dev' -t example-postgresql
81+
```
82+
83+
#### docker-compose
84+
85+
```
86+
$ docker-compose up
87+
```
88+
89+
*docker-compose will run service with `local-docker` config profile*
90+
91+
### After run
92+
93+
Go to [http://localhost:8000/swagger/ui](http://localhost:8000/swagger/ui) to try endpoints.
94+

azure-pipelines.yml

Lines changed: 4 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -13,81 +13,8 @@ jobs:
1313
clean: all
1414
steps:
1515
- script: |
16-
mkdir build
16+
docker-compose -f ci.compose.yaml build
17+
displayName: 'Compose build'
1718
- script: |
18-
git clone https://github.com/oatpp/oatpp
19-
mkdir -p oatpp/build
20-
displayName: 'Checkout - oatpp'
21-
workingDirectory: build
22-
- script: |
23-
cmake -DOATPP_BUILD_TESTS=OFF ..
24-
sudo make install
25-
displayName: 'Build - oatpp'
26-
workingDirectory: build/oatpp/build
27-
- script: |
28-
cmake ..
29-
make
30-
displayName: 'Build - Project'
31-
workingDirectory: build
32-
- script: |
33-
make test ARGS="-V"
34-
displayName: 'Test'
35-
workingDirectory: build
36-
- job: macOS
37-
displayName: 'Build - macOS'
38-
continueOnError: false
39-
pool:
40-
vmImage: 'macOS-10.14'
41-
workspace:
42-
clean: all
43-
steps:
44-
- script: |
45-
mkdir build
46-
- script: |
47-
git clone https://github.com/oatpp/oatpp
48-
mkdir -p oatpp/build
49-
displayName: 'Checkout - oatpp'
50-
workingDirectory: build
51-
- script: |
52-
cmake -DOATPP_BUILD_TESTS=OFF ..
53-
sudo make install
54-
displayName: 'Build - oatpp'
55-
workingDirectory: build/oatpp/build
56-
- script: |
57-
cmake ..
58-
make
59-
displayName: 'Build - Project'
60-
workingDirectory: build
61-
- script: |
62-
make test ARGS="-V"
63-
displayName: 'Test'
64-
workingDirectory: build
65-
- job: windows
66-
displayName: 'Build - Windows'
67-
continueOnError: false
68-
pool:
69-
vmImage: 'windows-latest'
70-
workspace:
71-
clean: all
72-
steps:
73-
- script: |
74-
MD build
75-
- script: |
76-
git clone https://github.com/oatpp/oatpp
77-
MD oatpp\build
78-
displayName: 'Checkout - oatpp'
79-
workingDirectory: build
80-
- script: |
81-
cmake -DOATPP_BUILD_TESTS=OFF ..
82-
cmake --build . --target INSTALL
83-
displayName: 'Build - oatpp'
84-
workingDirectory: build\oatpp\build
85-
- script: |
86-
cmake ..
87-
cmake --build .
88-
displayName: 'Build - Project'
89-
workingDirectory: build
90-
- script: |
91-
my-project-test.exe
92-
displayName: 'Test'
93-
workingDirectory: build\Debug\
19+
docker-compose -f ci.compose.yaml run test
20+
displayName: 'Compose run'

ci.compose.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
version: '3'
2+
3+
services:
4+
db:
5+
image: postgres
6+
restart: always
7+
environment:
8+
POSTGRES_PASSWORD: db-pass
9+
ports:
10+
- 5432:5432
11+
12+
test:
13+
build:
14+
context: .
15+
dockerfile: ci.dockerfile
16+
ports:
17+
- "8000:8000"
18+
depends_on:
19+
- db
20+
environment:
21+
CONFIG_PROFILE: local-docker

ci.dockerfile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
FROM lganzzzo/alpine-cmake:latest
2+
3+
RUN apk add postgresql-dev
4+
5+
ADD . /service
6+
7+
WORKDIR /service/utility
8+
9+
RUN ./install-oatpp-modules.sh
10+
11+
WORKDIR /service/build
12+
13+
RUN cmake ..
14+
RUN make
15+
16+
EXPOSE 8000 8000
17+
18+
ENTRYPOINT ["./example-postgresql-test"]

docker-compose.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
version: '3'
2+
3+
services:
4+
db:
5+
image: postgres
6+
restart: always
7+
environment:
8+
POSTGRES_PASSWORD: db-pass
9+
ports:
10+
- 5432:5432
11+
12+
service:
13+
build: .
14+
ports:
15+
- "8000:8000"
16+
depends_on:
17+
- db
18+
environment:
19+
CONFIG_PROFILE: local-docker

resources/config.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"dev": {
3+
"host": "0.0.0.0",
4+
"port": 8000,
5+
"swaggerHost": "localhost:8000",
6+
"dbConnectionString": "postgresql://postgres:db-pass@localhost:5432/postgres"
7+
},
8+
"local-docker": {
9+
"host": "0.0.0.0",
10+
"port": 8000,
11+
"swaggerHost": "localhost:8000",
12+
"dbConnectionString": "postgresql://postgres:db-pass@db:5432/postgres"
13+
}
14+
}

0 commit comments

Comments
 (0)