Skip to content

Instantly share code, notes, and snippets.

@LookBad
Created April 24, 2020 08:17
Show Gist options
  • Save LookBad/89e77c6f5a45fbe386ae89115366cfe0 to your computer and use it in GitHub Desktop.
Save LookBad/89e77c6f5a45fbe386ae89115366cfe0 to your computer and use it in GitHub Desktop.
@KtorExperimentalAPI
@KtorExperimentalLocationsAPI
fun Route.adminAuthHandler(service: AdminBusinessLogic = AdminService()) {
post<Admin.Auth.SignIn> {
val request = call.receive<AdminSignInRequest>()
if (request.isNotValid())
throw BadRequestException("Incorrect request")
val admin = service.authenticate(request.name!!, request.password!!)
val generatedToken = JwtConfig.makeToken(admin)
call.respond(AdminSignInResponse(generatedToken, admin))
}
}
@KtorExperimentalLocationsAPI
@Location("/admin")
class Admin {
@Location("/manage")
data class Manage(val admin: Admin) {
@Location("/find-user-by-phone")
data class FindUserByPhone(val manage: Manage, var phoneNumber: String, val page: Int = 1, val count: Int = 10)
@Location("/parking-contracts")
data class ParkingContracts(val manage: Manage, val page: Int = 1, val count: Int = 10)
@Location("/add-user-parking-spot")
data class AddUserParkingSpot(val manage: Manage, val page: Int = 1, val count: Int = 10)
@Location("/remove-user-parking-spot")
data class RemoveUserParkingSpot(val manage: Manage, val page: Int = 1, val count: Int = 10)
}
@Location("/auth")
data class Auth(val admin: Admin) {
@Location("/sign-in")
data class SignIn(val auth: Auth)
}
}
data class AdminSignInRequest(
val name: String?,
val password: String?
){
fun isValid() = !name.isNullOrBlank() && !password.isNullOrBlank()
}
plugins {
// Support for Kotlin
id("org.jetbrains.kotlin.jvm") version "1.3.61"
// Support for building a CLI application
application
// FatJar
id("com.github.johnrengelman.shadow") version "5.0.0"
// Documentation
id("org.jetbrains.dokka") version "0.10.1"
}
application {
mainClassName = "com.easythings.parkkometr.AppKt"
group = "com.easythings"
version = "0.0.1"
}
sourceSets {
main {
java.srcDir("app/main/src")
resources.srcDir("app/main/resources")
}
test {
java.srcDir("app/test/src/")
resources.srcDir("app/test/resources/")
}
}
repositories {
jcenter()
maven { setUrl("https://kotlin.bintray.com/ktor") }
}
dependencies {
val ktorVersion: String by project
val logbackVersion: String by project
val exposedVersion: String by project
val pgVersion: String by project
val spekVersion: String by project
val sendGridVersion: String by project
val assertJVersion: String by project
// Kotlin ==========================================================================================================
implementation(platform("org.jetbrains.kotlin:kotlin-bom"))
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
// Libs ============================================================================================================
// Ktor - framework
implementation("io.ktor", "ktor-server-jetty", ktorVersion)
implementation("io.ktor", "ktor-server-core", ktorVersion)
implementation("io.ktor", "ktor-server-host-common", ktorVersion)
implementation("io.ktor", "ktor-auth", ktorVersion)
implementation("io.ktor", "ktor-auth-jwt", ktorVersion)
implementation("io.ktor", "ktor-gson", ktorVersion)
implementation("io.ktor", "ktor-network-tls-certificates", ktorVersion)
implementation("io.ktor", "ktor-locations", ktorVersion)
// Logback - application logger
implementation("ch.qos.logback", "logback-classic", logbackVersion)
// Exposed - orm
implementation("org.jetbrains.exposed", "exposed-core", exposedVersion)
implementation("org.jetbrains.exposed", "exposed-dao", exposedVersion)
implementation("org.jetbrains.exposed", "exposed-jdbc", exposedVersion)
implementation("org.jetbrains.exposed", "exposed-jodatime", exposedVersion)
// Postgresql - database driver
implementation("org.postgresql", "postgresql", pgVersion)
// SendGrid - mailer
implementation("com.sendgrid", "sendgrid-java", sendGridVersion)
// Tests ===========================================================================================================
testImplementation("org.jetbrains.kotlin", "kotlin-test")
testImplementation("org.jetbrains.kotlin", "kotlin-test-junit")
// Ktor test lib - default
testImplementation("io.ktor", "ktor-server-tests", ktorVersion)
// Speak - test lib
testImplementation("org.spekframework.spek2", "spek-dsl-jvm", spekVersion)
testRuntimeOnly("org.spekframework.spek2", "spek-runner-junit5", spekVersion)
// Speak requires kotlin-reflect, can be omitted if already in the classpath
testRuntimeOnly("org.jetbrains.kotlin", "kotlin-reflect")
// AssertJ - more readable assertion methods
testImplementation("org.assertj", "assertj-core", assertJVersion)
}
tasks {
dokka {
outputDirectory = "$buildDir/docs/dokka"
outputFormat = "html"
}
test {
useJUnitPlatform {
includeEngines("spek2")
}
}
shadowJar {
manifest {
attributes(
mapOf("Main-Class" to application.mainClassName)
)
}
mergeServiceFiles()
}
}
kotlin.code.style=official
ktorVersion=1.3.2
kotlinVersion=1.3.61
logbackVersion=1.2.1
exposedVersion=0.21.1
pgVersion=42.2.2
spekVersion=2.0.10
sendGridVersion=4.4.5
assertJVersion=3.15.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment