Skip to content

Commit

Permalink
Misc tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
Willdotwhite committed Apr 17, 2024
1 parent 75900ee commit 67e5859
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 33 deletions.
14 changes: 7 additions & 7 deletions api/src/main/kotlin/com/gmtkgamejam/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import io.ktor.http.*
import io.ktor.serialization.kotlinx.json.*
import io.ktor.server.application.*
import io.ktor.server.plugins.contentnegotiation.*
import io.ktor.server.plugins.cors.*
import io.ktor.server.plugins.cors.routing.CORS
import io.ktor.server.plugins.cors.routing.*
import kotlinx.serialization.json.Json
import org.koin.core.context.startKoin
import org.koin.environmentProperties
Expand All @@ -22,13 +21,14 @@ fun Application.module() {
modules(DatabaseModule, DiscordBotModule)
}

configureRequestHandling()
configureUserInfoRouting()
configureAuthRouting()
configureAdminRouting()
configurePostRouting()
configureFavouritesRouting()
configureAuthRouting()
configureDiscordBotRouting()
configureFavouritesRouting()
configureInfraRouting()
configurePostRouting()
configureRequestHandling()
configureUserInfoRouting()
}

fun Application.configureRequestHandling() {
Expand Down
31 changes: 31 additions & 0 deletions api/src/main/kotlin/com/gmtkgamejam/routing/InfraRoutes.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.gmtkgamejam.routing

import com.gmtkgamejam.models.posts.PostItem
import com.gmtkgamejam.respondJSON
import com.gmtkgamejam.search.OpenSearch
import com.gmtkgamejam.search.OpensearchClusterConfigurer
import com.gmtkgamejam.search.SearchItem
import com.gmtkgamejam.services.PostService
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.routing.*
import org.litote.kmongo.eq

// TODO: Auth control
fun Application.configureInfraRouting() {

val postService = PostService()

routing {
route("/infra") {
route("/se") {
get("/reset") {
OpensearchClusterConfigurer.initCluster()
// TODO: Bulk uploading!
postService.getPosts(PostItem::deletedAt eq null).map { OpenSearch.index(SearchItem(it)) }
call.respondJSON("Search engine reset complete", HttpStatusCode.OK)
}
}
}
}
}
12 changes: 2 additions & 10 deletions api/src/main/kotlin/com/gmtkgamejam/routing/PostRoutes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import io.ktor.server.auth.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import org.litote.kmongo.eq
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import kotlin.math.min
Expand All @@ -31,14 +30,6 @@ fun Application.configurePostRouting() {
val favouritesService = FavouritesService()

routing {
route("/debug") {
get {
OpenSearch.initCluster()
// TODO: Bulk!
service.getPosts(PostItem::deletedAt eq null).map { OpenSearch.index(SearchItem(it)) }
call.respondJSON("We here now", HttpStatusCode.OK)
}
}
route("/posts") {
get {
val searchParams = SearchParams(call.parameters)
Expand Down Expand Up @@ -96,7 +87,8 @@ fun Application.configurePostRouting() {
data.timezoneOffsets = data.timezoneOffsets.filter { tz -> tz >= -12 && tz <= 12 }.toSet()
}
?.let { PostItem.fromCreateDto(data) }
?.let { service.createPost(it) }
?.also { service.createPost(it) }
?.also { OpenSearch.index(SearchItem(it)) }
?.let { return@post call.respond(it) }

call.respondJSON("Post could not be created", status = HttpStatusCode.NotFound)
Expand Down
16 changes: 0 additions & 16 deletions api/src/main/kotlin/com/gmtkgamejam/search/Opensearch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ import org.apache.hc.core5.http.HttpHost
import org.opensearch.client.json.jackson.JacksonJsonpMapper
import org.opensearch.client.opensearch.OpenSearchClient
import org.opensearch.client.opensearch._types.SortOptions
import org.opensearch.client.opensearch._types.mapping.SearchAsYouTypeProperty
import org.opensearch.client.opensearch._types.query_dsl.Query
import org.opensearch.client.opensearch.core.DeleteRequest
import org.opensearch.client.opensearch.core.IndexRequest
import org.opensearch.client.opensearch.core.SearchRequest
import org.opensearch.client.opensearch.core.UpdateRequest
import org.opensearch.client.opensearch.indices.CreateIndexRequest
import org.opensearch.client.opensearch.indices.DeleteIndexRequest
import org.opensearch.client.transport.OpenSearchTransport
import org.opensearch.client.transport.httpclient5.ApacheHttpClient5TransportBuilder

Expand All @@ -30,18 +27,6 @@ object OpenSearch {
client = OpenSearchClient(transport)
}

// Manually called when needed
// TODO: Add to a superadmin dashboard somewhere
fun initCluster() {
client.indices().delete(DeleteIndexRequest.Builder().index("posts").build())

val createPostsIndex = CreateIndexRequest.Builder()
.index("posts")
.mappings { it.properties(mapOf("description_shingle" to SearchAsYouTypeProperty.Builder().build()._toProperty())) }
.build()
client.indices().create(createPostsIndex)
}

/**
* Perform a search request against OpenSearch and return the documents in the result
*/
Expand All @@ -68,7 +53,6 @@ object OpenSearch {
client.index(indexRequest)
}

// TODO: Do we need this _and_ index?
fun update(item: SearchItem) {
val upsertRequest: UpdateRequest<SearchItem, SearchItem> = UpdateRequest.Builder<SearchItem, SearchItem>()
.index("posts")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.gmtkgamejam.search

import org.apache.hc.core5.http.HttpHost
import org.opensearch.client.json.jackson.JacksonJsonpMapper
import org.opensearch.client.opensearch.OpenSearchClient
import org.opensearch.client.opensearch._types.mapping.SearchAsYouTypeProperty
import org.opensearch.client.opensearch.indices.CreateIndexRequest
import org.opensearch.client.opensearch.indices.DeleteIndexRequest
import org.opensearch.client.transport.OpenSearchTransport
import org.opensearch.client.transport.httpclient5.ApacheHttpClient5TransportBuilder

object OpensearchClusterConfigurer {

private val client: OpenSearchClient

init {
val transport: OpenSearchTransport = ApacheHttpClient5TransportBuilder
.builder(HttpHost("http", "localhost", 9200))
.setMapper(JacksonJsonpMapper())
.build()

client = OpenSearchClient(transport)
}

// Manually called when needed
// TODO: Add to a superadmin dashboard somewhere
/**
* Initialise the cluster indexes
*
* Call to set up/refresh the cluster's `posts` index - all data will be lost
*/
fun initCluster() {
initIndices()
}

private fun initIndices() {
client.indices().delete(DeleteIndexRequest.Builder().index("posts").build())

val createPostsIndex = CreateIndexRequest.Builder()
.index("posts")
.mappings { it.properties(mapOf("description_shingle" to SearchAsYouTypeProperty.Builder().build()._toProperty())) }
.build()

client.indices().create(createPostsIndex)
}

}

0 comments on commit 67e5859

Please sign in to comment.