Code Monkey home page Code Monkey logo

url-shortener's Introduction

#URL Shortener Tags: Scala, Play Framework, Slick, play-slick, PostgreSQL, Backbone, jQuery, Knockout url-shortener ##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:

Requirement

  • Install Java
  • Download Activator
  • Make sure you have added activator into your PATH

Database connection

  • 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

Start server

  • 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 db-evolution
  • 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:

  1. Use activator new _<project-name>_ play-scala to create a skeleton project in current directory
  2. If you want to use activator gen-idea command to import project into Intellij, you need to add addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.6.0") into plugins.sbt inside project folder
  3. Add endpoints in routes file
  4. 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.
  5. play-slick setup and PostgreSQL connection:
  • In build.sbt remove jdbc from libraryDependencies (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 :)

url-shortener's People

Contributors

tanghaoji avatar

Watchers

 avatar

Forkers

newtry001

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.