Ktor - Static content routing - kotlin

I would love to understand better how Ktor is handling the routing for static content. I have the following hierarchy in my static folder (working directory):
- static
- index.html
- (some files)
- static
- css (directory)
- js (directory)
- (some files)
I'd like to serve all of them. So I was using directly this code in routing:
static {
defaultResource("index.html", "static")
resources("static")
}
Which works very well, but the issue is that it's taking the hand on all requests including my small get:
get("/smoketest"){
call.respondText("smoke test!", ContentType.Text.Plain)
}
What would be the best to handle in general the static content in Ktor?
Here is the code
Thank you

I tried reproducing it locally and made it work with two different approaches.
Put one of these in your static block
file("*", "index.html") // single star will only resolve the first part
file("{...}", "index.html") // tailcard will match anything
Or, put the following get handler as your last route:
val html = File("index.html").readText()
get("{...}") {
call.respondText(html, ContentType.Text.Html)
}
The {...} is a tailcard and matches any request that hasn't been matched yet.
Documentation available here: http://ktor.io/features/routing.html#path
Edit:
For resources I made the following work:
fun Route.staticContent() {
static {
resource("/", "index.html")
resource("*", "index.html")
static("static") {
resources("static")
}
}
}
I can't see your static files in the repository, so here is what it looks like in my project:

Related

How to add links to resources without any entities in Spring Data Rest

I have a very loosely couplet system that takes about any json payload and saves in a mongo colection.
There are no entities to expose as resouces, but only controller endpoints
eg.
#RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Map<String, Object>> publish(#RequestBody Map<String, Object> jsonBody) {
.. save the body in mongo
....}
I still want to build a hypermedia driven app. with links for navigation and paging.
The controller therefor implements ResourceProcessor
public class PublicationController implements ResourceProcessor<RepositoryLinksResource> {
....
#Override
public RepositoryLinksResource process(RepositoryLinksResource resource) {
resource.add(linkTo(methodOn(PublicationController.class).getPublications()).withRel("publications"));
return resource;
}
The problem is that the processor never gets called ??
Putting #EnableWebMvc on a configuration class solves it (the processor gets called), but firstly that should not be necessary, and secondary the format of HAL links seems broken
eg. gets formattet as a list
links: [
{
"links":[
{
"rel":"self",
"href":"http://localhost:8080/api/publications/121212"
},
{
"rel":"findByStartTimeBetween",
"href":"http://localhost:8080/api/publications/search/findStartTimeBetween?timeStart=2015-04-10T13:44:56.437&timeEnd=2015-04-10T13:44:56.439"
}
]
}
Are there alternatives to #enableWebMvc so the processor gets called ?
Currently I'm running Spring boot v. 1.2.3
Well it turns out that the answer was quite simple.
The problem was that I had static content (resources/static/index.html)
This will suppress the hypermedia links from the root.
Moving the static content made everything thing work great.

yii create a clean app without assets

I use ./yiic webapp /path/to/name create a project, but I don't need some file that created.
Actual:
assets css images index.php index-test.php protected themes
Expected:
index.php protected
Where is template that I should to change.
If you wish to really change this you should extend ( or modify) the class WebAppCommand that is part of the framework. It can be found in
Yii
-> Framework
->cli
-> commands
->WebAppCommand.php
Instead of modifying the exisiting code i suggest you write a custom class that extends WebAppCommand class and just remove the directories in separate method that calls the run method of WebAppCommand and adds additional lines to delete the unnecessary directories.
Perhaps something like this...
<?php
class MyCustomWebAppCommand extends WebAppCommand {
private $_rootPath; // Need to redefine and compute this as thevariable is defined as private in the parent class and better not touch core classes;
public function run($args){
parent::run($args);
$path=strtr($args[0],'/\\',DIRECTORY_SEPARATOR);
if(strpos($path,DIRECTORY_SEPARATOR)===false)
$path='.'.DIRECTORY_SEPARATOR.$path;
if(basename($path)=='..')
$path.=DIRECTORY_SEPARATOR.'.';
$dir=rtrim(realpath(dirname($path)),'\\/');
if($dir===false || !is_dir($dir))
$this->usageError("The directory '$path' is not valid. Please make sure the parent directory exists.");
if(basename($path)==='.')
$this->_rootPath=$path=$dir;
else
$this->_rootPath=$path=$dir.DIRECTORY_SEPARATOR.basename($path);
$this->deleteDir($this->_rootPath.DIRECTORY_SEPARATOR."assets");
$this->deleteDir($this->_rootPath.DIRECTORY_SEPARATOR."themes");
$this->deleteDir($this->_rootPath.DIRECTORY_SEPARATOR."images");
$this->deleteDir($this->_rootPath.DIRECTORY_SEPARATOR."css");
unset($this->_rootPath.DIRECTORY_SEPARATOR."index-test.php");
}
public static function deleteDir($dirPath) {
if (! is_dir($dirPath)) {
throw new InvalidArgumentException("$dirPath must be a directory");
}
if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {
$dirPath .= '/';
}
$files = glob($dirPath . '*', GLOB_MARK);
foreach ($files as $file) {
if (is_dir($file)) {
self::deleteDir($file);
} else {
unlink($file);
}
}
rmdir($dirPath);
}
}
Finally call MyCustomWebApp instead of calling WebApp.
P.S. I would generally suggest not to extend/modify core classes without knowing what you are doing, it will break lot of things in places you won't anticipate, and upgrades become extremely difficult. Simpler in your case is to delete the files manually.

What is the difference in setBrowserUrl() and url() in Selenium 2 web driver for phpunit?

In many examples, I have seen calls made to both webdriver->setBrowserURL(url) and webdriver->url(url). Why would I want to use one instead of the other. One such example shows using both in the same manner (taken from the phpunit manual):
<?php
class WebTest extends PHPUnit_Extensions_Selenium2TestCase
{
protected function setUp()
{
$this->setBrowser('firefox');
$this->setBrowserUrl('http://www.example.com/');
}
public function testTitle()
{
$this->url('http://www.example.com/');
$this->assertEquals('Example WWW Page', $this->title());
}
}
?>
Why would setBrowserUrl() be called once in setup -- and then url() be called with the identical url in the test case itself?
In other examples, I've seen url() called with just a path for the url. What is the proper usage here? I can find almost no documentation on the use of url().
setBrowserUrl() sets a base url, allowing you to use relative paths in your tests.
The example from the phpunit manual is kind of confusing - I believe setBrowserUrl() is being used during setup simply because it'll throw an error without it:
public function start()
{
if ($this->browserUrl == NULL) {
throw new PHPUnit_Framework_Exception(
'setBrowserUrl() needs to be called before start().'
);
}
$this->url will use this base if a relative path is given.

Geb page url method from ConfigSlurper

I am trying to store the urls I need in a config file that gets pulled using ConfigSlurper. I think this may not work but not sure. Thoughts?
You are probably looking for functionality provided by baseUrl configuration. When using to MyPage the url which is used by the browser is determined by combining basUrl configuration and the url property of your page class.
If you wanted a slightly cleaner method of doing this, you could implement a base page such as the one below - inner class for brevity and to avoid calling protected methods directly - (we have apps on 26 different subdomains!):
package page.admin
import geb.Configuration
import geb.Page
class AdminPage extends Page {
class WrappedConfig extends Configuration {
WrappedConfig(ConfigObject rawConfig) {
super(rawConfig)
}
String getAdminBaseUrl() {
return readValue('adminUrl', '<invalid-url>')
}
}
String getPageUrl() {
WrappedConfig config = new WrappedConfig(browser.config.rawConfig)
return config.adminBaseUrl + this.class.url
}
}
Your config might look something like this:
baseUrl = 'http://base-app.example.net'
adminUrl = 'http://admin-app.example.com'
This way, you can still use normal geb syntax:
given:
to PageWhichExtendsAdminPage, 'your-path', key1: 'value1
to generate the url http://admin-app.example.com/your-path/?key1=value1
I run geb on different locales so I encountered the same issue. I usually load the different urls out of a config file with locale.getCountry() as parameter for the environment.
In the running class I replace the baseUrl with the loaded entry with the ConfigSlurper. The advantage is that I can handle multiple locales and localhost environments. Testing locally vs testing the staging environment.
I have one main spock file containing the whole regression testing and a inheriting spock class for every country. The inheriting spock files doesn't contain much except the country/language encoding in the class name.
The config file:
environment{
CA{
url="ca.somewhere.com"
validZipCode=...
...
}
...
}
The main class:
class MainRegression extends GebReportingSpec{
#Shared Locale locale
def setupSpec(){
...
locale = ParameterReader.getLocale(this.class.name)
...
}
def "testing the website"(){
when: "entering the main url"
go URLService.getBaseUrl(locale)
...
}
The inheriting class:
#Stepwise
class CaEnRegressionSpec{} // Canada with english language
A good way to handle the at-verification with different languages / locales:
http://ldaley.com/post/1013531080/painless-page-identification-with-geb-grails

ASP.NET MVC4 App fails to compile Bootstrap.LESS on production while it works on dev

I feel a Little stuck right now. First I used nuget to
install-package Bootstrap.less
as well as
install-package dotless
Then, as shown in Rick Andersons Blogpost about bundling and minification in asp.net mvc, I created a LessTransform-Class. I set up 2 nearly empty .less files and created a new bundle packaging them...
var lessBundle = new Bundle("~/MyLess").IncludeDirectory("~/Content/MyLess", "*.less", true);
lessBundle.Transforms.Add(new LessTransformer());
lessBundle.Transforms.Add(new CssMinify());
bundles.Add(lessBundle);
That worked well. Then I added a new StyleBundle to the main bootstrap.less file (which basically uses #import to include all the other .less files that bootstrap.less ships)...
bundles.Add(new StyleBundle("~/Bootstrap").Include("~/Content/Bootstrap/less/bootstrap.less"));
and a ScriptBundle to the bootstrap JavaScripts...
bundles.Add(new ScriptBundle("~/bundles/Bootstrap").Include("~/Scripts/bootstrap/js/bootstrap-*"));
to include all shipped bootstrap-*.js files and TADAA everything worked fine. The CSS got compiled including all imported JavaScript files were properly loaded.
But ... all that only worked for development mode with
<compilation debug="true" targetFramework="4.5"/>
As soon as I disable debug to see if the bundling into one file and the minification works properly I encounter the Problem.
The bundling process seems to fail to import all those .less files imported into bootstrap.less
/* Minification failed. Returning unminified contents.
(11,1): run-time error CSS1019: Unexpected token, found '/'
(11,2): run-time error CSS1019: Unexpected token, found '/'
(12,1): run-time error CSS1031: Expected selector, found '#import'
(12,1): run-time error CSS1025: Expected comma or open brace, found '#import'
(12,27): run-time error CSS1019: Unexpected token, found '/'
(12,28): run-time error CSS1019: Unexpected token, found '/'
... here go many many lines like these
(60,25): run-time error CSS1019: Unexpected token, found ';'
(62,1): run-time error CSS1019: Unexpected token, found '/'
(62,2): run-time error CSS1019: Unexpected token, found '/'
(63,1): run-time error CSS1031: Expected selector, found '#import'
(63,1): run-time error CSS1025: Expected comma or open brace, found '#import'
(63,27): run-time error CSS1019: Unexpected token, found '/'
(63,28): run-time error CSS1019: Unexpected token, found '/'
: run-time error CSS1067: Unexpected end of file encountered
*/
/*!
* Bootstrap v2.3.1
*
* Copyright 2012 Twitter, Inc
* Licensed under the Apache License v2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Designed and built with all the love in the world #twitter by #mdo and #fat.
*/
// Core variables and mixins
#import "variables.less"; // Modify this for custom colors, font-sizes, etc
#import "mixins.less";
... and the rest of the original bootstrap.less... no style definitions
having a look at the minified bootstrap.javascript bundle also boggles me. in dev there was no Problem after loading the page, now after the bootstrap.javascript was bundled and minified in Google the JavaScript console states
Uncaught TypeError: Cannot read property 'Constructor' of undefined
I have had a look at several Topics that seemed closely related to my Problem, and I tried a few things, but so far without success.
Many thanks in advance to anyone who could shed some light into my Situation and who would point out what I am missing or doing wrong. Best regards, Ingo
If you want to use bootstrap as less-files and in addition want to stop worrying about bundling and minification on your development machine as well as on your production machine, you might consider using the following approach.
Note: you don't need all this if you only play around with Less-Files while DEBUGging is enabled; But as soon as you want your application to go live on a production server like Windows Azure, and still want to just modify your less files without having to take care about the bundling and minification procedures... well... then this approach will work
So in order to solve the problem I felt a little stuck in, I had to approach the problem differently and had to modify (see Modification 2 further down the post) the "BundleSource" I thought I'd like to have.
SO DONT FORGET TO READ THE 2nd Modification/Warning close to the bottom of this answer!
MODIFICATION 1)
So the first and bigger part of the job is to get the bundling of the bootstrap-less files working. In order to do that I took the liberty to fork a piece of code I found in the web that (if you only need one less-file bundle) itself solves my problem... unless you might want to use or be able to use multiple less-bundles with several base directories... So that is where I actually found the approach that helped me a lot ...
... wherefore I award many thanks to Kristof Claes for his Blog-Entry "Using ASP.NET bundling and minification with LESS files" which I accidently and gladly stumbled over.
Like me he tried to use the LessMinify.cs that Scott Hanselman was showing in his speeches to work with 1 LESS-file instead of just bundling every single file in 1 directory full of LESS-files.
But he had to extend the whole bundling procedure slightly as he shows in his Blog-Entry. That way the solution he proposes can bundle 1 less file that uses imports to load other less files. But as he statically implements the path that is added to the source directory in which to find the less files... whichever less bundle you define has to pick a less file in the same directory...
That is where I took the liberty to extend his solution a bit further. I created a file LessBundling.cs with the following content:
using dotless.Core.configuration;
using dotless.Core.Input;
using MvcApplication2.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Web;
using System.Web.Hosting;
using System.Web.Optimization;
namespace MvcApplication2.Extensions
{
// create Less-Minifier (use Type to define source directory of less files [see below at BootstrapFileReader])
public class LessMinify<TFileReader> : CssMinify
where TFileReader : IFileReader
{
public LessMinify() {}
public override void Process(BundleContext context, BundleResponse response)
{
var config = new DotlessConfiguration()
{
MinifyOutput = true,
ImportAllFilesAsLess = true,
CacheEnabled = false,
LessSource = typeof(TFileReader)
};
response.Content = dotless.Core.Less.Parse(response.Content, config);
base.Process(context, response);
}
}
// create a LessStyleBundler to allow initializing LessBundle with a single less file that uses imports
public class LessStyleBundle<TFileReader> : Bundle
where TFileReader : IFileReader
{
public LessStyleBundle(string virtualPath)
: base(virtualPath, new LessMinify<TFileReader>()) {}
public LessStyleBundle(string virtualPath, string cdnPath)
: base(virtualPath, cdnPath, new LessMinify<TFileReader>()) { }
}
// create abstract VirtualFileReader from dotless-IFileReader as a Base for localized
internal abstract class VirtualFileReader : IFileReader
{
public byte[] GetBinaryFileContents(string fileName)
{
fileName = GetFullPath(fileName);
return File.ReadAllBytes(fileName);
}
public string GetFileContents(string fileName)
{
fileName = GetFullPath(fileName);
return File.ReadAllText(fileName);
}
public bool DoesFileExist(string fileName)
{
fileName = GetFullPath(fileName);
return File.Exists(fileName);
}
public string GetFullPath(string path)
{
return HostingEnvironment.MapPath(SourceDirectory + path);
}
public abstract string SourceDirectory {get;}
// implement to return Path to location of less files
// e. g. return "~/Content/bootstrap/less/";
}
// create BootstrapFileReader overwriting the Path where to find the Bootstrap-Less-Files
internal sealed class BootstrapFileReader : VirtualFileReader
{
public override string SourceDirectory
{
get { return "~/Content/bootstrap/less/"; }
}
}
}
So what does this actually do?
LessMinify extends the CssMinify class and therefore brings everything needed to minify css files
The important difference to "usual" bundling is that you create a new Dotless-Configuration with the LessSource defined as typeof(TFileReader) ...
By using <TFileReader> you can define a class that will contain the source directory in which the bundler/minifier will look for the less files to be taken into account
LessStyleBundle extends Bundle and therefore brings everything needed to bundle the files
In this class I again use TFileReader as this is where the LessMinify(er) will be instantiated
VirtualFileReader implements IFileReader which is a dotless interface defining all methods required to parse less files and give information where to find files to be imported
In order to extend Kristof's solution to the problem I added the abstract property SourceDirectory... requiring me to also make the VirtualFileReader abstract class
Now with that setup you can create as many LessFileReaders as you want. You just have to extend the abstract VirtualFileReader as can be seen in
BootstrapFileReader extends VirtualFileReader
The only purpose of the BootstrapFileReader is to have a property-getter for the SourceDirectory in which the bundler/minifier will find the less files that are to be imported
Well in my case Bootstraps Less-Files where lying in ~/Content/bootstrap/less which should be the default location if you install the "twitter.bootstrap.less"-nugget.
If you'd have another directory in your application, which contained a less file which again has multiple imports you just create a new class extending VirtualFileReader and define the property-getter for the SourceDirectory to return the corresponding path
If you then want to use this Bundling method to actually bundle and minify less files in a production environment you just add the LessStyleBundle-instantion to the BundlerConfig.cs:
bundles.Add(new LessStyleBundle<BootstrapFileReader>("~/bundles/BootstrapCSS")
.Include("~/Content/bootstrap/less/bootstrap.less"));
and of course your _Layout.cshtml should also be aware of the readily prepared bundle
#Styles.Render("~/bundles/BootstrapCSS")
MODIFICATION 2)
now the minor Modification which I also had to add to get this working
In my first attempt to bundle bootstrap.less I used this
bundles.Add(new LessStyleBundle<BootstrapFileReader>("~/Content/BootstrapCSS")
.Include("~/Content/bootstrap/less/bootstrap.less"));
I thought I would use Content in the routes for CSS/Less and Bundles in the routes for Javascript.
But that does not work out of the box. ASP.net doesnt permit the creation of a Bundle that starts with ~/Content. You will get a 403 authorization failure. Therefore the easiest solution to that is to use ~/bundles instead:
bundles.Add(new LessStyleBundle<BootstrapFileReader>("~/bundles/BootstrapCSS")
.Include("~/Content/bootstrap/less/bootstrap.less"));
As there aren't many real solutions to this problem I hope this will help at least some of you if you plan to integrate twitter bootstrap into your asp.net mvc4 application.
best regards,
Ingo
I've modified Ingo workaround to get rid of custom classes for each directory.
Also, I've added proper exception output (because otherwise all exceptions was silent and you just got empty less file in case of error).
public class LessTransform : IItemTransform
{
[ThreadStatic]
internal static string CurrentParsedFileDirectory;
public string Process (string includedVirtualPath, string input)
{
CurrentParsedFileDirectory = Path.GetDirectoryName (includedVirtualPath);
var config = new DotlessConfiguration
{
MinifyOutput = false,
CacheEnabled = false,
MapPathsToWeb = true,
ImportAllFilesAsLess = true,
LessSource = typeof (VirtualFileReader),
Logger = typeof (ThrowExceptionLogger)
};
return Less.Parse (input, config);
}
}
internal class VirtualFileReader : IFileReader
{
public bool UseCacheDependencies
{
get { return false; }
}
public byte[] GetBinaryFileContents (string fileName)
{
return File.ReadAllBytes (GetFullPath (fileName));
}
public string GetFileContents (string fileName)
{
return File.ReadAllText (GetFullPath (fileName));
}
public bool DoesFileExist (string fileName)
{
return File.Exists (GetFullPath (fileName));
}
public string GetFullPath (string path)
{
if (string.IsNullOrEmpty (path))
return string.Empty;
return HostingEnvironment.MapPath (path[0] != '~' && path[0] != '/'
? Path.Combine (LessTransform.CurrentParsedFileDirectory, path)
: path);
}
}
public class ThrowExceptionLogger : Logger
{
public ThrowExceptionLogger (LogLevel level) : base (level)
{
}
protected override void Log (string message)
{
if (string.IsNullOrEmpty (message))
return;
if (message.Length > 100)
message = message.Substring (0, 100) + "...";
throw new LessTransformException (message);
}
}
[Serializable]
public sealed class LessTransformException : Exception
{
public LessTransformException (string message) : base (message)
{
}
}
Usage:
bundles.Add (new StyleBundle ("~/styles-bundle/common")
.Include ("~/content/bootstrap/bootstrap.less", new LessTransform ()));
I was having the same issue today, I found a work around but I'd like a better solution as well. I was also trying to use dotless and a custom transform like what you have.
Workaround:
Pre-build event:
"$(SolutionDir)packages\dotless.1.3.1.0\tool\dotless.compiler.exe" "$(ProjectDir)Content\less\bootstrap.less"
That will create a bootstrap.css file which you can then include as regular CSS instead of LESS.
This solution isn't ideal, as you'd have to update the build event each time you update dotless, and having the bundle handle it is cleaner as well.
I really, really recommend installing WebEssentials 2012 instead.
It will generate a css-file AND a minified css-file from your .less and you can reference the css instead. It will automatically update the css everytime you make a change to your .less so there is no need to remember any pre-build steps or anything...
When installing WebEssentials you'll also get other sweet features like preview of CoffeeScript, TypeScript and LESS. JSHint, automatic minification and lots and lots more "goodies"!