Skip to content

Instantly share code, notes, and snippets.

@siavashalipour
Created November 18, 2016 23:40
Show Gist options
  • Save siavashalipour/32b9b8d02986b343ca8601f1ae61732d to your computer and use it in GitHub Desktop.
Save siavashalipour/32b9b8d02986b343ca8601f1ae61732d to your computer and use it in GitHub Desktop.
End-to-end Swift Solution with IBM Bluemix Part one - 2
//
// Controller.swift
// Microservice1
//
// Created by Siavash on 26/9/16.
//
//
import Foundation
import Kitura
import SwiftyJSON
import LoggerAPI
import CloudFoundryEnv
import KituraNet
import SimpleHttpClient
public class Controller {
 
 let router: Router
 let appEnv: AppEnv
var port: Int {
 get { return appEnv.port }
 }
 
 var url: String {
 get { return appEnv.url }
 }
 
 init() throws {
 appEnv = try CloudFoundryEnv.getAppEnv()
 
 // All web apps need a Router instance to define routes
 router = Router()
 
 // Serve static content from “public”
 router.all(“/”, middleware: StaticFileServer())
 // Basic GET request
 router.get(“/hello”, handler: getHello)
 
 // Basic POST request
 router.post(“/hello”, handler: postHello)
 
 // JSON Get request
 router.get(“/json”, handler: getJSON)
 }
 
 public func getHello(request: RouterRequest, response: RouterResponse, next: @escaping () -> Void) throws {
 Log.debug(“GET — /hello route handler…”)
 response.headers[“Content-Type”] = “text/plain; charset=utf-8”
 try response.status(.OK).send(“Hello from Kitura-Starter-Bluemix!”).end()
 }
 
 public func postHello(request: RouterRequest, response: RouterResponse, next: @escaping () -> Void) throws {
 Log.debug(“POST — /hello route handler…”)
 response.headers[“Content-Type”] = “text/plain; charset=utf-8”
 if let name = try request.readString() {
 try response.status(.OK).send(“Hello \(name), from Kitura-Starter-Bluemix!”).end()
 } else {
 try response.status(.OK).send(“Kitura-Starter-Bluemix received a POST request!”).end()
 }
 }
 
 public func getJSON(request: RouterRequest, response: RouterResponse, next: @escaping () -> Void) throws {
 Log.debug(“GET — /json route handler…”)
 response.headers[“Content-Type”] = “application/json; charset=utf-8”
 var jsonResponse = JSON([:])
 jsonResponse[“framework”].stringValue = “Microservice1”
 jsonResponse[“applicationName”].stringValue = “Microservice1”
 jsonResponse[“company”].stringValue = “Microservice1 Comp”
 jsonResponse[“organization”].stringValue = “Swift @ Swifty”
 jsonResponse[“location”].stringValue = “Sydney, NSW”
 try response.status(.OK).send(json: jsonResponse).end()
 }
 
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment