Play framework 2.0. Use alternative application.conf in test - testing

my Play using mysql in production. But I am trying to use memory for testing.
I created 2 conf file, 1 is application.conf, the other is application.test.conf (in the same directory).
I tried to do
play -Dconfig.file=conf/application.test.conf test-only
But it still use the default conf file.
I'm just wonder if anyone know how to use a different conf file during testing. (or at least use a different database setting during testing).

If you mean for unit tests then just add
running(FakeApplication(additionalConfiguration = inMemoryDatabase())) { Test code... }
to your tests and they will be done in memory. No need to change conf files.

Related

Is there any way to hot-reload static files in Ktor?

I've been following the ktor tutorial for making a website and notice that every time I make a change to a resource file, I have to recompile to see it updated in the browser. Is there any way to hot reload static files to speed up development? I'm using IntelliJ if it matters.
I believe Spring Boot has hot-swapping functionality explicitly for this purpose, I was just wondering if something equivalent for Ktor exists.
Ktor should be able to pick any new static content you provide. Since you are saying that recompilation is needed, I would propose to double-check if you are modifying the correct files (resulting artifact vs. the sources). If you are using the default template, you should be changing /build/resources/main/static in order for changes to take effect.

export and maintain vue application

I have developed a vue application and did run npm run build
After that I uploaded the content in the dist file to my webpage but it returned a blank page.
Since I did this for testing I uploaded it to a folder in my public_html/mypage.com/vueapplication To get all the paths right I added a vue.config.js with this content:
// vue.config.js
module.exports = {
publicPath: '/vueapplication/'
}
The application now works but I wounder however:
how do I best publish/upload the application to my site? Just by simply dragging the content inte the right folder?
how can I best maintain my site? Do I need to build again and upload, overwriting my files when everytime I make an update on my site?
And what is the difference between build and deploy your application?
Drag and dropping your code should work. But as your app grows you may want to look into automating this. For instance if you use an S3 bucket you can use the aws cli to automate the upload.
Yes, you should overwrite your deploy folder(s). You need to also take care of deploying different binary files, that have the same name. An example is if you have a global css file (main.css for instance). The file will probably change content between deployments, but keep the same name. Browsers may cache the file so users that downloaded older versions of the file will not use the new one. There are different techniques to handle this, but if you use webpack, it uses cache busting techniques and you should be fine.
Build is the process of transforming source code into an artifact(s). Exactly what this means differs from language to language, platform to platform. In the vuejs world this usually means a couple of js files, a couple of css files and some assets.
Deploying means taking the output of a build and making it available to your users. Again this differs from project to project. In the vuejs world this usually means taking the artifacts from the build and uploading them to an http enabled web server.

What's the easiest way to have "settings profiles" in Scrapy?

Scrapy picks up settings from settings.py (there are default settings, project settings, per-spider settings as well). What I'm looking for is being able to have more than one file with settings and being able to switch between them as I launch my spiders quickly. If there is some inheritance between files that would be awesome too.
If you know Spring Boot from Java world there is an idea of profile. You have application.settings file with your base settings. And then you can have application-dev.settings and application-prod.settings. If you run your application with option -Dspring.profiles.active=dev then it will pick up application.settings and add application-dev.settings on top of it. This way you can maintain multiple configurations in parallel and rapidly switch between them.
I've found an approach for Scrapy with no supporting code required. The approach is to use SCRAPY_SETTINGS_MODULE and import base settings file in my dev and prod modules. Are there any other approaches that you use?
Launch line in my case would look like:
export SCRAPY_SETTINGS_MODULE=projectname.profiles.dev && scrapy crawl myspider
Firstly, if you're only going to change one or two values, then it would be simpler to use a single dynamic settings.py (as mentioned in Gallaecio's answer).
However, if you really need separate settings, there is an even shorter way by defining separate "projects" in scrapy.cfg (docs):
[settings]
default = myproject.settings.dev
dev = myproject.settings.dev
prod = myproject.settings.prod
Then to run a specific one:
SCRAPY_PROJECT=prod scrapy crawl myspider
SCRAPY_PROJECT=dev scrapy crawl myspider
If you don't specify SCRAPY_PROJECT it will use default.
And yes, you can inherit from settings files. Replace your settings.py file with a module instead:
myproject/settings/__init__.py
myproject/settings/base.py
myproject/settings/dev.py
myproject/settings/prod.py
In base.py you can have exactly what you have in settings.py. Then at the top of each override file you add:
from .base import *
# Override settings in the same way as if they were declared in settings.py
That wildcard import is usually a bad practice, but in this case since it's just a plain Python file so the end result is just having all the variables available. This is a trick we often use in Django (example).
I believe SCRAPY_SETTINGS_MODULE is the best approach.
Alternatively, since a settings module is a Python script, you could change settings dynamically from within settings.py. I’ve seen this done, for example, to detect automatically whether a spider is running in a local machine or on a Scrapyd server, and adjust the settings accordingly at run time.

