With the entire environment set, we can finally implement the ASAP Server.
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() #else return productsAsNSString as String #endif } catch { print("Error") } return "{ products: []...