#URL Shortener
Tags: Scala, Play Framework, Slick, play-slick, PostgreSQL, Backbone, jQuery, Knockout
##Description:
A sample project to demonstrate how to create RESTful API using Play Framework combined with external WebService, Slick3 repository, PostgreSQL database, and front-end framework
##Versions: play 2.5.4, play-slick 2.0.0, scala 2.11.7, PostgreSQL 9.5.3
##Installation:
- Install Java
- Download Activator
- Make sure you have added
activator
into your PATH
- Install PostgreSQL
- Open pgAdim and create a local host port 5432 with username postgres and password postgres
- Under this port, create a new database named url_shortener
- Open Command/Terminal,
cd
to the project root - Enter
activator run
, it will take some time to download the required libraries and resolve dependencies - When you see
(Server started, use Ctrl+D to stop and go back to the console...)
, go to URL http://localhost:9000 - It will compile all the source code, and go to homepage
- You will see this page if you start the server for the first time. Don't be paninc, just click Apply this script now!
##Tests: TODO
##Project setup guide and tips for beginners:
- Use
activator new _<project-name>_ play-scala
to create a skeleton project in current directory - If you want to use
activator gen-idea
command to import project into Intellij, you need to addaddSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.6.0")
into plugins.sbt inside project folder - Add endpoints in routes file
- Handle request and JSON in Controllers, all application logic should go into Services files. The best practice is not to access database persistance at Controller level.
- play-slick setup and PostgreSQL connection:
- In build.sbt remove
jdbc
fromlibraryDependencies
(This is an important step, since you will be using postgres jdbc and you will no longer need the default jdbc) - Add
"com.typesafe.play" %% "play-slick" % "2.0.0",
"com.typesafe.play" %% "play-slick-evolutions" % "2.0.0",
"org.postgresql" % "postgresql" % "9.4.1209.jre7"
to the libraryDependencies
(Note: by adding "play-slick-evolutions" it will enable db evolution)
- In application.conf, add the following settings to configure postgresql driver (This only works for play-slick version 2.0.0 which uses slick3 interface and it is totally different from the previous version aka slick 2.x)
slick.dbs.default {
driver = "slick.driver.PostgresDriver$"
db.driver = "org.postgresql.Driver"
db.url = "jdbc:postgresql://localhost:5432/<your_db_name>"
db.user = "postgres"
db.password = "postgres"
}
Note: localhost port, user and password is configurable as long as they are same as the ones in postgres. Important: You have to manually create a database in postgreSQL under that port with the name <your_db_name>. Otherwise, you will get a SQLTimeout exception when you start your server.
- In your persistance level code, use dependency injection to get
DatabaseConfigProvider
which will be your default setting as above. - If you came from slick2.x, you don't need to provide
implicit session: Session
anymore when you call db - In order to setup table and columns in code, you need to import the following packages:
import play.api.db.slick.{DatabaseConfigProvider, HasDatabaseConfig}
import slick.driver.JdbcProfile
import slick.driver.PostgresDriver.api._
- After you have done the persistance level code, you need to create a SQL evolution script under conf/evolutions/default (create these folders if you don't have) before you start the server. The evolution script should contain twos part, up script and down script. Play will automatically detect the change of the evolution script and ask you to apply the up script when you go to your localhost page.
If you have any questions or if you find any errors in my code or documentation, don't hesitate to fire an issue :)