Added mongodb to microservice and docker compose

This commit is contained in:
Marcel Haag 2021-06-25 12:15:18 +02:00
parent a20e526ae5
commit 46e63c9585
22 changed files with 111 additions and 59 deletions

View File

@ -3,6 +3,7 @@ docker_reg="c4po.io"
baseDir=$(pwd)
composeKeycloak=$baseDir"/security-c4po-cfg/kc/docker-compose.keycloak.yml"
composeDatabase=$baseDir"/security-c4po-cfg/mongodb/docker-compose.mongodb.yml"
composeFrontend=$baseDir"/security-c4po-cfg/frontend/docker-compose.frontend.yml"
composeBackend=$baseDir"/security-c4po-cfg/backend/docker-compose.backend.yml"
@ -20,6 +21,7 @@ echo "-------------CLEAN UP Container---------------"
echo -e "\n"
#docker rm -f security-c4po-keycloak
#docker rm -f security-c4po-postgres-keycloak
docker rm -f security-c4po-security-c4po-db
docker rm -f security-c4po-api
docker rm -f security-c4po-angular
echo -e "\n"
@ -35,4 +37,4 @@ echo -e "\n"
echo "------------Start Docker Container------------"
echo -e "\n"
docker-compose -f ${composeKeycloak} -f ${composeBackend} -f ${composeFrontend} up
docker-compose -f ${composeKeycloak} -f ${composeDatabase} -f ${composeBackend} -f ${composeFrontend} up

View File

