Skip to content

Commit

Permalink
Switch to ktor
Browse files Browse the repository at this point in the history
  • Loading branch information
jmfayard committed May 23, 2021
1 parent f0f73cb commit 971ab2d
Show file tree
Hide file tree
Showing 18 changed files with 1,763 additions and 23 deletions.
20 changes: 13 additions & 7 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import de.fayard.refreshVersions.core.versionFor
import io.kotless.plugin.gradle.dsl.Webapp.Route53
import io.kotless.plugin.gradle.dsl.kotless
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmCompile
import io.kotless.resource.Lambda.Config.Runtime

plugins {
kotlin("jvm")
Expand All @@ -12,37 +14,41 @@ group = packageName
version = "1.0-SNAPSHOT"

repositories {
jcenter()
mavenCentral()
jcenter()
}

dependencies {
implementation(kotlin("stdlib"))
implementation("io.kotless", "kotless-lang", versionFor("plugin.io.kotless"))
//implementation("io.kotless", "ktor-lang-aws", versionFor("plugin.io.kotless"))
implementation("io.kotless", "ktor-lang", versionFor("plugin.io.kotless"))
implementation(KotlinX.html.jvm)
}

tasks.withType<KotlinJvmCompile> {
kotlinOptions {
jvmTarget = "1.8"
languageVersion = "1.3"
apiVersion = "1.3"
languageVersion = "1.5"
apiVersion = "1.5"
}
}

kotless {
config {
bucket = "jm.kotless.bucket"
prefix = "ktor-site"


terraform {
profile = "jm.kotless.user"
region = "eu-west-1"
}
}
webapp {
route53 = Route53("ktor.site", "kotless.io")

lambda {
kotless {
packages = setOf(packageName)
}
//runtime = Runtime.GraalVM
}
}
}
79 changes: 79 additions & 0 deletions src/main/kotlin/dev/jmfayard/Server.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package dev.jmfayard


import dev.jmfayard.bootstrap.siteStatics
import io.kotless.dsl.ktor.Kotless
import io.kotless.dsl.ktor.lang.LambdaWarming
import io.kotless.dsl.ktor.lang.event.events
import io.kotless.examples.site.pages.*
import io.ktor.application.Application
import io.ktor.application.call
import io.ktor.http.ContentType
import io.ktor.response.respondText
import io.ktor.routing.get
import io.ktor.routing.route
import io.ktor.routing.routing

class Server : Kotless() {
override fun prepare(app: Application) {
app.routing {
siteStatics()

get("/") {
call.respondText(MainPages.root(), ContentType.Text.Html)
}

//Supports route inner calls
route("pages") {
get("/introduction") {
call.respondText(IntroductionPages.introduction(), ContentType.Text.Html)
}

get("/faq") {
call.respondText(FAQPages.faq(), ContentType.Text.Html)
}


route("/dsl") {
get("/overview") {
call.respondText(DSLPages.overview(), ContentType.Text.Html)
}
get("/lifecycle") {
call.respondText(DSLPages.lifecycle(), ContentType.Text.Html)
}
get("/permissions") {
call.respondText(DSLPages.permissions(), ContentType.Text.Html)
}
get("/http") {
call.respondText(DSLPages.http(), ContentType.Text.Html)
}
get("/events") {
call.respondText(DSLPages.events(), ContentType.Text.Html)
}
}

route("/plugin") {
get("/overview") {
call.respondText(PluginPages.overview(), ContentType.Text.Html)
}
get("/configuration") {
call.respondText(PluginPages.configuration(), ContentType.Text.Html)
}
get("/tasks") {
call.respondText(PluginPages.tasks(), ContentType.Text.Html)
}
get("/extensions") {
call.respondText(PluginPages.extensions(), ContentType.Text.Html)
}
}
}

}

app.events {
subscribe(LambdaWarming) {
println("Lambda warming execution")
}
}
}
}
46 changes: 46 additions & 0 deletions src/main/kotlin/dev/jmfayard/bootstrap/ContainerUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.kotless.examples.site.bootstrap

import kotlinx.html.*

fun BODY.mainContainer(body: MAIN.() -> Unit) {
main("container") {
role = "main"
body()
}
}

fun BODY.mainDoc(body: DIV.() -> Unit) {
mainContainer {
div("doc") {
body()
}
}
}

fun BODY.mainLanding(body: DIV.() -> Unit) {
mainContainer {
div("landing") {
body()
}
}
}

fun FlowContent.row(body: DIV.() -> Unit) {
div("row") {
body()
}
}

fun DIV.smCol(size: Int, body: DIV.() -> Unit) {
div("col-sm-$size") {
body()
}
}

fun FlowContent.simpleCard(classes: String = "", body: DIV.() -> Unit) {
div("card $classes") {
div("card-body") {
body()
}
}
}
87 changes: 87 additions & 0 deletions src/main/kotlin/dev/jmfayard/bootstrap/NavUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package io.kotless.examples.site.bootstrap

import kotlinx.html.*


fun BODY.navbar(body: NAV.() -> Unit) {
nav("navbar navbar-expand-sm navbar-dark bg-dark fixed-top") {
body()
}
}

fun NAV.brand(labelImg: String, text: String, iconHref: String) {
img(alt = "Kotless label", src = labelImg, classes = "img-label")
a(iconHref, classes = "navbar-brand") {
+text
}
}

fun UL.dropdown(label: String, labelsToHref: List<Pair<String, String>>) {
li("nav-item dropdown") {
a(href = "#", classes = "nav-link dropdown-toggle") {
attributes["id"] = "dropdown-dsl"
attributes["data-toggle"] = "dropdown"
attributes["aria-haspopup"] = "true"
attributes["aria-expanded"] = "false"
+label
}
div("dropdown-menu") {
attributes["aria-labelledby"] = "dropdown-dsl"
for ((btnLabel, href) in labelsToHref) {
a(href = href, classes = "dropdown-item") {
+btnLabel
}
}
}
}
}

fun BODY.siteNavbar() {
navbar {
brand("/favicon.apng", "Kotless", "/")

button(classes = "navbar-toggler collapsed") {
type = ButtonType.button
attributes["data-toggle"] = "collapse"
attributes["data-target"] = "#navbar-toggler"
attributes["aria-controls"] = "navbar-toggler"
attributes["aria-expanded"] = "false"
attributes["aria-label"] = "Toggle navigation"
span(classes = "navbar-toggler-icon") { }
}

div(classes = "navbar-collapse collapse") {
style = ""
id = "navbar-toggler"

ul("navbar-nav") {
li("nav-item") {
a(href = "/pages/introduction", classes = "nav-link") {
+"Getting Started"
}
}

dropdown("DSL", listOf(
"Overview" to "/pages/dsl/overview",
"Lifecycle" to "/pages/dsl/lifecycle",
"HTTP" to "/pages/dsl/http",
"Events" to "/pages/dsl/events",
"Permissions" to "/pages/dsl/permissions"
))

dropdown("Plugin", listOf(
"Overview" to "/pages/plugin/overview",
"Configuration" to "/pages/plugin/configuration",
"Tasks" to "/pages/plugin/tasks",
"Extensions" to "/pages/plugin/extensions"
))

li("nav-item") {
a(href = "/pages/faq", classes = "nav-link") {
+"FAQ"
}
}
}
}
}
}
61 changes: 61 additions & 0 deletions src/main/kotlin/dev/jmfayard/bootstrap/Statics.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package io.kotless.examples.site.bootstrap

import kotlinx.html.*

private var HTMLTag.integrity: String
get() = attributes["integrity"]!!
set(value) {
attributes["integrity"] = value
}

private var HTMLTag.crossorigin: String
get() = attributes["crossorigin"]!!
set(value) {
attributes["crossorigin"] = value
}


fun TagConsumer<String>.headerSite() {
head {
title {
+"Kotless Serverless Framework"
}
link("/favicon.apng", rel = "icon")
link {
href = "https://use.fontawesome.com/releases/v5.8.1/css/all.css"
rel = "stylesheet"
crossorigin = "anonymous"
integrity = "sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf"
}
link {
href = "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
rel = "stylesheet"
crossorigin = "anonymous"
integrity = "sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
}
link("/css/kotless-site.css", rel = "stylesheet")
link("/css/highlight-style.css", rel = "stylesheet")
script { src = "/js/highlight.pack.js" }
script {
src = "https://code.jquery.com/jquery-3.3.1.slim.min.js"
crossorigin = "anonymous"
integrity = "sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
}
script {
src = "https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
crossorigin = "anonymous"
integrity = "sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
}
script {
src = "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
crossorigin = "anonymous"
integrity = "sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
}

script(type = ScriptType.textJavaScript) {
unsafe {
raw("hljs.initHighlightingOnLoad();")
}
}
}
}
27 changes: 27 additions & 0 deletions src/main/kotlin/dev/jmfayard/bootstrap/Statistics.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package dev.jmfayard.bootstrap

import io.ktor.http.content.file
import io.ktor.http.content.files
import io.ktor.http.content.static
import io.ktor.http.content.staticRootFolder
import io.ktor.routing.Routing
import java.io.File


fun Routing.siteStatics() {
//There are used almost all possible definitions of static resources

static {
staticRootFolder = File("src/main/resources/static")

static("css") {
files("css")
}

static("js") {
file("highlight.pack.js", "js/highlight.pack.js")
}

file("favicon.apng")
}
}
Loading

0 comments on commit 971ab2d

Please sign in to comment.