-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(backend): add nats * fix: reconfigure Gradle * fix(backend): add common modules in Dockerfile
- Loading branch information
Showing
29 changed files
with
422 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
backend/api_gateway/src/main/kotlin/com/linkedout/backend/controller/JobsController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.linkedout.backend.controller | ||
|
||
import com.linkedout.backend.model.Job | ||
import com.linkedout.backend.service.JobService | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
|
||
@RestController | ||
@RequestMapping("/api/v1/jobs") | ||
open class JobsController(private val jobService: JobService) { | ||
@GetMapping | ||
open fun getJobs(): List<Job> { | ||
return jobService.findAll() | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
backend/api_gateway/src/main/kotlin/com/linkedout/backend/model/Job.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.linkedout.backend.model | ||
|
||
data class Job( | ||
val id: String, | ||
val title: String, | ||
val category: String | ||
) |
27 changes: 27 additions & 0 deletions
27
backend/api_gateway/src/main/kotlin/com/linkedout/backend/service/JobService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.linkedout.backend.service | ||
|
||
import com.linkedout.backend.model.Job | ||
import com.linkedout.common.service.NatsService | ||
import com.linkedout.common.utils.RequestResponseFactory | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class JobService(private val natsService: NatsService, @Value("\${app.services.jobs.subjects.findAll}") private val findAllSubject: String) { | ||
fun findAll(): List<Job> { | ||
// Request jobs from the jobs service | ||
val response = natsService.requestWithReply(findAllSubject, RequestResponseFactory.newRequest().build()) | ||
|
||
// Handle the response | ||
if (!response.hasGetJobsResponse()) { | ||
throw Exception("Invalid response") | ||
} | ||
|
||
val getJobsResponse = response.getGetJobsResponse() | ||
|
||
return getJobsResponse.jobsList | ||
.map { job -> | ||
Job(job.id, job.title, job.category) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile | ||
import org.springframework.boot.gradle.tasks.bundling.BootJar | ||
|
||
plugins { | ||
`java-library` | ||
id("org.springframework.boot") | ||
id("io.spring.dependency-management") | ||
id("org.jlleitschuh.gradle.ktlint") | ||
kotlin("jvm") | ||
kotlin("plugin.spring") | ||
kotlin("plugin.jpa") | ||
} | ||
|
||
group = "com.linkedout" | ||
version = "1.0.0-SNAPSHOT" | ||
|
||
java { | ||
sourceCompatibility = JavaVersion.VERSION_17 | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
implementation(project(":protobuf")) | ||
implementation("org.springframework.boot:spring-boot-starter") | ||
implementation("org.springframework:spring-messaging") | ||
implementation("io.nats:jnats:2.17.1") | ||
implementation("com.google.protobuf:protobuf-kotlin:3.25.0") | ||
testImplementation("org.springframework.boot:spring-boot-starter-test") | ||
} | ||
|
||
tasks.withType<KotlinCompile> { | ||
kotlinOptions { | ||
freeCompilerArgs += "-Xjsr305=strict" | ||
jvmTarget = "17" | ||
} | ||
} | ||
|
||
tasks.withType<Test> { | ||
useJUnitPlatform() | ||
} | ||
|
||
tasks.getByName<BootJar>("bootJar") { | ||
enabled = false | ||
} |
28 changes: 28 additions & 0 deletions
28
backend/common/src/main/kotlin/com/linkedout/common/service/NatsService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.linkedout.common.service | ||
|
||
import com.linkedout.proto.RequestOuterClass.Request | ||
import com.linkedout.proto.ResponseOuterClass.Response | ||
import io.nats.client.Connection | ||
import io.nats.client.Nats | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.context.annotation.Lazy | ||
import org.springframework.stereotype.Service | ||
import java.time.Duration | ||
|
||
@Service | ||
@Lazy | ||
class NatsService(@Value("\${nats.spring.server}") private val natsUrl: String, @Value("\${nats.timeout}") private val natsTimeout: Long) { | ||
val nc: Connection = Nats.connect(natsUrl) | ||
val timeout: Duration = Duration.ofMillis(natsTimeout) | ||
|
||
fun requestWithReply(subject: String, request: Request): Response { | ||
val rawResponse = nc.request(subject, request.toByteArray(), timeout) | ||
val response = Response.parseFrom(rawResponse.data) | ||
|
||
if (!response.success) { | ||
throw Exception(response.errorMessage) | ||
} | ||
|
||
return response | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...nd/common/src/main/kotlin/com/linkedout/common/stream/converter/ConverterConfiguration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.linkedout.common.stream.converter | ||
|
||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.messaging.converter.MessageConverter | ||
|
||
@Configuration | ||
class ConverterConfiguration { | ||
@Bean | ||
fun requestConverter(): MessageConverter { | ||
return RequestConverter() | ||
} | ||
|
||
@Bean | ||
fun responseConverter(): MessageConverter { | ||
return ResponseConverter() | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
backend/common/src/main/kotlin/com/linkedout/common/stream/converter/RequestConverter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.linkedout.common.stream.converter | ||
|
||
import com.linkedout.proto.RequestOuterClass | ||
import org.springframework.messaging.Message | ||
import org.springframework.messaging.converter.AbstractMessageConverter | ||
import org.springframework.util.MimeType | ||
|
||
class RequestConverter : AbstractMessageConverter(MimeType("application", "vnd.linkedout.proto-request")) { | ||
override fun supports(clazz: Class<*>): Boolean { | ||
return RequestOuterClass.Request::class.java == clazz | ||
} | ||
|
||
override fun convertFromInternal(message: Message<*>, targetClass: Class<*>, conversionHint: Any?): Any? { | ||
val payload = message.payload | ||
return payload as? RequestOuterClass.Request ?: RequestOuterClass.Request.parseFrom(payload as ByteArray) | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
backend/common/src/main/kotlin/com/linkedout/common/stream/converter/ResponseConverter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.linkedout.common.stream.converter | ||
|
||
import com.linkedout.proto.ResponseOuterClass | ||
import org.springframework.messaging.MessageHeaders | ||
import org.springframework.messaging.converter.AbstractMessageConverter | ||
import org.springframework.util.MimeType | ||
|
||
class ResponseConverter : AbstractMessageConverter(MimeType("application", "vnd.linkedout.proto-response")) { | ||
override fun supports(clazz: Class<*>): Boolean { | ||
return ResponseOuterClass.Response::class.java == clazz | ||
} | ||
|
||
override fun convertToInternal(payload: Any, headers: MessageHeaders?, conversionHint: Any?): Any? { | ||
return (payload as ResponseOuterClass.Response).toByteArray() | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
backend/common/src/main/kotlin/com/linkedout/common/utils/RequestResponseFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.linkedout.common.utils | ||
|
||
import com.linkedout.proto.RequestOuterClass | ||
import com.linkedout.proto.ResponseOuterClass | ||
import java.util.UUID | ||
|
||
class RequestResponseFactory private constructor() { | ||
companion object { | ||
fun newRequest(): RequestOuterClass.Request.Builder { | ||
return RequestOuterClass.Request.newBuilder() | ||
.setRequestId(UUID.randomUUID().toString()) | ||
} | ||
|
||
fun newSuccessfulResponse(): ResponseOuterClass.Response.Builder { | ||
return ResponseOuterClass.Response.newBuilder() | ||
.setSuccess(true) | ||
} | ||
|
||
fun newFailedResponse(message: String): ResponseOuterClass.Response.Builder { | ||
return ResponseOuterClass.Response.newBuilder() | ||
.setSuccess(false) | ||
.setErrorMessage(message) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.