Coder Social home page Coder Social logo

spray-example's Introduction

Spray Example

I want this to be an example of a Spray API that goes a bit beyond just the routing layer. In particular, it shows how to wire together an API that uses a separate service and model actor. It also shows off a few things I consider good practice:

  • "Intelligent" cache control. Tailor the upstream cache time per resource.

  • Separate on-the-wire protocol. I often see code bases where the domain objects contain more annotations than code---often for both JSON and ORM mappings. I think this is bad practice and prefer to use different objects.

  • Use sbt-revolver. This is great plugin by the Spray guys to simplify and speed up the dev/build/test cycle.

  • Stub out child actors for testing using the Cake Pattern. See the TopLevelConfig trait and its corresponding ProductionTopLevelConfig implementation for more details.

Running the example service

To start the example service, launch a terminal and cd into the directory and run sbt:

$ sbt

Once sbt starts the prompt will change. You can start the example service in the background using the re-start command, provided by the sbt-revolver plugin:

> re-start

In a different terminal (or a browser), call the service:

$ curl localhost:8080/items

You should see:

[{
  "id": 1,
  "stockLevel": "LowStock",
  "title": "foo"
}, {
  "id": 2,
  "stockLevel": "LowStock",
  "title": "bar"
}, {
  "id": 3,
  "stockLevel": "InStock",
  "title": "qux"
}, {
  "id": 4,
  "stockLevel": "InStock",
  "title": "quux"
}, {
  "id": 5,
  "stockLevel": "InStock",
  "title": "quuux"
}]

You can also try getting only the items where the description contains 'Qu':

$ curl 'localhost:8080/items?q=Qu'

The quotes are necessary because ? would otherwise be interpreted by the shell. Anyway, you should see this:

[{
  "id": 3,
  "stockLevel": "InStock",
  "title": "qux"
}, {
  "id": 4,
  "stockLevel": "InStock",
  "title": "quux"
}, {
  "id": 5,
  "stockLevel": "InStock",
  "title": "quuux"
}]

Now try getting a single item:

$ curl localhost:8080/items/2

You should see:

{
  "id": 2,
  "stockLevel": "LowStock",
  "title": "bar",
  "desc": "More information about Bar"
}

Finally, try getting an item that doesn't exist:

$ curl localhost:8080/items/23

You should get a "Not Found" message, and the status code 404.

spray-example's People

Contributors

stig avatar

Watchers

James Cloos avatar Mathieu Simoph avatar

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.