Showing posts with label Play Framework. Show all posts
Showing posts with label Play Framework. Show all posts

Wednesday, March 08, 2017

View.html.index cannot be resolved to a type

If your play project in eclipse says that it can't resolve a type (see message below), but you are certain that everything should be fine
$ View.html.index cannot be resolved to a type
try to make a clean compile
$ activator clean compile eclipse
It helped me to resolve my issue.

Thursday, January 12, 2017

Kill Play Framework process

When we run application in DEV mode (using command activator run) the process normally will be killed when terminal is closed. It's pretty easy since PID is started/closed automatically and therefore we do not care about it at all.
Once we start application in PROD mode there is a file RUNNING_PID is created (./target/universal/stage/RUNNING_PID). There is a command (since version 2.4) in activator (stopProd) which will close PID
$ activator stopProd
Alternatively you can just kill process 'manually'
$ kill $(cat target/universal/stage/RUNNING_PID)

Play Framework project in production - Building process

We want to deploy our Play project to production environment.

Create a binary version of application

There are two commands that can help you to make a build. Simply run dist (it invokes universal:packageBin) or universal:packageZipTarball in Play console and wait
$ dist
$ universal:packageZipTarball
Result will be a ZIP or TGZ file located in target/universal folder with everything needed for you project (it means you do not need to install SBT or Activator on your server, just pure Java). Once you extract ZIP you will find 2 runner files in bin folder (one for unix and one for windows). Just run it and your server will up
$ path/to/hellow-world/bin/hello-world-app 
[info] play.api.Play - Application started (Prod)
[info] p.c.s.NettyServer - Listening for HTTP on /0:0:0:0:0:0:0:0:9000
Make sure you have rights to run server, sometimes you need to give rights (see example below)
$ chmod +x /path/to/bin/project-name
To run process in background
$ sudo nohup target/universal/stage/bin/eqa-app -Dhttp.port=80 -Dplay.crypto.secret="secret_token_123" /dev/null 2>&1 &

Play Framework project in production - Application Secret

I am going to make series of articles about how to deploy Play Framework (version 2.5) application on centOS together with build system Jenkins. I'm doing this first time and want to document everything for myself and at the same time I hope it can be useful for somebody else as well.

Before I wrote few articles how to setup hello-world project on centOS or macOS however now I'm going to work on production setup. I assume you already have you hello-world project and clean centOS environment.

Let's have a look on important moment.

Application secret


Each play application has secret key which is used for signing session and some other important stuff. It is not possible to run play project in production mode in case if secret is not set or if it is set to default value 'changeme'. Secret key is stored in application.conf file /path/to/hello-word/conf/application.conf in variable play.crypto.secret (see below).
## Secret key
# http://www.playframework.com/documentation/latest/ApplicationSecret
# ~~~~~
# The secret key is used to sign Play's session cookie.
# This must be changed for production, but we don't recommend you change it in this file.
play.crypto.secret = "changeme"
Of course we should not share our secret key and therefore it has to be used/stored on production side only.

There are at least 3 ways how we can use secret key on production side.

1. Secret key as a parameter

It is fine if you have simple application on 1 server, but I would not really recommend that for bigger project.
/path/to/hello-world -Dplay.crypto.secret="secret_token_123"

2. Environment variables

That would read variable from OS environment, otherwise default value will be used (actually the last defined one, in example below it is "chagneme").
play.crypto.secret="changeme"
play.crypto.secret=${?APPLICATION_SECRET}

3. Use separate configuration file

Separate configuration is probably the best way to go.
include "application"
play.crypto.secret="QCY?tAnfk?aZ?iwrNwnxIlR6CTf:G3gf:90Latabg@5241AB`R5W:1uDFN];Ik@n"
They include config while running application.
/path/to/hello-word/bin/yourapp -Dconfig.file=/path/to/production.conf

Secret tools

