Showing posts with label play. Show all posts
Showing posts with label play. Show all posts

Wednesday, 30 January 2013

Heroku commands & tips


I wrote a quick howto of Heroku commands and tips I use all the time.

It covers creating instances, configurations, common add-ons such as databases & email, deployment strategies and more.

As mentioned in the document I tend to have multiple applications per project, so I always append --remote staging for example, resulting in this long deploy command:

git push staging master && \
heroku logs -t --remote staging; \
heroku open --remote staging && \
heroku logs -t --remote staging

For the eagle eyed you will have noticed the ";" which means as the log tailing never really finishes, when you notice the app is up and running you have to manually end it with control+c to proceed with opening up a browser.

Obviously these commands ties nicely into my use of Play on Heroku and the howtos I wrote on integrating both.

They are perhaps obvious commands and basic but I hope they are of use to some people.


Wednesday, 23 May 2012

Send email via SendGrid on Heroku using Play! 2.0 with Scala

If you have a Play! framework 2.0 application that you want to send email from, here are a few tips.

These tips assumes you deploy to Heroku, but other platforms should work similarly. The examples here are using Scala, but Java should work along similar lines. Finally the specifics are for the SendGrid add-on for Heroku, but other mail server providers should be fine.


First add the free option of the SendGrid add-on to your Heroku app by typing in:
heroku addons:add sendgrid:starter

Then configure your Play! app to use the mail plugin provided by Typesafe:

Add to your dependencies in the project/Build.scala file: (all on one line)
"com.typesafe" %% "play-plugins-mailer" % "2.0.2"
Then create and add these to a conf/play.plugins file: (all on one line)
1500:com.typesafe.plugin.CommonsMailerPlugin

Next configure the mail server settings. You can either add these directly to your conf/application.conf file, but I prefer to share my projects' source code, so my production settings are set via environment variables so that my username/password are not publicly available.

However for the plugin to run smpt.host  must be present. Open conf/application.conf and add:
smtp.host=mock

On Heroku I append the settings to the Heroku's propriatory Procfile file. I append these settings to the Procfile to use SendGrid's servers: (all on one line)
-Dsmtp.host=smtp.sendgrid.net -Dsmtp.port=587 -Dsmtp.ssl=yes -Dsmtp.user=$SENDGRID_USERNAME -Dsmtp.password=$SENDGRID_PASSWORD
You may already have other settings in the Procfile, e.g. database URL, so be aware of the 255 char limit, and use a custom properties file instead.
web: target/start -Dhttp.port=${PORT} 
-Dconfig.resource=heroku-prod.conf

The SendGrid add-on should create the environment SENDGRID_USERNAME and SENDGRID_PASSWORD variables for you.
You can verify this with:
heroku config

Finally we then create our actual application code to send email:
package notifiers
import com.typesafe.plugin._
import play.api.Play.current
import play.api.Play
import play.Logger
object EmailNotifier {
  def sendMail {
    val mail = use[MailerPlugin].email
    mail.setSubject("Mail test")
    mail.addRecipient("Joe Smith  <joe@example.com>","sue@example.com")
    mail.addFrom("Jon Doe <joe@example.com>")
    mail.send( "Test email" )
  }
  def sendTestMail {
    if(Play.current.mode == Mode.Prod){
      Play.current.configuration.getString("smtp.host") match {
        case None => Logger.debug("Email mock")
        case Some("mock") => 
Logger.debug("Email mock")   
        case _ => sendMail(participant)

    }
  } else {
    Logger.debug("Email mock")}
  }
}
(In case of this posts cant parse tags replace &lt;  and &gt; with less than and greater than tags;)




This should be all that is needed.


Play! 1.x did have a handy mock email interface for development and testing. I will try and find a suitable replacement for 2.0 and update this post when I do.




For more information





Sunday, 22 April 2012

Play! 1 & 2 command tip

If you love the Play! Framework, you might be like me and have both version 1.x and 2.x installed.

Version 1.x, in my case 1.2.4, is a well established feature rich stable version. 
Version 2.x, in my case 2.0, is a new radically different version, that is still in its infancy but released. 


You might have both installed as you have older projects using 1.x and new in development projects using 2.x, or similar.




With both version the norm is to install them and expose the main executable as "play". So how do you differentiate between which version to use for which project?


OK, it is no rocket science but here is a quick tip on how I do it:



I have Play! 1.x installed in
/usr/local/lib/play-1.2.4


I have Play! 2.x installed in /usr/local/lib/play-2.0


You can add either play folders to the your PATH, e.g. in /etc/environment: 


PATH="blahblabh:/usr/local/lib/play-2.0"


But I simple add them to my .bash_aliases file:


alias play1="/usr/local/lib/play1/play"
alias play2="/usr/local/lib/play2/play"


On top of that I symlink this:


$: cd /usr/local/lib;
$: sudo ln -s play-1.2.4 play1;
$: sudo ln -s play-2.0 play2



With this setup I have to make a conscious decision whether to run Play! 1 or 2, and can switch between the two very easily.

$: play1 help;
$: play2 help




Wednesday, 11 January 2012

Play! 2.0 in IntelliJ IDEA

Play! Framework 1.x supported creating an IntelliJ IDEA project by the command: play idealize.

However while Play! Framework 2.0 is in beta that command does not work**.

So how do you get your Play! 2.0 project to open in IntelliJ IDEA? There are few different work arounds. Especially regarding integrating sbt.

However I have a quick way. For this to work you need both Play! 2.0 and Play! 1.2.x installed.

Create Play! 2.0 project:
/usr/local/lib/play-2.0-beta/play new helloworld
(I am assuming it was a Java project that you chose)

Rename project folder:
mv helloworld helloworld2

Create Play! 1.x project:
/usr/local/lib/play-1.2.4/play new helloworld

Create IntelliJ project:
cd helloworld;
/usr/local/lib/play-1.2.4/play idealize


Move IntelliJ files to Play! 2.0 project:
cd ..;
mv helloworld/helloworld.i* helloworld2/


Remove 1.x project and rename 2.0 folder:
rm -rf helloworld;
mv helloworld2 helloworld


Now you can open IntelliJ and then go to file/open project, then find and open helloworld/helloworld.ipr.


There will be some issues such as libraries etc but this a good start. For further tips try these suggestions.


** As of 11th of January 2012 it is not present in Play! 2.0. I fully expect Play! to create an idealize, eclipsify, netbeansify etc as soon as 2.0 is stable.