removed static web page dispatcher
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
8e99789947
commit
e15ed2a7f8
@ -1,8 +0,0 @@
|
|||||||
package org.tbasket.dispatch
|
|
||||||
|
|
||||||
import zio.*
|
|
||||||
import zio.http.{Request, Response}
|
|
||||||
|
|
||||||
trait StaticWebService {
|
|
||||||
def get(r: Request): IO[Object, Response]
|
|
||||||
}
|
|
@ -1,57 +0,0 @@
|
|||||||
package org.tbasket.dispatch
|
|
||||||
|
|
||||||
import zio.*
|
|
||||||
import zio.http.*
|
|
||||||
import zio.http.model.Status
|
|
||||||
import zio.http.model.Status.*
|
|
||||||
import zio.stream.ZStream
|
|
||||||
|
|
||||||
import java.nio.file.{Files, Path}
|
|
||||||
import scala.collection.mutable
|
|
||||||
|
|
||||||
class StaticWebServicImpl(pagesLocation: Path) extends StaticWebService {
|
|
||||||
|
|
||||||
private val resources = resolveResources
|
|
||||||
|
|
||||||
override def get(r: Request) =
|
|
||||||
ZIO.attempt(pagesLocation.toString + r.url.path.toString)
|
|
||||||
.filterOrFail(!_.startsWith("/"))(Forbidden)
|
|
||||||
.map(resources.get)
|
|
||||||
.someOrFail(NotFound)
|
|
||||||
.map(content => Response(status = Ok, body = content))
|
|
||||||
.catchSome {
|
|
||||||
case status: Status =>
|
|
||||||
ZIO.attempt(Response.status(status))
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private def resolveResources: Map[String, Body] = {
|
|
||||||
|
|
||||||
val map = mutable.HashMap.empty[String, Body]
|
|
||||||
|
|
||||||
def resolveAll(loc: Path): Unit = {
|
|
||||||
Files.list(loc)
|
|
||||||
.forEach {
|
|
||||||
case d if Files.isDirectory(d) => resolveAll(d)
|
|
||||||
case f =>
|
|
||||||
val body = Body.fromFile(f.toFile)
|
|
||||||
val fullPath = f.toString
|
|
||||||
val fileName = f.getFileName.toString
|
|
||||||
val purename = fileName.take(fileName.indexOf('.'))
|
|
||||||
|
|
||||||
map.put(fullPath, body)
|
|
||||||
|
|
||||||
//also bind the dir path with the index body
|
|
||||||
if (purename == "index" || purename == f.getParent.getFileName.toString) {
|
|
||||||
val extension = fullPath.drop(fullPath.indexOf('.'))
|
|
||||||
val dirPath = f.getParent.toString
|
|
||||||
if (extension == ".html")
|
|
||||||
map.put(dirPath, body)
|
|
||||||
else map.put(dirPath + extension, body)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
resolveAll(pagesLocation)
|
|
||||||
map.toMap
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,10 +0,0 @@
|
|||||||
package org.tbasket.test
|
|
||||||
|
|
||||||
import org.tbasket.dispatch.StaticWebService
|
|
||||||
import zio.ZIO
|
|
||||||
import zio.http.model.Status.NotFound
|
|
||||||
import zio.http.{Http, Request, Response}
|
|
||||||
|
|
||||||
object TestStaticWebService extends StaticWebService {
|
|
||||||
override def get(r: Request) = ZIO.attempt(Response.status(NotFound))
|
|
||||||
}
|
|
Reference in new issue