There are few already builtin function that can help you deal with secrets: playGenerateSecret (generate secret) and playUpdateSecret (generate and update into config).
$ playGenerateSecret
[info] Generated new secret: G28Dze]Z4lr@Or_9DCoz;tT_yCj6opKkkIh27K>[0l_NT9lZaFfs?=zx[Wulz>cX
[success] Total time: 0 s, completed Jan 11, 2017 6:24:12 PM
$ playUpdateSecret
[info] Generated new secret: QmJ?udauJgDj34AYifbprJvbT5I8^Vw1MY0WmbYRscZmAOotkalbhXbIs^48_Uc9
[info] Updating application secret in /Users/dpa/git/eqa-app/conf/application.conf
[info] Replacing old application secret: changeme
[success] Total time: 0 s, completed Jan 11, 2017 9:22:06 PM

Sunday, December 25, 2016

HTTP Routing in Play Framework

We are going to check how Play connect http requests with our code. F.x. when user hits http://localhost:9000/ what happens?

HTTP Routing

There is already built in http router in Play Framework. It allows to connect incoming requests with Play Action and therefore with public method in a controller class.

Configuring HTTP Routing

Normally the configuration for HTTP routing is located in conf/routes. See example:

# Static path
GET   /clients/all                  controllers.Clients.list()
# Dynamic path
GET   /clients/:id                  controllers.Clients.show(id: Long)
GET   /files/*name               controllers.Application.download(name)
# Dynamic parts with regexp
GET   /items/$id<[0-9]+>    controllers.Items.show(id: Long)

If there are few routes are matched for the same request then the first one in a configuration file will be used.

Thursday, December 22, 2016

Actions in Play Framework 2.5

Requests that come to application based on Play usually are processed by thing which is called action.

Action it is just a method that processes parameters of requests and sends result back to web client

Let's look on example below

package controllers;

import play.mvc.*;

/**
 * This controller contains an action to handle HTTP requests
 * to the application's home page.
 */
public class HomeController extends Controller {

    /**
     * An action that renders an HTML page with a welcome message.
     * The configuration in the routes file means that
     * this method will be called when the application receives a
     * GET request with a path of /.
     */
    public Result index() {
        return ok("Hello World!");
    }

}

We see an action index that return Result (HTTP response which we send to web client).

Wednesday, December 21, 2016

Hello World on Play Framework 2.5 on OSX

Today we are going to make Hello World project based on Play Framework 2.5 on OS X

I'm getting back to Play Framework again and I'm going to build simple start project.

1. Checking if Java is installed

Make sure you have java installed.

java -version

If java is installed you will see message like that:

java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

2. Installing Typesafe activator (current version is 1.3.12)

brew install typesafe-activator

3. Let's create a new project

activator new hello-world

Activator will ask you what template you want to use for new project? (I used play-java).

  1) minimal-akka-java-seed
  2) minimal-akka-scala-seed
  3) minimal-java
  4) minimal-scala
  5) play-java
  6) play-scala

That will create a java project for us

Now let's run it. Go into newly create project and run activator

activator run

That will run our project and up server as well. You can access project by localhost:9000

Wednesday, October 14, 2015

Setup Play Framework and TypeSafe on centOS

I'm going to setup simple project based on Play Framework together with Cassandra on two centOS servers.

I'm going to do 3 steps during that process:
  1. Install Java
  2. Install Typesafe activator
  3. Create test project.

Installing Java

//1. go to opt folder
cd /opt

//2. download java
wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u60-b27/jdk-8u60-linux-x64.tar.gz"

//3. extract java archive
tar xzf jdk-8u60-linux-x64.tar.gz

//4. installing java
cd /opt/jdk1.8.0_60/
alternatives --install /usr/bin/java java /opt/jdk1.8.0_60/bin/java 2
alternatives --config java

//5. recommended options: setup javac and jar
alternatives --install /usr/bin/jar jar /opt/jdk1.8.0_60/bin/jar 2
alternatives --install /usr/bin/javac javac /opt/jdk1.8.0_60/bin/javac 2
alternatives --set jar /opt/jdk1.8.0_60/bin/jar
alternatives --set javac /opt/jdk1.8.0_60/bin/javac

//6. setup variables so we can access java from everywhere
export JAVA_HOME=/opt/jdk1.8.0_60
export JRE_HOME=/opt/jdk1.8.0_60/jre
export PATH=$PATH:/opt/jdk1.8.0_60/bin:/opt/jdk1.8.0_60/jre/bin

//7. checking java version
java -version

//8. delete java archive
rm jdk-8u60-linux-x64.tar.gz

Installing Play Framework / TypeSafe


Example is about current version, which is 1.3.6
//1. go to tmp folder and download typesafe activator
cd /tmp
wget https://downloads.typesafe.com/typesafe-activator/1.3.6/typesafe-activator-1.3.6.zip

//2. unzip and move to opt folder
unzip typesafe-activator-1.3.6.zip
mv activator-dist-1.3.6 /opt

//3. delete zip file, we do not need it anymore
rm typesafe-activator-1.3.6.zip

//4. create soft symbolic link from /opt/activator-dist-1.3.6/activator to /usr/local/sbin/activator
ln -s /opt/activator-dist-1.3.6/activator /usr/local/sbin/activator

//5. set variables so we can use activator from everywhere
export PATH=/usr/local/sbin/activator:$PATH

Create helloworld project


Time to create our first project. Play Framework provides many templates, we are going to use play-java (it has already web interface and some code, so we can discover it).
//1. create folder www in /var (we are going to keep projects there).
cd /var
mkdir www
cd www

//2. have a look on current templates
activator list-templates

//3. create a new project based on play-java template
activator new helloworld play-java

//4. go into newly created project and run it
cd helloworld
activator run

If activator run properly, you should see something like this:


Now you are ready to open project in browser. Type 127.0.0.1:900 and boooom, we have it ready!


I'm going to play with centOS little bit more (before I start to look on code). I already have some minor issues I want to optimise.