Skip to content

Commit

Permalink
feat: connect to catalog-mongodb
Browse files Browse the repository at this point in the history
  • Loading branch information
NilsOveTen committed Oct 20, 2023
1 parent 17d5bd9 commit 04f8b1f
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 1 deletion.
22 changes: 22 additions & 0 deletions deploy/staging/env.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: service-catalog
labels:
app: service-catalog
spec:
template:
spec:
containers:
- name: service-catalog
env:
- name: MONGO_USERNAME
value: root
- name: MONGO_PASSWORD
valueFrom:
secretKeyRef:
name: catalog-mongodb
key: ROOT_PASSWORD
- name: MONGO_SERVICE
value: staging-catalog-mongodb
3 changes: 3 additions & 0 deletions deploy/staging/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ namespace: staging
resources:
- ../base
- ingress.yaml

patchesStrategicMerge:
- env.yaml
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

<dependency>
<groupId>net.logstash.logback</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package no.digdir.service_catalog.controller

import no.digdir.service_catalog.model.Service
import no.digdir.service_catalog.mongodb.ServiceRepository
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.CrossOrigin
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping

@Controller
@CrossOrigin
@RequestMapping
class ServiceController(private val serviceRepository: ServiceRepository) {

@GetMapping(value = ["/services"])
fun getAllServices(): ResponseEntity<List<Service>> =
ResponseEntity(serviceRepository.findAll(), HttpStatus.OK)

@PostMapping(value = ["/services"], consumes = [MediaType.APPLICATION_JSON_VALUE])
fun createService(
@RequestBody service: Service
): ResponseEntity<Unit> =
ResponseEntity(locationHeaderForCreated(serviceRepository.save(service).id),HttpStatus.CREATED)

}

private fun locationHeaderForCreated(newId: String): HttpHeaders =
HttpHeaders().apply {
add(HttpHeaders.LOCATION, "/services/$newId")
add(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, HttpHeaders.LOCATION)
}
9 changes: 9 additions & 0 deletions src/main/kotlin/no/digdir/service_catalog/model/Service.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package no.digdir.service_catalog.model

import org.springframework.data.mongodb.core.mapping.Document

@Document(collection = "services")
data class Service (
val id: String,
val title: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package no.digdir.service_catalog.mongodb

import no.digdir.service_catalog.model.Service
import org.springframework.data.mongodb.repository.MongoRepository
import org.springframework.stereotype.Repository

@Repository
interface ServiceRepository : MongoRepository<Service, String?>
3 changes: 2 additions & 1 deletion src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ management:
endpoint.health.probes.enabled: true
health:
livenessState.enabled: true
readinessState.enabled: true
readinessState.enabled: true
spring.data.mongodb.uri: mongodb://${MONGO_USERNAME}:${MONGO_PASSWORD}@${MONGO_SERVICE}:27017/serviceCatalog?authSource=admin&authMechanism=SCRAM-SHA-1

0 comments on commit 04f8b1f

Please sign in to comment.