Kitura is a new package based web framework for Swift 3.0 by IBM. As Swift is a compiled language, your Kitura application compiles to a binary that can either act as its own webserver (by default, on port 8090) or as a FastGCI server for use with Nginx or Apache.
Features
- URL routing (GET, POST, PUT, DELETE)
- URL parameters
- Static file serving
- FastCGI support
- SSL/TLS support
- JSON parsing
- Pluggable middleware
Starting first project
To create executable package, create and switch to a directory. For example I have created a directory with name ‘Todoapp’ and moved in.Now in the terminal,
$ swift package init
will create folder structure for Kitura as shown
Todoapp
├── Package.swift
├── Sources
│ └── main.swift
└── Tests
Tip : In case swift file ~/Sources/Todoapp.swift created then rename to ~/Sources/main.swift.
In the Package.swift add ‘dependencies’.
/***** Package.swift ******/
import PackageDescription
let package = Package(
name: “Todoapp",
dependencies: [
.Package(url: "https://github.com/IBM-Swift/Kitura.git", majorVersion: 1),
])
Note :
- Mentioned versions will change in future.
- Careful while mentioning versions of packages.
When you build, it will download and compile all the package dependencies in Packages directory.
$ swift build
First time it will start downloading dependencies of specified in the package followed by compilation like below
Resolved version: 1.3.0 Cloning https://github.com/IBM-Swift/LoggerAPI.git HEAD is now at 06272da Check logging level before invoking logger implementation (#17) Resolved version: 1.3.0 Cloning https://github.com/IBM-Swift/BlueSocket.git HEAD is now at 9daa891 Merge pull request #43 from BDHernand/master Resolved version: 0.12.15 Cloning https://github.com/IBM-Swift/CCurl.git HEAD is now at 3cfb752 Add header callback helper function (#9) Resolved version: 0.2.3 Cloning https://github.com/IBM-Swift/BlueSSLService.git HEAD is now at 417d4e4 Merge pull request #9 from BDHernand/master Resolved version: 0.12.9 Cloning https://github.com/IBM-Swift/SwiftyJSON.git HEAD is now at 9c8a8a0 Merge pull request #28 from BDHernand/master Resolved version: 15.0.2 Cloning https://github.com/IBM-Swift/Kitura-TemplateEngine.git HEAD is now at 4fe7829 Support Swift 3.0.1 (#9) Resolved version: 1.1.0 Compile Swift Module 'Socket' (3 sources) Compile Swift Module 'LoggerAPI' (1 sources) Compile Swift Module 'KituraTemplateEngine' (1 sources) Compile Swift Module 'SwiftyJSON' (2 sources) Compile Swift Module 'SSLService' (1 sources) Compile CHTTPParser utils.c Compile CHTTPParser http_parser.c Linking CHTTPParser Compile Swift Module 'KituraNet' (34 sources) Compile Swift Module 'Kitura' (42 sources) ......
After success, let's start with Print(“Hello World !”); in main.swift
Build again, and now you can see debug directory has exec file when it says :
Linking ./.build/debug/Todoapp
When execute, it will print those string.
$ .build/debug/Todoapp
Hello World !
Serving GET request
In your main.swift,/********* main.swift *******/
import Foundation
import Kitura
import LoggerAPI
import SwiftyJSON
// Disable buffering
setbuf(stdout, nil)
// setup routes
let router = Router()
router.get("/") { _, response, next in
response.status(.OK).send(json: JSON([“Title 1" : “Task 1"]))
next()
}
// Start server
Log.info("Starting server")
Kitura.addHTTPServer(onPort: 8090, with: router)
Kitura.run()
Open Browser and hit http://localhost:8090
Will give you response.
{ “Title 1” : “Task 1” }
Additional Tips:
1. If you got error 'The dependency graph could not be satisfied because an update to XXXXX.git Required'Update out dated packages with command :
$ swift package update
2 . Generate Xcode project from the root will make easy to manage your project structure.
$ swift package generate-xcodeproj
Get complete project in my Github.