From b15b9cc217c1135feb0fa5e88d5f4c5a1b88af82 Mon Sep 17 00:00:00 2001 From: Eike Kettner Date: Mon, 30 Dec 2019 21:59:06 +0100 Subject: [PATCH] Streamline routing - put openapi doc behind `api/doc` instead of `app/doc` - don't require `index.html` for the webapp --- modules/microsite/docs/api.md | 2 +- modules/microsite/docs/doc/install.md | 4 +-- modules/microsite/docs/doc/uploading.md | 2 +- modules/microsite/docs/getit.md | 2 +- .../docspell/restserver/RestServer.scala | 5 +-- .../docspell/restserver/webapp/Flags.scala | 2 +- .../restserver/webapp/TemplateRoutes.scala | 35 ++++++++++++------- .../webapp/src/main/elm/Comp/SourceForm.elm | 2 +- 8 files changed, 33 insertions(+), 21 deletions(-) diff --git a/modules/microsite/docs/api.md b/modules/microsite/docs/api.md index 25392e6d..75907add 100644 --- a/modules/microsite/docs/api.md +++ b/modules/microsite/docs/api.md @@ -45,7 +45,7 @@ server. If it is deployed at `http://localhost:7880`, then check this url: ``` -http://localhost:7880/app/doc +http://localhost:7880/api/doc ``` ## Examples diff --git a/modules/microsite/docs/doc/install.md b/modules/microsite/docs/doc/install.md index fa8f665c..af4ba903 100644 --- a/modules/microsite/docs/doc/install.md +++ b/modules/microsite/docs/doc/install.md @@ -150,8 +150,8 @@ $ ./docspell-joex*/bin/docspell-joex /path/to/joex-config.conf ``` After starting the rest server, you can reach the web application at -path `/app/index.html`, so using default values it would be -`http://localhost:7880/app/index.html`. +path `/app`, so using default values it would be +`http://localhost:7880/app`. You should be able to create a new account and sign in. Check the [configuration page](configure.html) to further customize docspell. diff --git a/modules/microsite/docs/doc/uploading.md b/modules/microsite/docs/doc/uploading.md index dbed6b38..44734958 100644 --- a/modules/microsite/docs/doc/uploading.md +++ b/modules/microsite/docs/doc/uploading.md @@ -53,7 +53,7 @@ Example screenshot: This example shows a source with name "test". It defines two urls: -- `/app/index.html#/upload/` +- `/app#/upload/` - `/api/v1/open/upload/item/` The first points to a web page where everyone could upload files into diff --git a/modules/microsite/docs/getit.md b/modules/microsite/docs/getit.md index c8eefcbb..3059c02a 100644 --- a/modules/microsite/docs/getit.md +++ b/modules/microsite/docs/getit.md @@ -42,7 +42,7 @@ really required, but improves OCR. $ ./docspell-joex*/bin/docspell-joex ``` in the other. -4. Point your browser to: +4. Point your browser to: 5. Register a new account, sign in and try it. Check the [documentation](doc.html) for more information on how to use diff --git a/modules/restserver/src/main/scala/docspell/restserver/RestServer.scala b/modules/restserver/src/main/scala/docspell/restserver/RestServer.scala index 93ae891b..ae5b254d 100644 --- a/modules/restserver/src/main/scala/docspell/restserver/RestServer.scala +++ b/modules/restserver/src/main/scala/docspell/restserver/RestServer.scala @@ -22,17 +22,18 @@ object RestServer { blocker: Blocker )(implicit T: Timer[F], CS: ContextShift[F]): Stream[F, Nothing] = { + val templates = TemplateRoutes[F](blocker, cfg) val app = for { restApp <- RestAppImpl.create[F](cfg, connectEC, httpClientEc, blocker) - httpApp = Router( "/api/info" -> routes.InfoRoutes(), "/api/v1/open/" -> openRoutes(cfg, restApp), "/api/v1/sec/" -> Authenticate(restApp.backend.login, cfg.auth) { token => securedRoutes(cfg, restApp, token) }, + "/api/doc" -> templates.doc, "/app/assets" -> WebjarRoutes.appRoutes[F](blocker), - "/app" -> TemplateRoutes[F](blocker, cfg) + "/app" -> templates.app ).orNotFound finalHttpApp = Logger.httpApp(logHeaders = false, logBody = false)(httpApp) diff --git a/modules/restserver/src/main/scala/docspell/restserver/webapp/Flags.scala b/modules/restserver/src/main/scala/docspell/restserver/webapp/Flags.scala index 8731c635..463562dc 100644 --- a/modules/restserver/src/main/scala/docspell/restserver/webapp/Flags.scala +++ b/modules/restserver/src/main/scala/docspell/restserver/webapp/Flags.scala @@ -21,7 +21,7 @@ object Flags { cfg.appName, cfg.baseUrl, cfg.backend.signup.mode, - s"assets/docspell-webapp/${BuildInfo.version}" + s"/app/assets/docspell-webapp/${BuildInfo.version}" ) implicit val jsonEncoder: Encoder[Flags] = diff --git a/modules/restserver/src/main/scala/docspell/restserver/webapp/TemplateRoutes.scala b/modules/restserver/src/main/scala/docspell/restserver/webapp/TemplateRoutes.scala index 17d6779a..e731496f 100644 --- a/modules/restserver/src/main/scala/docspell/restserver/webapp/TemplateRoutes.scala +++ b/modules/restserver/src/main/scala/docspell/restserver/webapp/TemplateRoutes.scala @@ -21,25 +21,36 @@ object TemplateRoutes { val `text/html` = new MediaType("text", "html") + trait InnerRoutes[F[_]] { + def doc: HttpRoutes[F] + def app: HttpRoutes[F] + } + def apply[F[_]: Effect](blocker: Blocker, cfg: Config)( implicit C: ContextShift[F] - ): HttpRoutes[F] = { + ): InnerRoutes[F] = { val indexTemplate = memo(loadResource("/index.html").flatMap(loadTemplate(_, blocker))) val docTemplate = memo(loadResource("/doc.html").flatMap(loadTemplate(_, blocker))) val dsl = new Http4sDsl[F] {} import dsl._ - HttpRoutes.of[F] { - case GET -> Root / "index.html" => - for { - templ <- indexTemplate - resp <- Ok(IndexData(cfg).render(templ), `Content-Type`(`text/html`)) - } yield resp - case GET -> Root / "doc" => - for { - templ <- docTemplate - resp <- Ok(DocData().render(templ), `Content-Type`(`text/html`)) - } yield resp + new InnerRoutes[F] { + def doc = + HttpRoutes.of[F] { + case GET -> Root => + for { + templ <- docTemplate + resp <- Ok(DocData().render(templ), `Content-Type`(`text/html`)) + } yield resp + } + def app = + HttpRoutes.of[F] { + case GET -> _ => + for { + templ <- indexTemplate + resp <- Ok(IndexData(cfg).render(templ), `Content-Type`(`text/html`)) + } yield resp + } } } diff --git a/modules/webapp/src/main/elm/Comp/SourceForm.elm b/modules/webapp/src/main/elm/Comp/SourceForm.elm index 2274adb8..16a85cb0 100644 --- a/modules/webapp/src/main/elm/Comp/SourceForm.elm +++ b/modules/webapp/src/main/elm/Comp/SourceForm.elm @@ -196,7 +196,7 @@ urlInfoMessage flags model = , dd [] [ let url = - flags.config.baseUrl ++ "/app/index.html#/upload/" ++ model.source.id + flags.config.baseUrl ++ "/app#/upload/" ++ model.source.id in a [ href url, target "_blank" ] [ code [] [ text url ] ] ]