play framework route that matches all - angularjs

I'm working on an angular app using play framework for my rest-services. Everything in the public folder is an angular app (stylesheets, javascripts, images and html). I want every request that is not for something in the stylesheets, javascripts, templates or images folder to be routed to the index.html page. This is so that angular routing can take over from there...
As a side note i can mention that I am going to place every restservice under /services/ which links to my own java controllers.
Is it possible in play framework 2.3.4 to define a route that catches all without having to use the matching elements?
This is my route defs so far:
GET / controllers.Assets.at(path="/public", file="index.html")
GET /stylesheets/*file controllers.Assets.at(path="/public/stylesheets", file)
GET /javascripts/*file controllers.Assets.at(path="/public/javascripts", file)
GET /templates/*file controllers.Assets.at(path="/public/templates", file)
GET /images/*file controllers.Assets.at(path="/public/images", file)
#this line fails
GET /* controllers.Assets.at(path="/public", file="index.html")

It's not possible to omit usage of matching elements but you can route a client via controller. The route definition looks like this:
GET /*path controllers.Application.matchAll(path)
And the corresponding controller can be implemented as follows:
public class Application extends Controller {
public static Result matchAll(String path) {
return redirect(controllers.routes.Assets.at("index.html"));
}
}
Update
If you don't want to redirect a client you can return a static resource as a stream. In this case a response MIME type is required.
public class Application extends Controller {
public static Result matchAll(String path) {
return ok(Application.class.getResourceAsStream("/public/index.html")).as("text/html");
}
}

For this task you can use onHandlerNotFound in Global class which will render some page without redirect:
import play.*;
import play.mvc.*;
import play.mvc.Http.*;
import play.libs.F.*;
import static play.mvc.Results.*;
public class Global extends GlobalSettings {
public Promise<Result> onHandlerNotFound(RequestHeader request) {
return Promise.<Result>pure(notFound(
views.html.notFoundPage.render(request.uri())
));
}
}

Answer for scala developers using playframework :)
Similar to above one about creating controller which will accept parameters and then omit them.
Example routing:
GET / controllers.Assets.at(path="/public", file="index.html")
GET /*ignoredPath ui.controller.AssetsWithIgnoredWildcard.at(path="/public", file="index.html", ignoredPath: String)
controller with assets injected by framework:
class AssetsWithIgnoredWildcard #Inject() (assets: Assets) {
def at(
path: String,
file: String,
wildcardValueToIgnore: String,
aggressiveCaching: Boolean = false): Action[AnyContent] = {
assets.at(path, file, aggressiveCaching)
}
}

Related

How to access Private files in CDN within react Project

This is for language translation on web page. I want to store and access translation files on CDN.
I want to access json or any static file and which is on CDN(content delivery network).
And that static file is private i.e. not accessible by public URL.
Need this solution for localization of an react app with quick response (hence storing static json file on CDN privately).
How can I access privately held file on CDN from react ?... is the main question
I tried i18next, i18next-http-backend.
It works with public file url.
I am not sure how to access private files from it.
Based on the docs, you can pass customHeaders options...
This means you could do something like this:
import i18next from 'i18next';
import HttpApi from 'i18next-http-backend';
i18next.use(HttpApi).init({
backend: {
loadPath: 'https://path-to-your-private-cdn',
customHeaders: {
authorization: 'some secret or whatever you need to be authorized'
}
},
});
Or alternatively use a service that offers this, like locize:
import i18next from 'i18next';
import LocizeBackend from 'i18next-locize-backend';
i18next.use(LocizeBackend).init({
backend: {
projectId: "[PROJECTID]",
apiKey: "[APIKEY]",
version: "[VERSION]",
private: true
}
});

unable to set react routing with vertx

public class BackendVerticle extends AbstractVerticle {
#Override
public void start() throws Exception {
// tag::backend[]
Router router = Router.router(vertx);
router.get().handler(StaticHandler.create()); // <3>
vertx.createHttpServer()
.requestHandler(router)
.listen(8080);
// end::backend[]
}
// tag::main[]
public static void main(String[] args) {
Vertx vertx = Vertx.vertx(); // <1>
vertx.deployVerticle(new BackendVerticle()); // <2>
}
// end::main[]
}
I have created react application with vertex following https://how-to.vertx.io/single-page-react-vertx-howto/. I have set react routing using react-router which works fine when i use the internal react node server which runs on localhost:3000 by default.But when i trid to serve static pages via vertx static handler except for default / route other routes return "Not Found"
enter image description here
When the static website is built, it consists in a single index HTML file.
The React Router allows to create paths for specific views. These paths can be interpreted by the React Router but they do not map to actual files on the backend server.
This is why you get a 404 NOT FOUND response if you try to load anything else than /.
To fix the issue, configure the Vert.x Web Router to reroute anything the StaticHandler could not load to /:
// After you've setup the other routes
router.get().handler(StaticHandler.create());
router.get().handler(rc -> rerouteToIndex(rc));
private void rerouteToIndex(RoutingContext rc) {
if (!"/".equals(rc.normalisedPath())) {
rc.reroute("/");
} else {
rc.next();
}
}

How to remove routes added by other vendormodules in SilverStripe 4?

I have installed the Blog module for SilverStripe 4, but do not want all the different routes that it has available.
I want to remove for instance the "profile", "archive" and "tag" routes. Those routes are defined by the module's BlogController class.
How can I ensure these are replaced with a HTTP 404 response?
Within your_module_folder/_config/config.yml_if you indicate it should be processed After the blog module and you define those routes it should overwrite them:
---
name: your_module
After:
- 'blog/*'
---
SilverStripe\Control\Director:
rules:
'profile/': 'MyCustomController'
'archive/': 'MyCustomController'
'tag/': 'MyCustomController'
Please review the routing documentation
The controller should only have one action that throws a 404 http error.
use SilverStripe\Control\Director;
use SilverStripe\View\Requirements;
class MyCustomController extends Controller {
private static $allowed_actions = ['index'];
public function index(HTTPRequest $request) {
return $this->httpError(404, "Not Found");
}
}

Thymleaf everywhere Spring mvc

hey everyone i have an issue with thymeleaf and my static html pages
to be more specific i have a spring mvc web application am using also spring security , well in my login page i want to use thymeleaf so can spring security communicate with client layer in the other side i don't want to include thymeleaf in my all html pages cause am going to use AngularJs
I tried to put the login in templates folder and the other's in the static folder but id doesn't work
this is my thymleaf configuration class
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.thymeleaf.spring3.SpringTemplateEngine;
import org.thymeleaf.spring3.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
#Configuration
public class ThymeleafConfig {
#Bean
public ServletContextTemplateResolver templateResolver() {
ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
resolver.setPrefix("/WEB-INF/vues/");
resolver.setSuffix(".html");
resolver.setTemplateMode("HTML5");
resolver.setOrder(1);
return resolver;
}
#Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setTemplateResolver(templateResolver());
return engine;
}
#Bean
public ThymeleafViewResolver thymeleafViewResolver() {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine());
return resolver;
}
}
my problem is i don't want Thymeleaf included in my all pages you know XHTML is pretty annoying with closing the html tag
any guide will be thankfull
Well i think you dont have to use the XHTML Syntax if u are not including thymleaf into the specific page.

nancy razor first time setup - route not found 404

Steps I took to setup project:
created new asp.net project
Install-Package Nancy.Viewengines.Razor
added Views/hello.cshtml (simple hello world html)
added MainModule.cs
hit ctrl-F5 (it returns the directory listing)
change url to localhost:41915/hello
Then I get 404 resource not found.
What am I missing?
// MainModule.cs
namespace MyProj
{
using Nancy.Routing;
using Nancy;
public class MainModule : NancyModule
{
public MainModule(IRouteCacheProvider routeCacheProvider)
{
Get["/hello"] = parameters => {
return View["hello.cshtml"];
};
}
}
}
You need the Nancy.Hosting.AspNet package too.

Resources