Commit 8c8e4ed6 authored by Areum Kim's avatar Areum Kim
Browse files

[Issue, #36] fix application.kt

parent 71a1d65f
Pipeline #77317 failed with stages
in 2 minutes and 3 seconds
......@@ -12,32 +12,18 @@ import de.h_da.fbi.smebt.intentfinder.server.routing.summarization.summarization
import de.h_da.fbi.smebt.intentfinder.server.routing.tags.Tags
import de.h_da.fbi.smebt.intentfinder.server.routing.upload.uploadRoutes
import de.h_da.fbi.smebt.intentfinder.server.upload.registerApiRoutes
import com.squareup.moshi.JsonDataException
import de.h_da.fbi.smebt.intentfinder.server.nlp.PythonBridge
import de.h_da.fbi.smebt.intentfinder.server.nlp.dto.Summary
import de.h_da.fbi.smebt.intentfinder.server.sources.MongoData
import de.h_da.fbi.smebt.intentfinder.server.sources.MoshiReader
import io.ktor.application.*
import io.ktor.features.*
import io.ktor.gson.*
import io.ktor.http.*
import io.ktor.http.content.*
import io.ktor.request.*
import io.ktor.response.*
import io.ktor.routing.*
import io.ktor.serialization.*
import kotlinx.serialization.json.Json
import kotlin.reflect.KType
import java.io.File
import java.lang.RuntimeException
fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
fun Application.module() {
val data = MongoData()
install(OpenAPIGen) {
info {
version = "0.0.1"
......@@ -62,13 +48,7 @@ fun Application.module() {
}
install(ContentNegotiation) {
gson {
setPrettyPrinting()
}
json(Json {
prettyPrint = true
})
json()
}
install(StatusPages) {
......@@ -89,134 +69,6 @@ fun Application.module() {
get("/openapi.json") {
call.respond(GsonBuilder().create().toJson(application.openAPIGen.api.serialize()))
}
get("/summary") {
val response = PythonBridge().getSummary("test bridge",10)
call.respond(response)
}
get("/") {
call.respondText("IntentFinder is available")
}
// Definition eines Endpunkts zum Hochladen einer DOCX-Datei
post("/file/{chatbotId}") {
call.respondText("file was successful uploaded")
}
// Definition endpoint zur Änderung eine bereits existierende docx
put("/file/{chatbotId}/{id}/{filename}"){
}
// Definition eines Endpunkts zur Definition einer FAQ-Webseite mit JSON-Konfiguration
post("/faqRessource/{chatbotId}/{jsonStructure}"){
try{
//parse file title
var title =call.parameters["jsonStructure"]
if (title != null) {
title= title!!.replace(".json", "", false)
}
//set file directory
val uploadDir = "tmp"
val multipart = call.receiveMultipart()
var file = File("")
multipart.forEachPart { part ->
when (part) {
is PartData.FormItem -> {
if (part.name == "title") {
title = part.value
}
}
is PartData.FileItem -> {
val ext = File(part.originalFileName).extension
//generate file title
file = File(uploadDir, "upload-${System.currentTimeMillis()}-${title.hashCode()}.$ext")
val fileBytes = part.streamProvider().readBytes()
File(uploadDir).mkdir()
file.writeBytes(fileBytes)
}
}
part.dispose()
}
//building json String from File
val stringList = file.readLines()
var json = ""
for(element in stringList){
json+=element
}
try{
//Generate DataObject from json string
val moshiReader = MoshiReader("url")
val jsonO = moshiReader.getObjectFromJson(json)
}
catch (exception: Exception){
when(exception) {
is JsonDataException -> {
val errorMessage = "The uploaded file doesn't match the predefined structure. The only valid structure is: \n" +
" {\n" +
" \"type\": \"url\",\n" +
" \"name\": \"https://your.url\",\n" +
" \"question\" : \n " +
" {\n" +
" \"type\": \"text\",\n" +
" \"css_selector\": \"table.category > tbody td.list-title > a\"\n" +
" },\n" +
" \"answer\": \n " +
" {\n" +
" \"type\": \"[text|href]\",\n" +
" \"href_css_selector\": \"table.category > tbody td.list-title > a\",\n" +
" \"css_selector\": \"div.item-page > div:nth-child(5)\"\n" +
" }\n" +
"} \n"+
"The following field is missing or couldn't be parsed: "
call.respondText(errorMessage+exception.message.toString())
}
else -> throw exception
}
}
}
catch (exception: Exception){
call.respondText("The uploaded File can not be parsed. Please choose another .json File")
println(exception.message.toString())
}
}
// get faq with Json Configuration
get("/faqRessource"){
//Rückgabe Json Object (vgl. #38)
}
// Definition eines Endpunkts zur Definition einer Docx-Datei mit JSON-Konfiguration
post("/docxRessource/{chatbotId}/{filename}"){
}
// get docx file with Json Configuration
get("/docxRessource"){
//Rückgabe Json Object (vgl. #37)
}
get("/files"){
var allFiles: MutableList<AllFiles> = mutableListOf()
data.getAllFaqData().forEach { faqData ->
if(faqData.type == "docx"){
allFiles.add(AllFiles(faqData.name, faqData.id.toString(), faqData.status))
}
}
call.respond(allFiles)
}
// Routen ohne Funktionalität
routing{
}
}
apiRouting {
......@@ -235,7 +87,4 @@ fun Application.module() {
}
}
class InternalServerErrorException : RuntimeException()
data class AllFiles(var filename:String, var id: String, var status: String)
class InternalServerErrorException : RuntimeException()
\ No newline at end of file
package de.h_da.fbi.smebt.intentfinder.server.sources
/*
import de.h_da.fbi.smebt.intentfinder.server.module
import io.ktor.http.*
import io.ktor.server.testing.*
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
class ApplicationTest {
private val data = MongoData()
@Test
fun testRoot() {
withTestApplication({ module() }) {
handleRequest(HttpMethod.Get, "/").apply {
assertEquals(HttpStatusCode.OK, response.status())
assertEquals("IntentFinder is available", response.content)
}
}
}
@Test
fun testDocxFiles() {
withTestApplication({ module() }) {
handleRequest(HttpMethod.Get, "/files").apply {
assertEquals(HttpStatusCode.OK, response.status())
data.getAllFaqData().forEach { faqData ->
if (faqData.type == "docx")
assertTrue { response.content!!.contains(faqData.name) }
}
}
}
}
}*/
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment