Scala/Play/Nginx: static resource routing - scala

I've got my Play app running:
http://localhost:9000
Nginx is proxy passing it to this url:
http://localhost/Demo/
I have a problem with static assets though. For instance, this asset in html template
<link rel="stylesheet" media="screen" href="#routes.Assets.at("stylesheets/main.css")">
is going to
http://localhost/assets/stylesheets/main.css
and obviously results in not being found. If I change it to this (add /Demo in front of the url):
<link rel="stylesheet" media="screen" href="/Demo#routes.Assets.at("stylesheets/main.css")">
it will now correctly go to this url:
http://localhost/Demo/assets/stylesheets/main.css
My question is: how can I add this /Demo to all my static assets without hard codding it into my templates? I prefer to solve this using Play's routing and limit changes done to nginx conf.
I've tried adding a url prefix into application.conf like this
application.context="/Demo"
but that affected all urls not only the static ones, so not a solution. Any thoughts?
My stack:
Play Framework 2.2.1 /
Scala 2.10.3 /
Java 8 64bit /
Nginx 1.4.3 /
Ubuntu 13.10 64bit
UPDATE: SOLUTION Thanks to #biesior for providing the Java version, I've converted it to scala:
package core
import controllers.routes
object Assets {
def at(path: String): String = {
"/Demo" + routes.Assets.at(path)
}
}

Just overwite the path using own implementation of assets (ie. in utils package):
package utils;
import controllers.routes;
public class MyAssets {
public static String at(String path){
return "/Demo"+ routes.Assets.at(path).toString();
}
}
in templates:
<img src='#utils.MyAssets.at("images/logo.png")' alt="">
On the quite other hand, will target project also work in subdirectory ? If not it's just easier to use subdomains, it's possible also with localhost, just configure your nginx for that project to use ie.: http://demo.loc instead od http://localhost/Demo and add this 'domain' to your hosts file

Related

How can I import lodash library in my Vanilla JS file

I have installed the "lodash" using "npm install --save lodash" and imported in my JS file.My intention is to Deep clone of an Object and found that Lodash is the library which solve this problem. My HTML and JS files are as follows.
My HTML file is as follows.
<!DOCTYPE html>
<html lang="en">
<body>
<script type="module" src="scripts.js" ></script>
</body>
</html>
My JS file (scripts.js) is as follows:
import _ from 'lodash';
let userOne = {
name: "Siju",
userFunction:function(){
return this.name;
}
};
// Deep Copy: Start
let userTwo=_.cloneDeep(userOne);
// Deep Copy: End
userTwo.name="Johnson";
userTwo.userFunction=function(){ return this.name.length }
console.log(userOne.name);
console.log(userTwo.name);
console.log(userOne.userFunction());
console.log(userTwo.userFunction());
But i am getting following errors.
Error1: (When use: import _ from 'lodash';)
Uncaught TypeError: Failed to resolve module specifier "lodash". Relative references must start with either "/", "./", or "../".
Error2: (When use: import _ from './node_modules/lodash';)
Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.
I have spend lot of time on this and did'nt get any suitable solution. I am really frustrated on this issue because it is a silly issue but did'nt get any proper fix in the internet. Most of the explanation is seems to be with so many dependency fixes and all. But no any fixes are worked for me. So anyone can be come up with simple and proper fix for this. Thank You Very Much in Advance!
You can go to http://lodash.com/ and download the entire .js file into your current project folder and then include the following line in your HTML body paragraph.
<script src="lodash.js"> </script>
And then import it at the top of your JavaScript file in which you would like to use it.
import _ from 'lodash'

What is the physical path of Odoo webserver?

I'm building a custom odoo module that will be using several Javascript libraries.
I need to add references to those libraries (local references) but I don't know exactly where to place those libraries and how to refer to their location.
What I tried:
- I created the new module and placed the libraries inside the module directory but it didn't work.
- I also placed the libraries in the home directory of odoo.
As I understand, the problem would be solved if I could get the default directory of the webserver that odoo runs on.
If module is using js files, then you must put these files inside your module. And still if you cant reach these files from your module its your technical error and you have to fix it yourself, also note that odoo has its js libraries already
I found this page: how to add css and js files in Openerp 7 / Odoo module maybe can help you.
Below is the content.
Store files correctly:
CSS and JS files should be reside under 'static' directory in the module(the rest of subdirectory tree under 'static' is an optional convention):
static/src/css/your_file.css
static/src/js/your_file.js
Add files in manifest (v7.0) or in XML(v8.0)
Openerp v7.0 way is to add following entries in manifest (in openerp.py):
...
'css': ['static/src/css/your_file.css'],
'js': [static/src/js/your_file.js'],
...
Odoo v8.0 way is to add corresponding record in the XML:
Add XML to the manifest (openerp.py):
...
'data': [ 'your_file.xml'],
...
Then add following record in 'your_file.xml':
<data>
<template id="assets_backend" name="your_module_name assets" inherit_id="web.assets_backend">
<xpath expr="." position="inside">
<link rel='stylesheet' href="/your_module_name/static/src/css/your_file.css"/>
<script type="text/javascript" src="/your_module_name/static/src/js/your_file.js"></script>
</xpath>
</template>
....
....
</data>

Import declarations in a namespace from external module

I would like use TypeScript.NET in my angular application. I am newbie in typescript and maybe root of problem is simple.
For example I would like to use StringBuilder.
First I install TypeScript via nuget
Add ref to index.html
<script src="source/System/Text/StringBuilder.js"></script>
<script src="source/System/Text/Utility.js"></script>
Here I am confuse, I don’t use any module system (e.g commonJs, amd, etc).
TypeScript.NET nuget add to my project folder dist which constains probably dist version for amd, commonjs etc hence I reference files from source folder.
Now I want to use StringBuilder in my internal module.
module App.Company{
//in internal module
import IVersion = App.IVersion;
import IError = App.IError;
//TypeScript.NET
import StringBuilder from "../../../../source/system/text/stringbuilder";
}
Here I get compilation errror
Import declarations in a namespace cannot reference a module
What is solution?
Second how can simple reference all types from TypeScript.NET?

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.

Gwt static resource from subfolders not found

I have a problem with path to javascript in my GWT html.
I get: WARNING: No file found for: /smartajax/libs/historyjs/history.html4.js and more like this
File exists in my war/html1 dir.
It happens only if script like this above is called not from my Html1.html, but from other script that is in public (its not declared in html by <script type="text/javascript" ...>
- public_res
- smartajax
- libs
- historyjs
- war
- html1
- smartajax
- libs
- historyjs
<script type="text/javascript" src="html1/smartajax/load.smartajax.js"></script>
How to solve this?
You should ideally place all your third party scripts in same relative folder.
1) All in public/thirdparty
2) All in war/thirdparty
Your gwt public resource folder is compiled into your module's folder, and not war base directory. It will be compiled into war/module_name/html1/smartajax/... So, instead of using relative uris, how about absolute uris?
GWT.getHostPageBaseURL() -> Base url for html page, http://x.y/
GWT.getModuleBaseURL() -> Base url for gwt module, http://x.y/module_name/