简体   繁体   中英

How to define routes for static file in scala playframework

I am really stuck in here, i need to know how to gives routes for static file. I have static file in the root folder of project not in public

I tried this

GET /dump.txt  controllers.Assets.at(path=".", "dump.txt") 

gives me: Compilation error, Identifier expected

code that generates file:

val pw = new PrintWriter(new File("dump.txt"))
  val result = Json.parse(qdb.sourceSystem(request.session("role")))
  val source_system = (result \ "source_system").get.as[List[String]]
  for (x ← source_system) {
    try {
      // val flagedJsonData = Json.parse(qdb.getSourceLineagesFromDBFlag(x, request.session("role")))
      // val flagData = (flagedJsonData \ "flaglist").get.as[String]
      val flagData = qdb.getSourceLineagesFromDBFlag(x, request.session("role"))
      if (flagData.isEmpty == false) {
        val myarray = flagData.split(",")
        for (variable ← myarray) {
          var dump = variable.split("::") map { y ⇒ "\"%s\"".format(y) } mkString ","
          dump = "\"%s\",".format(x) + dump
          pw.write(dump + "\n")
        }
      }
    } catch {
      case _: Throwable ⇒ println("Data Not Found for " + x)
    }
  }
  pw.close

The Play docs address this well. I am assuming that you are using Play 2.xx

You will need to add an extra route in your routes.conf file that maps to the special Assets controller. The at method will let you specify which physical file you want it to route to. If it's in your root folder, you shouldn't need to use any path spec.

Assets controller is for the serving "Assets" ie public static files.

You are trying to serve not public file that looks like dynamic file (name "dump" looks like you plan to do dumps time to time, so it's not seems to be static).

So, if you sure that your file is "asset" ie public and static then you need to put it in to the project "public" folder or subfolder and use controllers.Assets as you describe or as it is documented

In the case if you want to serve not public file that is dynamic, you need to create your own controller. I will show you a little example - you can take it and go:

app/controllers/Static.scala

package controllers

import play.api._
import play.api.mvc._

import scala.io.Source
import play.api.Play.current

class Static extends Controller {

  def file(path: String) = Action {
    var file = Play.application.getFile(path);

    if (file.exists())
      Ok(Source.fromFile(file.getCanonicalPath()).mkString);
    else
      NotFound
  }

}

in the routs

GET     /build.sbt                    controllers.Static.file(path="build.sbt")

result in the browser http://localhost:9000/build.sbt

name := """scala-assests-multy"""

version := "1.0-SNAPSHOT"

lazy val root = (project in file(".")).enablePlugins(PlayScala)

scalaVersion := "2.11.6"

libraryDependencies ++= Seq(
  jdbc,
  cache,
  ws,
  specs2 % Test
)

resolvers += "scalaz-bintray" at "http://dl.bintray.com/scalaz/releases"

// Play provides two styles of routers, one expects its actions to be injected, the
// other, legacy style, accesses its actions statically.
routesGenerator := InjectedRoutesGenerator

You can take this controller and use it for the serving your dump file with the

GET     /dump.txt                    controllers.Static.file(path="dump.txt")

This is how I bootstrap my SPA.

class Bootstrap extends Controller {
  def index = Assets.at("/public", "app/index.html")
}

For SPA static assets, I just make sure they're visible from my routes.

GET   /                controllers.Bootstrap.index
GET   /views/*file     controllers.Assets.versioned(path="/public/app/js/views", file: Asset)
GET   /*file           controllers.Assets.versioned(path="/public/app", file: Asset)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM