Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Server-side Swift experience and pitfalls coming from iOS

Kenichi Ueno
September 14, 2018

Server-side Swift experience and pitfalls coming from iOS

I have published a SDK for Swift to help developers to make their Clova skills. This presentation shows some problems I have faced through the development of the SDK and a sample project for it and solutions for them.

Kenichi Ueno

September 14, 2018
Tweet

Other Decks in Programming

Transcript

  1. Agenda • My background and my topic of discussion •

    What I made with Server-side Swift • What I have experienced
  2. Self Introduction • Kenichi Ueno from Kyoto, Japan • iOS

    engineer for LINE Corporation for 8 years • Server-side Swift from the beginning of this year
  3. Self Introduction • Kenichi Ueno from Kyoto, Japan • iOS

    engineer for LINE Corporation for 8 years • Server-side Swift from the beginning of this year
  4. Self Introduction • Kenichi Ueno from Kyoto, Japan • iOS

    engineer for LINE Corporation for 8 years • Server-side Swift from the beginning of this year
  5. LINE Corporation • IT company based in Japan • LINE,

    messaging app • Clova, AI assistant speaker
  6. Clova Developer Center and Skill Store • 3rd party developers

    can build Skills • We provide SDK to help the development • Languages: Swift, node.js, python, elixir, kotlin and Java
  7. Why Swift? • Because I love Swift • It is

    fun to use it in various fields • It helps learning more about Swift and Server-side development
  8. Why is this allowed? • Reach more developers • Most

    Swift developers can maintain it • Our PM is a bit of a geek herself • We get to mention our company at this conference!
  9. Voice control Analyzed intent (HTTPS request) How the skill works

    3rd party server Clova Device and platform User
  10. Processed result (HTTPS response) Voice response Voice control Analyzed intent

    (HTTPS request) How the skill works 3rd party server Clova Device and platform User
  11. Server tasks 1. Verify the request 2. Parse the JSON

    request 3. Process the Intent 4. Return the JSON response
  12. Server tasks 1. Verify the request 2. Parse the JSON

    request 3. Process the Intent 4. Return the JSON response 1. Verify the request 2. Parse the JSON request 3. Process the Intent 4. Return the JSON response
  13. How to work with SDK • Add a dependency in

    Package.swift
 https://github.com/line/clova-cek-sdk-swift • Write handlers for:
 LaunchEvent
 IntentEvent
 SessionEndedEvent • Set environment variables:
 PORT
 APPLICATION_ID • Run the server
  14. Core features • Web server with Kitura • Message verification

    • Codable struct for the JSON request / response • Core part is pure Swift
  15. Why Kitura? • Met people from IBM at ‘try! Swift

    Tokyo’ • They provides a set of libraries
 CommonCrypto, OpenSSL, swift-ubuntu-docker, etc. • Good support in Swift@IBM slack channel • But I have not compared with other frameworks in detail
  16. Server Deployment • How to deploy a server that: •

    Supports HTTPS • Runs Swift • Can deploy with private code
  17. Server Deployment • ngrok • SSH tunneling from local machine

    • docker and SPM • Some PaaS supports docker and can host HTTPS • swift-ubuntu-docker enables Swift build and run • SPM can contain both sample project and library in a single repository
  18. 41. • Contains the sample app as executable
 You can

    immediately try it out • Contains the SDK as library
 Your project can include it in Package.swift products: [ .executable(name: "SampleApp", targets: ["SampleApp"]), .library( name: "AwesomeLibrary", targets: [“AwesomeLibrary"]), ], 1BDLBHFTXJGU
  19. Designing Protocol • Define a protocol to tell what is

    necessary
 • The protocol defines functions for each event
 launchHandler
 intentHandler
 sessionEndedHandler public protocol ExtensionRequestHandler { … } public func startServer(port: Int, paths: [ApiPath], handler: ExtensionRequestHandler) { … } 1SPUPDPMBOE'VODUJPO
  20. "TZODISPOPVTQSPDFTT • How to design such a function that enables

    async processing? • Do not return a response object
 ! launchHandler(request:) -> CEKResponse
  21. "TZODISPOPVTQSPDFTT • How to design such a function that enables

    async processing? • Do not return a response object
 ! launchHandler(request:) -> CEKResponse • Let a developer to invoke a callback that takes a response object
 launchHandler(request:next:) -> ()
  22. "TZODISPOPVTQSPDFTT • How to design such a function that enables

    async processing? • Do not return a response object
 ! launchHandler(request:) -> CEKResponse • Let a developer to invoke a callback that takes a response object
 launchHandler(request:next:) -> () func launchHandler(request: CEKRequest, next: @escaping (CEKResponse) -> ()) -> () &YUFOTJPO3FRVFTU)BOEMFS
  23. Server-to-server request • How to make a POST Request? •

    Use available library 
 ex. SwiftyRequest
  24. Server-to-server request • How to make a POST Request? •

    Use available library 
 ex. SwiftyRequest • URLSession is not 100% implemented for Linux
  25. Server-to-server request • How to make a POST Request? •

    Use available library 
 ex. SwiftyRequest • URLSession is not 100% implemented for Linux • Check the document for availability
 https://github.com/apple/swift-corelibs-foundation/blob/master/Docs/Status.md
  26. 4JHOBUVSF7FSJpDBUJPO • How to utilize RSA algorithm? • Use existing

    library if possible • If not, work with proper libraries • Security Framework for macOS • OpenSSL for Linux
 https://www.openssl.org/docs/manmaster/man3/
  27. OS version check • Some functions in Security framework requires


    macOS ≧ 10.12. How to Indicate OS version?
  28. OS version check • Some functions in Security framework requires


    macOS ≧ 10.12. How to Indicate OS version? • Attribute for if condition
 #available(OSX 10.12, *)
  29. OS version check • Some functions in Security framework requires


    macOS ≧ 10.12. How to Indicate OS version? • Attribute for if condition
 #available(OSX 10.12, *) • Build options for minimum target version
 swift build -Xswiftc "-target" 
 -Xswiftc "x86_64-apple-macosx10.12"
  30. Development for Linux • How to check if it can

    build and pass tests for Linux?
  31. Development for Linux • How to check if it can

    build and pass tests for Linux? • Develop in docker
  32. Development for Linux • How to check if it can

    build and pass tests for Linux? • Develop in docker • Edit XCTestManifests.swift to make sure all tests are included
  33. Development for Linux • How to check if it can

    build and pass tests for Linux? • Develop in docker • Edit XCTestManifests.swift to make sure all tests are included • Run tests for each environment
  34. import XCTest #if !os(macOS) public func __allTests() -> [XCTestCaseEntry] {

    return [ testCase(AwesomeLibraryTests.allTests), testCase(HelperMethodTests.allTests), ] } #endif 9$5FTU.BOJGFTUTTXJGU import AwesomeLibraryTests var tests = __allTests() XCTMain(tests) -JOVY.BJOTXJGU Test for Linux
  35. 6OTPMWFEQSPCMFNT • How to force a developer to call next

    in a function? • What is more efficient way to develop for Linux?