@ -65,8 +65,8 @@ dependencies {
implementation("org.springframework.boot:spring-boot-starter-webflux")
implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
/*implementation("org.springframework.boot:spring-boot-starter-data-mongodb-reactive")
implementation("org.springframework.boot:spring-boot-starter-data-mongodb")*/
implementation("org.springframework.boot:spring-boot-starter-data-mongodb-reactive")
implementation("org.springframework.boot:spring-boot-starter-data-mongodb")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("com.github.spotbugs:spotbugs-annotations:4.1.2")

View File

@ -2,27 +2,19 @@ package com.securityc4po.api.project
import com.fasterxml.jackson.annotation.JsonFormat
import com.securityc4po.api.ResponseBody
import org.springframework.data.mongodb.core.index.Indexed
import java.time.Instant
import java.util.UUID
data class Project(
/*
* @Indexed(background = true, unique = true)
* Can be used after adding deps for mongodb
*/
val id: String = UUID.randomUUID().toString(),
val client: String,
val title: String,
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ssZ")
/* Change to Instant after database integration */
val createdAt: String,
val tester: String? = null,
val logo: String? = null
@Indexed(background = true, unique = true)
val id: String = UUID.randomUUID().toString(),
val client: String,
val title: String,
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ssZ")
val createdAt: String = Instant.now().toString(),
val tester: String? = null,
val logo: String? = null
)
fun Project.toProjectResponseBody(): ResponseBody {
@ -30,7 +22,7 @@ fun Project.toProjectResponseBody(): ResponseBody {
"id" to id,
"client" to client,
"title" to title,
"createdAt" to createdAt.toString(),
"createdAt" to createdAt,
"tester" to tester,
"logo" to logo
)

View File

@ -4,9 +4,9 @@ import com.securityc4po.api.configuration.BC_BAD_CAST_TO_ABSTRACT_COLLECTION
import com.securityc4po.api.extensions.getLoggerFor
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings
import com.securityc4po.api.ResponseBody
import org.springframework.http.HttpHeaders
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import reactor.core.publisher.Mono
@RestController
@RequestMapping("/projects")
@ -22,8 +22,13 @@ class ProjectController(private val projectService: ProjectService) {
var logger = getLoggerFor<ProjectController>()
@GetMapping
fun getProjects(): List<Project> {
return projectService.getProjects()
fun getProjects(): Mono<ResponseEntity<List<ResponseBody>>> {
return projectService.getProjects().map {
it.map {
it.toProjectResponseBody()
}
}.map {
ResponseEntity.ok(it)
}
}
}

View File

@ -1,11 +1,26 @@
package com.securityc4po.api.project
import com.securityc4po.api.BaseEntity
import org.springframework.data.mongodb.core.mapping.Document
/*
* @Document(collection = "project")
* Can be used after adding deps for mongodb
*/
@Document(collection = "projects")
open class ProjectEntity(
data: Project
) : BaseEntity<Project>(data)
fun ProjectEntity.toProject() : Project {
return Project(
this.data.id,
this.data.client,
this.data.title,
this.data.createdAt,
this.data.tester,
this.data.logo
)
}
fun List<ProjectEntity>.toProjects(): List<Project> {
return this.map {
it.toProject()
}
}

View File

@ -0,0 +1,13 @@
package com.securityc4po.api.project
import org.springframework.data.mongodb.repository.Query
import org.springframework.data.mongodb.repository.ReactiveMongoRepository
import org.springframework.stereotype.Repository
import reactor.core.publisher.Mono
@Repository
interface ProjectRepository: ReactiveMongoRepository<ProjectEntity, String> {
@Query("{'data._id' : ?0}")
fun findProjectById(id: String): Mono<ProjectEntity>
}

View File

@ -1,39 +1,22 @@
package com.securityc4po.api.project
import com.securityc4po.api.extensions.getLoggerFor
import org.junit.BeforeClass
import org.springframework.stereotype.Service
import reactor.core.publisher.Flux
/* Remove after database is integrated */
import java.io.File
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import reactor.core.publisher.Mono
@Service
class ProjectService() {
class ProjectService(private val projectRepository: ProjectRepository) {
var logger = getLoggerFor<ProjectService>()
/* Remove after database is integrated */
val mapper = jacksonObjectMapper()
@BeforeClass
fun init() {
mapper.registerKotlinModule()
mapper.registerModule(JavaTimeModule())
}
/**
* Get all [Project]s
*
* @return list of [Project]
*/
fun getProjects(): List<Project> {
val jsonProjectsString: String = File("./src/main/resources/mocks/projects.json").readText(Charsets.UTF_8)
val jsonProjectList: List<Project> = mapper.readValue<List<Project>>(jsonProjectsString)
/* After database integration the return should be Flux of ProjectEntity */
return jsonProjectList;
fun getProjects(): Mono<List<Project>> {
return projectRepository.findAll().collectList().map {
it.map { projectEntity -> projectEntity.toProject() }
}
}
}

View File

@ -11,11 +11,10 @@ management.endpoint.health.enabled=true
management.endpoints.web.exposure.include=info, health, metrics
## Database (MONGODB) Config ##
# spring.data.mongodb.database=C4PO
# spring.data.mongodb.host=localhost
# spring.data.mongodb.port=27017
# spring.main.allow-bean-definition-overriding=true
# spring.data.mongodb.auto-index-creation=true
spring.data.mongodb.database=c4po
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.auto-index-creation=true
## IdentityProvider (Keycloak for tests) ##
spring.security.oauth2.resourceserver.jwt.issuer-uri=http://localhost:8888/auth/realms/c4po_realm_local

File diff suppressed because one or more lines are too long

View File

Before

Width:  |  Height:  |  Size: 3.9 KiB

After

Width:  |  Height:  |  Size: 3.9 KiB

View File

Before

Width:  |  Height:  |  Size: 96 KiB

After

Width:  |  Height:  |  Size: 96 KiB

View File

@ -10,7 +10,7 @@ services:
limits:
memory: "1G"
ports:
- '8443:8443'
- 8443:8443
networks:
c4po:

View File

@ -10,7 +10,7 @@ services:
limits:
memory: "1G"
ports:
- '4200:4200'
- 4200:4200
networks:
c4po:

BIN
security-c4po-cfg/mongodb/.DS_Store vendored Normal file

Binary file not shown.

View File

@ -0,0 +1,17 @@
version: '3.1'
services:
c4po-db:
image: mongo:latest
container_name: security-c4po-db
volumes:
- ../volumes/mongodb/data:/data/db
deploy:
resources:
limits:
memory: "1G"
ports:
- 27017:27017
networks:
c4po:

BIN
security-c4po-cfg/volumes/.DS_Store vendored Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.