O'Reilly logo

Swift 2 By Example by Giordano Scalzo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The ASAPServer

With the entire environment set, we can finally implement the ASAP Server.

The Products

In a real server, the products are probably saved in a database, but in our simple implementation, we will save the products in a file—the same file we used in the app.

So, let's start the implementation by saving the products.json file at the same level of makefile.

A DBRepository class will abstract the connection with the storage, making it easy to switch to a database, as we did in the app:

import Foundation let dbRepository = DBRepository() class DBRepository { func allProducts() -> String { print("Get /products") do { let productsAsNSString = try NSString(contentsOfFile: "products.json", encoding: NSUTF8StringEncoding) #if os(Linux) return productsAsNSString.bridge() ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required