Skip to content

Commit

Permalink
Clean up example modules
Browse files Browse the repository at this point in the history
  • Loading branch information
sake92 committed Aug 31, 2023
1 parent 7535023 commit 98b3a98
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 40 deletions.
12 changes: 6 additions & 6 deletions examples/form/src/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ import ba.sake.validson.*

@main def main: Unit = {

val server = FormApiModule(8181).server
server.start()

val serverInfo = server.getListenerInfo().get(0)
val url = s"${serverInfo.getProtcol}:/${serverInfo.getAddress}"
println(s"Started HTTP server at $url")
val module = FormApiModule(8181)
module.server.start()

println(s"Started HTTP server at ${module.baseUrl}")
}

class FormApiModule(port: Int) {

val baseUrl = s"http://localhost:${port}"

private val routes: Routes = { case POST() -> Path("form") =>
val req = Request.current.bodyForm[CreateCustomerForm].validateOrThrow
val fileAsString = Files.readString(req.file)
Expand Down
6 changes: 2 additions & 4 deletions examples/form/test/src/FormApiSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,17 @@ class FormApiSuite extends munit.FunSuite {

override def munitFixtures = List(moduleFixture)

test("customer can be created") {
test("Customer can be created") {

val module = moduleFixture()
val serverInfo = module.server.getListenerInfo().get(0)
val baseUrl = s"${serverInfo.getProtcol}:/${serverInfo.getAddress}"

val exampleFile =
Resource.fromClassPath("example.txt").get.asInstanceOf[Resource.ClasspathResource].underlying.getFile.toPath

val reqBody =
CreateCustomerForm("Meho", exampleFile, CreateAddressForm("street123ž"), List("hobby1", "hobby2"))
val res = requests.post(
s"$baseUrl/form",
s"${module.baseUrl}/form",
data = reqBody.toFormDataMap().toRequestsMultipart()
)

Expand Down
9 changes: 4 additions & 5 deletions examples/html/src/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,17 @@ import scalatags.Text.all._
Response.withBody(MyPage)
}

val port = 8181

val server = Undertow
.builder()
.addHttpListener(8181, "localhost")
.addHttpListener(port, "localhost")
.setHandler(ErrorHandler(RoutesHandler(routes)))
.build()

server.start()

val serverInfo = server.getListenerInfo().get(0)
val url = s"${serverInfo.getProtcol}:/${serverInfo.getAddress}"
println(s"Started HTTP server at $url")

println(s"Started HTTP server at http://localhost:${port}")
}

val MyPage = new HtmlPage {
Expand Down
10 changes: 5 additions & 5 deletions examples/json/src/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ import ba.sake.validson.*

@main def main: Unit = {

val server = JsonApiModule(8181).server
server.start()
val module = JsonApiModule(8181)
module.server.start()

val serverInfo = server.getListenerInfo().get(0)
val url = s"${serverInfo.getProtcol}:/${serverInfo.getAddress}"
println(s"Started HTTP server at $url")
println(s"Started HTTP server at ${module.baseUrl}")
}

class JsonApiModule(port: Int) {

val baseUrl = s"http://localhost:${port}"

private var db = Seq.empty[CustomerRes]

private val routes: Routes = {
Expand Down
6 changes: 2 additions & 4 deletions examples/json/test/src/JsonApiSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ class JsonApiSuite extends munit.FunSuite {

test("customers can be created and fetched") {
val module = moduleFixture()
val serverInfo = module.server.getListenerInfo().get(0)
val baseUrl = s"${serverInfo.getProtcol}:/${serverInfo.getAddress}"
val baseUrl = module.baseUrl

// first GET -> empty
locally {
Expand Down Expand Up @@ -80,8 +79,7 @@ class JsonApiSuite extends munit.FunSuite {

test("400 BadRequest when body not valid") {
val module = moduleFixture()
val serverInfo = module.server.getListenerInfo().get(0)
val baseUrl = s"${serverInfo.getProtcol}:/${serverInfo.getAddress}"
val baseUrl = module.baseUrl

// blank name not allowed
val reqBody = """{
Expand Down
2 changes: 2 additions & 0 deletions examples/oauth2/src/AppModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import org.pac4j.undertow.handler.SecurityHandler

class AppModule(port: Int, clients: Clients) {

val baseUrl = s"http://localhost:${port}"

private val securityConfig = SecurityConfig(clients)
private val securityService = new SecurityService(securityConfig.pac4jConfig)
private val appRoutes = new AppRoutes(securityService)
Expand Down
8 changes: 3 additions & 5 deletions examples/oauth2/src/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ import org.pac4j.oauth.client.*

val clients = new Clients(s"http://localhost:8181/callback", githubClient)

val server = AppModule(8181, clients).server
server.start()
val module = AppModule(8181, clients)
module.server.start()

val serverInfo = server.getListenerInfo().get(0)
val url = s"${serverInfo.getProtcol}:/${serverInfo.getAddress}"
println(s"Started HTTP server at $url")
println(s"Started HTTP server at ${module.baseUrl}")
}
7 changes: 1 addition & 6 deletions examples/oauth2/test/src/IntegrationTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,8 @@ trait IntegrationTest extends munit.FunSuite {

// assign fixture
module = AppModule(port, clients)

module.server.start()

val serverInfo = module.server.getListenerInfo().get(0)
baseUrl = s"${serverInfo.getProtcol}:/${serverInfo.getAddress}"

println(s"BASE = $baseUrl")
baseUrl = s"http://localhost:${port}"

override def afterEach(context: AfterEach): Unit =
module.server.stop()
Expand Down
10 changes: 5 additions & 5 deletions examples/todo/src/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ import ba.sake.validson.*

}

val port = 8181

val server = Undertow
.builder()
.addHttpListener(8181, "localhost")
.addHttpListener(port, "localhost")
.setHandler(
ErrorHandler(
CorsHandler(
Expand All @@ -60,12 +62,10 @@ import ba.sake.validson.*
)
)
.build()
server.start()

val serverInfo = server.getListenerInfo().get(0)
val url = s"${serverInfo.getProtcol}:/${serverInfo.getAddress}"
println(s"Started HTTP server at $url")
server.start()

println(s"Started HTTP server at http://localhost:${port}")
}

case class CreateTodo(title: String, order: Option[Int]) derives JsonRW
Expand Down

0 comments on commit 98b3a98

Please sign in to comment.