diff --git a/examples/form/src/Main.scala b/examples/form/src/Main.scala index 04409c2..a27abaa 100644 --- a/examples/form/src/Main.scala +++ b/examples/form/src/Main.scala @@ -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) diff --git a/examples/form/test/src/FormApiSuite.scala b/examples/form/test/src/FormApiSuite.scala index 789a748..625e300 100644 --- a/examples/form/test/src/FormApiSuite.scala +++ b/examples/form/test/src/FormApiSuite.scala @@ -8,11 +8,9 @@ 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 @@ -20,7 +18,7 @@ class FormApiSuite extends munit.FunSuite { 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() ) diff --git a/examples/html/src/Main.scala b/examples/html/src/Main.scala index e12ead5..39cdb09 100644 --- a/examples/html/src/Main.scala +++ b/examples/html/src/Main.scala @@ -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 { diff --git a/examples/json/src/Main.scala b/examples/json/src/Main.scala index bbbf6cd..28cf0be 100644 --- a/examples/json/src/Main.scala +++ b/examples/json/src/Main.scala @@ -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 = { diff --git a/examples/json/test/src/JsonApiSuite.scala b/examples/json/test/src/JsonApiSuite.scala index e284f52..a6070f8 100644 --- a/examples/json/test/src/JsonApiSuite.scala +++ b/examples/json/test/src/JsonApiSuite.scala @@ -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 { @@ -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 = """{ diff --git a/examples/oauth2/src/AppModule.scala b/examples/oauth2/src/AppModule.scala index c2f9d49..4123fae 100644 --- a/examples/oauth2/src/AppModule.scala +++ b/examples/oauth2/src/AppModule.scala @@ -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) diff --git a/examples/oauth2/src/Main.scala b/examples/oauth2/src/Main.scala index d0e0840..7b51af3 100644 --- a/examples/oauth2/src/Main.scala +++ b/examples/oauth2/src/Main.scala @@ -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}") } diff --git a/examples/oauth2/test/src/IntegrationTest.scala b/examples/oauth2/test/src/IntegrationTest.scala index b48274d..833dc95 100644 --- a/examples/oauth2/test/src/IntegrationTest.scala +++ b/examples/oauth2/test/src/IntegrationTest.scala @@ -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() diff --git a/examples/todo/src/Main.scala b/examples/todo/src/Main.scala index 2af375a..c3b1746 100644 --- a/examples/todo/src/Main.scala +++ b/examples/todo/src/Main.scala @@ -48,9 +48,11 @@ import ba.sake.validson.* } + val port = 8181 + val server = Undertow .builder() - .addHttpListener(8181, "localhost") + .addHttpListener(port, "localhost") .setHandler( ErrorHandler( CorsHandler( @@ -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