Skip to content

Commit

Permalink
Allow client to control mapping between urls and files
Browse files Browse the repository at this point in the history
For example:

    class WorkbenchRouting extends com.lihaoyi.workbench.Routing {
      def route(app: Directives)
               (implicit
                settings: RoutingSettings,
                resolver: ContentTypeResolver,
                refFactory: ActorRefFactory,
                log: LoggingContext): Route = {
        app.pathPrefix("css") {
          app.getFromDirectory(s"ui/target/web/sass/main/")
        }
      }
    }

    routing := new WorkbenchRouting,
  • Loading branch information
Naksu committed Apr 4, 2016
1 parent 1c87140 commit 76a1017
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/main/scala/workbench/Plugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ object Plugin extends sbt.Plugin {
val updateBrowsers = taskKey[Unit]("Partially resets some of the stuff in the browser")
val spliceBrowsers = taskKey[Unit]("Attempts to do a live update of the code running in the browser while maintaining state")
val localUrl = settingKey[(String, Int)]("localUrl")
val routing = settingKey[Routing]("route")
private[this] val server = settingKey[Server]("local websocket server")


Expand Down Expand Up @@ -95,7 +96,8 @@ object Plugin extends sbt.Plugin {
}
}
},
server := new Server(localUrl.value._1, localUrl.value._2, bootSnippet.value),
routing := DefaultRouting,
server := new Server(localUrl.value._1, localUrl.value._2, bootSnippet.value, routing.value),
(onUnload in Global) := { (onUnload in Global).value.compose{ state =>
server.value.kill()
state
Expand Down
26 changes: 26 additions & 0 deletions src/main/scala/workbench/Routing.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.lihaoyi.workbench

import akka.actor.ActorRefFactory
import spray.routing._
import spray.routing.directives.ContentTypeResolver
import spray.util.LoggingContext

trait Routing {
def route(app: Directives)
(implicit
settings: RoutingSettings,
resolver: ContentTypeResolver,
refFactory: ActorRefFactory,
log: LoggingContext): Route
}

object DefaultRouting extends Routing {
def route(app: Directives)
(implicit
settings: RoutingSettings,
resolver: ContentTypeResolver,
refFactory: ActorRefFactory,
log: LoggingContext): Route = {
app.getFromDirectory(".")
}
}
4 changes: 2 additions & 2 deletions src/main/scala/workbench/Server.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import scala.tools.nsc.typechecker.Analyzer
import org.scalajs.core.tools.classpath.{CompleteClasspath, PartialClasspath}
import scala.tools.nsc.util.{JavaClassPath, DirectoryClassPath}

class Server(url: String, port: Int, bootSnippet: String) extends SimpleRoutingApp{
class Server(url: String, port: Int, bootSnippet: String, routing: Routing) extends SimpleRoutingApp{
implicit val system = ActorSystem(
"Workbench-System",
config = ConfigFactory.load(ActorSystem.getClass.getClassLoader),
Expand Down Expand Up @@ -111,7 +111,7 @@ class Server(url: String, port: Int, bootSnippet: String) extends SimpleRoutingA
"""
}
} ~
getFromDirectory(".")
routing.route(this)

} ~
post {
Expand Down

0 comments on commit 76a1017

Please sign in to comment.