Skip to content

Commit

Permalink
Remove minio as data store
Browse files Browse the repository at this point in the history
  • Loading branch information
VovaStelmashchuk committed Aug 8, 2024
1 parent 54f557d commit e05040b
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 132 deletions.
12 changes: 10 additions & 2 deletions backend/src/main/java/com/nestapp/AppComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ package com.nestapp

import com.mongodb.kotlin.client.coroutine.MongoClient
import com.nestapp.minio.MinioFileUpload
import com.nestapp.minio.ProjectRepository
import com.nestapp.minio.MinioProjectRepository
import com.nestapp.mongo.NestHistoryRepository
import com.nestapp.mongo.ProjectDatabase
import com.nestapp.mongo.ProjectRepository
import io.minio.MinioClient
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.bson.types.ObjectId

class AppComponent(
val configuration: Configuration,
Expand All @@ -17,10 +23,12 @@ class AppComponent(

val minioFileUpload = MinioFileUpload(minioClient)

val projectRepository: ProjectRepository = ProjectRepository(minioClient, minioFileUpload)
val minioProjectRepository: MinioProjectRepository = MinioProjectRepository(minioClient, minioFileUpload)

private val mongoClient = MongoClient.create(connectionString = configuration.mongoUrl)

val nestHistoryRepository = NestHistoryRepository(mongoClient)

val projectRepository = ProjectRepository(mongoClient)

}
16 changes: 13 additions & 3 deletions backend/src/main/java/com/nestapp/RestController.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package com.nestapp

import com.nestapp.files.svg.SvgWriter
import com.nestapp.minio.MinioFileUpload
import com.nestapp.nest.PolygonGenerator
import com.nestapp.nest.jaguar.JaguarRequest
import com.nestapp.nest.nestRestApi
import com.nestapp.project.ProjectMaker
import com.nestapp.project.projectsRestController
import io.ktor.client.HttpClient
import io.ktor.http.HttpMethod
import io.ktor.http.HttpStatusCode
import io.ktor.serialization.kotlinx.json.json
import io.ktor.server.application.Application
import io.ktor.server.application.call
import io.ktor.server.application.install
import io.ktor.server.plugins.autohead.AutoHeadResponse
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
import io.ktor.server.plugins.cors.routing.CORS
import io.ktor.server.plugins.statuspages.StatusPages
import io.ktor.server.response.respond
import io.ktor.server.response.respondText
import io.ktor.server.routing.Route
import io.ktor.server.routing.get
Expand All @@ -39,6 +41,13 @@ fun createHttpClient(): HttpClient {
fun Application.restConfig(
appComponent: AppComponent,
) {
install(StatusPages) {
exception<Throwable> { cause, throwable ->
println(throwable.printStackTrace())
cause.respond(HttpStatusCode.InternalServerError, "Error: $throwable")
}
}

install(AutoHeadResponse)

install(CORS) {
Expand Down Expand Up @@ -69,18 +78,19 @@ fun Route.setupRouter(
) {
projectsRestController(
configuration = appComponent.configuration,
projectRepository = appComponent.projectRepository,
projectMaker = ProjectMaker(
minioProjectRepository = appComponent.minioProjectRepository,
projectRepository = appComponent.projectRepository,
svgWriter = SvgWriter(),
polygonGenerator = PolygonGenerator(),
),
projectRepository = appComponent.projectRepository,
)

nestRestApi(
jaguarRequest = JaguarRequest(client),
polygonGenerator = PolygonGenerator(),
projectRepository = appComponent.projectRepository,
minioProjectRepository = appComponent.minioProjectRepository,
nestHistoryRepository = appComponent.nestHistoryRepository,
configuration = appComponent.configuration,
minioFileUpload = appComponent.minioFileUpload,
Expand Down
38 changes: 38 additions & 0 deletions backend/src/main/java/com/nestapp/minio/MinioProjectRepository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.nestapp.minio

import io.minio.GetObjectArgs
import io.minio.MinioClient
import io.minio.errors.MinioException
import java.io.InputStream

class MinioProjectRepository(
private val minioClient: MinioClient,
private val minioFileUpload: MinioFileUpload,
) {

companion object {
private const val BUCKET_NAME = "nest2d"
}


fun uploadFileToMinioByteArray(bytes: ByteArray, contentType: String, objectName: String) {
minioFileUpload.uploadFileToMinioByteArray(bytes, contentType, objectName)
}

fun getDxfFileAsStream(projectSlug: String, fileName: String): InputStream? {
return try {
minioClient.getObject(
GetObjectArgs.builder()
.bucket(BUCKET_NAME)
.`object`("projects/$projectSlug/files/$fileName.dxf")
.build()
)
} catch (e: MinioException) {
println("Error occurred: ${e.message}")
return null
} catch (e: Exception) {
e.printStackTrace()
return null
}
}
}
99 changes: 0 additions & 99 deletions backend/src/main/java/com/nestapp/minio/ProjectRepository.kt

This file was deleted.

42 changes: 42 additions & 0 deletions backend/src/main/java/com/nestapp/mongo/ProjectRepository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.nestapp.mongo

import com.mongodb.client.model.Filters
import com.mongodb.kotlin.client.coroutine.MongoClient
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.toList
import org.bson.codecs.pojo.annotations.BsonId
import org.bson.types.ObjectId

class ProjectRepository(
private val client: MongoClient
) {

private val database by lazy { client.getDatabase("nest2d") }

suspend fun insertProject(projectDatabase: ProjectDatabase) {
val collection = database.getCollection<ProjectDatabase>(collectionName = "projects")
collection.insertOne(projectDatabase)
}

suspend fun getProjects(): List<ProjectDatabase> {
val collection = database.getCollection<ProjectDatabase>(collectionName = "projects")
return collection.find().toList()
}

suspend fun getProject(slug: String): ProjectDatabase {
val collection = database.getCollection<ProjectDatabase>(collectionName = "projects")
val query = Filters.eq("projectSlug", slug)
return collection.find(query).firstOrNull()
?: throw IllegalArgumentException("Project not found $slug")
}
}

data class ProjectDatabase(
@BsonId
val id: ObjectId,
val projectSlug: String,
val name: String,
val files: List<String>,
val preview: String?,
)

6 changes: 3 additions & 3 deletions backend/src/main/java/com/nestapp/nest/NestRestApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.nestapp.files.dxf.DxfWriter
import com.nestapp.files.dxf.reader.DXFReader
import com.nestapp.files.svg.SvgWriter
import com.nestapp.minio.MinioFileUpload
import com.nestapp.minio.ProjectRepository
import com.nestapp.minio.MinioProjectRepository
import com.nestapp.mongo.NestHistoryRepository
import com.nestapp.nest.jaguar.JaguarNestInput
import com.nestapp.nest.jaguar.JaguarRequest
Expand All @@ -23,7 +23,7 @@ import org.bson.types.ObjectId
fun Route.nestRestApi(
jaguarRequest: JaguarRequest,
polygonGenerator: PolygonGenerator,
projectRepository: ProjectRepository,
minioProjectRepository: MinioProjectRepository,
nestHistoryRepository: NestHistoryRepository,
configuration: Configuration,
minioFileUpload: MinioFileUpload,
Expand All @@ -38,7 +38,7 @@ fun Route.nestRestApi(
}
.flatMap { (file, count) ->
val dxfReader = DXFReader()
dxfReader.parseFile(projectRepository.getDxfFileAsStream(nestInput.projectSlug, file))
dxfReader.parseFile(minioProjectRepository.getDxfFileAsStream(nestInput.projectSlug, file))
val entities = dxfReader.entities
polygonGenerator.getMergedAndCombinedPolygons(entities, nestInput.tolerance).map {
JaguarNestInput.NestInputPolygons(
Expand Down
33 changes: 18 additions & 15 deletions backend/src/main/java/com/nestapp/project/ProjectDetails.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.nestapp.project

import com.nestapp.Configuration
import com.nestapp.minio.ProjectRepository
import com.nestapp.mongo.ProjectRepository
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.call
import io.ktor.server.plugins.NotFoundException
Expand All @@ -18,22 +18,25 @@ fun Route.projectDetails(
get("/project/{project_slug}") {
val slug = call.parameters["project_slug"] ?: throw NotFoundException("Project not found")

val projectFiles = projectRepository.getProjectSvgFiles(slug)
.distinct()
try {
val projectFiles = projectRepository.getProject(slug)

val response = ProjectDetailsResponse(
slug = slug,
name = slug,
files = projectFiles
.map { fileName ->
ProjectDetailsResponse.ProjectFile(
name = fileName,
preview = "${configuration.baseUrl}/files/projects/${slug}/files/$fileName.svg",
)
}
)
val response = ProjectDetailsResponse(
slug = slug,
name = slug,
files = projectFiles.files
.map { fileName ->
ProjectDetailsResponse.ProjectFile(
name = fileName,
preview = "${configuration.baseUrl}/files/projects/${slug}/files/$fileName.svg",
)
}
)

call.respond(HttpStatusCode.OK, response)
call.respond(HttpStatusCode.OK, response)
} catch (e: Exception) {
e.printStackTrace()
}
}
}

Expand Down
Loading

0 comments on commit e05040b

Please sign in to comment.