Is it possible to use Dojo build without modifying JS files?

Is it possible to use Dojo build without the need to modify JavaScript files?
The article dgrid and Dojo Nano Build provides the instruction to create the build, but it requires adding the following line into JavaScript file, which initializes the application:
require(['dgrid/dgrid'], function () {
(replacing 'dgrid/dgrid' with your build module name).
However, it is very problematic when using build for own modules, because, of course, in development mode the require with own layer can't be included, otherwise the modifications made to own modules wouldn't be visible. But in production mode this line must be added.
So either you must modify the file manually before production build, or write a script that would modify the file during the build. Both are very error-prone.
Is there a better way to achieve that result? Is it possible for Dojo to recognize that the build is provided and should be used, instead of loading each module separately?
The following line of code can be included in both development and production modes.
require(['dgrid/dgrid'], function () {
I describe the reasons why in my answer here.
What you need to do is configure Dojo differently based on what environment.
In a blog post that I wrote, I describe this in more detail. The following summarizes the post:
I create three modes: Production, Uncompressed, and Development.
Development
When developing code, I will have the js source linked into the web server and the Development mode will point to the dojo.js file and the raw css file(s). The browser will load modules that I need using xhr. And I point to the top level css files which import other css files. The result is that a lot of requests will be made to the server and the loading of the page will be noticeably slow. The benefit is that you can see development changes without having to do a full build.
Production
Production mode points the main dojo file at the dojo.js that is built using the build tool. I also create <script> elements for the other layers that are needed in the page. I point the css to the built css files which the build tool has inlined the imported css. The page loads quickly, but it is difficult to debug
Uncompressed
Similar to production, but I point to the .uncompressed.js files. Production and Uncompressed are available in the released version of our software. I use uncompressed when trying to troubleshoot an issue in a production environment. The value of this mode is dwindling as the developer tools are better supporting compressed javascript (ie source maps, etc.)
Server side
The default mode is Production, but I use a query parameter to switch modes. I also store the current mode in the session, so that I only have to set the mode once to change it. Subsequent pages will run in the changed mode, until I change it back.
Here is a java implementation of this code.

Run a .go file under Apache from source by 'compiling on the fly'

I'm able to run a Go application as a website with Apache using the following code.
hello.go:
package main
import (
"os"
)
func main() {
os.Stdout.WriteString("Content-Type: text/html; charset=UTF-8\n\n")
os.Stdout.WriteString("Hello!\n")
}
.htaccess:
AddHandler cgi-script .exe
I compile the app using go build hello.go and going to http://localhost/hello.exe works as expected.
But now I have to recompile after every change I make in the sourcecode.
Is it somehow possible to tell Apache to run hello.go (Apache should run go run hello.go) when visiting http://localhost/hello.go?
By the way, this is only to speed development, not for production!
Go is a compiled language, you'd need to compile it first. There currently aren't any interpreters/VM's for Go.
You're best bet is to just have a process/cron job that checks for the .go file being newer than the binary, and rebuilding it when it notices the file changed.
https://github.com/howeyc/fsnotify is a package that allows you to watch files for changes.
You could use gorun which enables you to put a #!/usr/bin/gorun line at the top of the file so it is run like a script. gorun will compile it on the fly then run it. I've used it quite a bit for making golang scripts and I expect it would work for CGIs too.
You'd have to mark the script as executable (chmod +x) and tell apache that the .go extension was executable.
Not sure I'd recommend this for production but it should work reasonably efficiently as gorun has a cache.
An easy solution would be to use a tool which re-compiles your code on changes to the source files. For example GoWatch.
Or try it yourself by using fsnotify as Erik already stated. Example: Simple Compile Daemon.
You could also invoke go run in your CGI script.