Defaulting to index.html when no path specified in the URL - scala

I am using Play Framework 2.8.
I set routes to:
GET /*file controllers.Assets.at(path="/public", file="index.html")
Or:
GET / controllers.Assets.at(path="/public", file="index.html")
None of them seem to default to index.html if no path is specified.
Is there a setting in routes to default xxx.test.com to xxx.test.com/index.html?

Found the answer. I had these in the wrong order
GET / controllers.Assets.at(path="/public", file="index.html")
GET /*file controllers.Assets.at(path="/public", file)
Had the */file as the first changing them around like I did fixed the issue.
Thank you all.

Related

Sbt dist cause no access to unmanaged resources?

With my sbt - play/scala application, I have been using sbt run while developing.
Almost finished my project, and now I want to sbt dist for production purposes.
(Correct me if this is a bad idea.)
My question is here.
With my sbt run, I had the access to unmanaged resources by adding
unmanagedResourceDirectories in Assets += baseDirectory.value / "works"
to my build.sbt
However, after sbt dist the same url no longer works and sends me 404 not found error.
Not Found For request 'GET /assets/RAW/abc.png'
This "works" folder includes files that will be generated during the service, which is separate directory from the usual "public" folder.
And This is my routes FYI.
GET /assets/*file controllers.Assets.at(path="/public", file)
GET /works/*file controllers.Assets.at(path="/works/", file)
Does sbt-dist requires any additional code in build.sbt or should I fix anything?
Additional asset directories specified via unmanagedResourceDirectories
unmanagedResourceDirectories in Assets += baseDirectory.value / "works"
will also be served from public according to docs:
A nuance with sbt-web is that all assets are served from the public
folder... note that the files there will be aggregated into the target public folder
This means you need to change GET /works route from
GET /works/*file controllers.Assets.at(path="/works/", file)
to
GET /works/*file controllers.Assets.at(path="/public", file)
Additional assets should now be accessible at both
http://example.com/assets/RAW/abc.png
http://example.com/works/RAW/abc.png
You can confirm that additional assets end up under public after sbt dist by unzipping the generated package at .../target/universal, and then listing the contents of a jar under lib directory ending with -assets.jar, for example:
jar tf target/universal/play-scala-starter-example-1.0-SNAPSHOT/play-scala-starter-example.play-scala-starter-example-1.0-SNAPSHOT-assets.jar
Ok, so here is how I solved my problem.
After hours(probably days) of research, I was able to get an access to external files by adding a new function in controllers, sendfile(new file(filename), inline=true)
I also had to set routes. In my case,
GET /download/:id/:name controllers.DownloadTask.download(id: String, name: String)
After adding this url/routes info in the client, it worked well.

cant see swagger json in localhost using play-swagger repository

I have done everything the github documentation mentioned:
added plugin:
addSbtPlugin("com.iheart" %% "sbt-play-swagger" % "0.7.3")
added to my root:
lazy val root = (project in file(".")).enablePlugins(PlayScala, SwaggerPlugin) //enable plugin
added a base swagger.yml
added the library:
libraryDependencies += "org.webjars" % "swagger-ui" % "2.2.0"
added those to my routes:
### NoDocs ###
GET /docs/swagger-ui/*file controllers.Assets.at(path:String="/public/lib/swagger-ui", file:String)
### NoDocs ###
GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)
and added some swagger annotation to my routes.
now when I run the app and go to http://localhost:9000/docs/swagger-ui/index.html?url=/assets/swagger.json im supposed to see something, and all I see is a green screen...
so weird, is there something missing in the doc?
Based on your version of play-swagger it looks like you're using it with Play 2.6. This version of Play introduces Content Security Policy (CSP) headers by default. There is a known issue with Play 2.6's CSP headers and play-swagger.
If you look in your browser console you should see some CSP errors logged. If this is the case you may need to disable the headers by adding the following to your application.conf file.
play.filters.headers.contentSecurityPolicy = null
We are working on an improvement to Play's Content Security Policy header implementation so that third-party libraries like play-swagger should work more seamlessly in the future.

Play framework beginner. Added some routes and get error in InteliJ

I've created a new project and added only a few routes and I cannot compile as I get errors. Why?
routes:
GET / controllers.Application.index
POST /users controllers.Application.createUser
GET /user/{id} controllers.Application.showUser
DELETE /user/{id} controllers.Application.deleteUser

exclude play route file -generated code from WartRemover

I am using WartRemover in a play project. I want to exclude routes file (generated code from it) from Wartremover scanning. I added following but it still scans play routes generated code.
wartremoverExcluded ++= Seq("com.xxx.controllers.ReverseMyController","com.xxx.controllers.javascript.ReverseMyController","com.xxx.controllers.ref.ReverseMyController")
And it still shows wart errors from the generated code for routes play file. for e.g.
warn] /xxx/conf/routes:23: Inferred type containing Nothing
warn] PUT /service/myendpoint com.xxx.controllers.MyController.postMyData
and same for many more routes defined in the routes file.
How to exclude routes from wartremover scan?
Have you tried putting -Xprint:typer in scalacOptions to see which package is the issue. It seems to work for me when I ignore the following
wartremoverExcluded ++= Seq("Routes", "controllers.ref")
It looks like this question was asked in the context of wartremover 0.11, but if anyone finds themselves here looking for a solution for 0.12, this works for me:
wartremoverExcluded += sourceManaged.value / "main" / "routes_reverseRouting.scala"
wartremoverExcluded += sourceManaged.value / "main" / "routes_routing.scala"

Scala play - "not found: value routes" (Eclipse and IDEA)

Occasionally, and seemingly for no reason, I get "not found: value routes" compilation error in one of the view templates (after compilation).
This happens either in Eclipse or IDEA.
Googling finds this but it's not possible to add mainLang = SCALA in play 2.10 (I'm using version 2.1.2).
Cleaning the project / re-eclipsifying it / seems to work, sometimes, but is there any more permenant solution / work-around?
Thanks
Since there seems to be no answer, I'll at-least describe my workaround:
Instead of using
<link href=#routes.Assets.at("stylesheets/style.css") rel="stylesheet" type="text/css" />
in my template HTML, I'm using
<link href="assets/stylesheets/styles.css") rel="stylesheet" type="text/css" />
Since I'm not invoking routes.Assets.at, there is no issue with not finding the value routes.
(However, I'm guessing this workaround will easily crumble when I would have need of more complex templates)
This can happen if the routes file does not exist or contains no routes.
I have this working defining an Asset Controller
object Assets extends controllers.AssetsBuilder
and having the route for assets too in the routes conf:
\#Map static resources from the /public folder to the /assets URL path
GET /assets/*file premise.internet_org.controllers.Assets.at(path="/public", file)
sbt compile
Then IntelliJ loads the output of the compilation and everything just works for me.
This happens when there is no route configuration for your Assets in the routes file.
You must add this to your routes file:
GET /assets/*file controllers.Assets.at(path="/public", file)
I had such error when tried build Sihouette example project https://github.com/mohiva/play-silhouette-seed/tree/master. I commented or replaced code, that caused error. For example:
def view = silhouette.UnsecuredAction.async { implicit request: Request[AnyContent] =>
//Future.successful(Ok(views.html.signUp(SignUpForm.form)))
Future.successful(Ok)
}
After that build become successful - Twirl and Routes directories created in target/scala-2.X/. I run application and restore original code.
def view = silhouette.UnsecuredAction.async { implicit request: Request[AnyContent] =>
Future.successful(Ok(views.html.signUp(SignUpForm.form)))
}
I had added .disablePlugins(PlayLayoutPlugin) to my built.sbt "root" definition without changing the directory structure to match (see the link below explaining this). It switches from "Play application layout" to the "default sbt layout". The routes and application.conf are now expected in a different location on disk. I encountered both "not found: value routes" and "resource not found on classpath: application.conf" errors. I had copied this disablePlugins line from another project.
https://www.playframework.com/documentation/2.8.x/Anatomy#Default-sbt-layout
I had the same problem, its resolved when i drop the generated folders (target) and i restart my application
I had to go to the terminal and type "activator test" before Intellij would stop giving me these errors during IDE tests.