A Ruby on Rails inspired Web Framework for Swift that runs on Linux and OS X

Related tags

Networking Swifton
Overview

IMPORTANT! We don't see any way how to make web development as great as Ruby on Rails or Django with a very static nature of current Swift. We hope that things will change at some point and we will return to active development.

Swifton

A Ruby on Rails inspired Web Framework for Swift that runs on Linux and OS X.

Build Status Linux Mac OS X Swift 2 compatible MIT license codebeat badge

Getting Started

  • Install Development snapshot version from Swift.org or via swiftenv. If you are on OSX I highly recommend swiftenv - latest Swift will be able to coexist with system wide Swift that comes with Xcode.
  • swift --version should show something like: Swift version 3.0-dev ...
  • Checkout TodoApp example project.
  • Run swift build inside app (most of dependencies throw deprecation warnings).
  • Run ./.build/debug/Swifton-TodoApp.
  • Open http://0.0.0.0:8000/todos in your browser.

Contributing

Contributions are more than welcome! The easiest way to start contributing to Swifton:

  • Setup TodoApp
  • Pick one issue from the issues list or propose enhancement.
  • You can find Swifton source code in Swifton-TodoApp/Packages/Swifton-<version> directory. Packages inside Packages directory comes with Git repository so feel free to do you changes there.
  • Compile and test TodoApp, this will help to check your changes and avoid regressions.
  • Write tests and run it swift build && swift test (run rm -r Packages/*/Tests inside Swifton folder if tests crashes)
  • Commit and push your changes, open pull request.
  • Enjoy ;)

Routing

Swifton comes with ready to use Router, also you can use any router as long as it accepts Request and returns Response. Routes are defined in main.swift file. Configured Router is passed to S4 interface supporting server. Router allows to define resources and regular routes.

...
let router = Router.create { route in
  route.resources("todos", controller: TodosController())
}
...

Which is equivalent to:

let router = Router()
router.get("/todos/new", TodosController()["new"])
router.get("/todos/{id}", TodosController()["show"])
router.get("/todos/{id}/edit", TodosController()["edit"])
router.get("/todos", TodosController()["index"])
router.post("/todos", TodosController()["create"])
router.delete("/todos/{id}", TodosController()["destroy"])
router.patch("/todos/{id}", TodosController()["update"])

Configured routes then are passed to application server.

...
serve { request in
    router.respond(request) 
}
...

Controllers

A controller inherits from ApplicationController class, which inherits from Controller class. Action is a closure that accepts Request object and returns Response object.

class TodosController: ApplicationController { 
    // shared todo variable used to pass value between setTodo filter and actions
    var todo: Todo?    
    override func controller() {
    super.controller()
    // sets before filter setTodo only for specified actions.
    beforeAction("setTodo", only: ["show", "edit", "update", "destroy"])

    // render all Todo instances with Index template (in Views/Todos/Index.html.stencil)
    action("index") { request in
        let todos = ["todos": Todo.allAttributes()]
        return render("Todos/Index", todos)
    }

    // render Todo instance that was set in before filter
    action("show") { request in
        return render("Todos/Show", self.todo)
    }

    // render static New template
    action("new") { request in
        return render("Todos/New")
    }

    // render Todo instance's edit form
    action("edit") { request in
        return render("Todos/Edit", self.todo)
    } 

    // create new Todo instance and redirect to list of Todos 
    action("create") { request in
        Todo.create(request.params)
        return redirectTo("/todos")
    }
    
    // update Todo instance and redirect to updated Todo instance
    action("update") { request in
        self.todo!.update(request.params)
        return redirectTo("/todos/\(self.todo!.id)")
    }

    // destroy Todo instance
    action("destroy") { request in
        Todo.destroy(self.todo)
        return redirectTo("/todos")
    }

    // set todo shared variable to actions can use it
    filter("setTodo") { request in
        // Redirect to "/todos" list if Todo instance is not found 
        guard let t = Todo.find(request.params["id"]) else { return self.redirectTo("/todos") } 
        self.todo = t as? Todo
        // Run next filter or action
        return self.next
    }

}}

Controller Responders

respondTo allows to define multiple responders based client Accept header:

...
action("show") { request in
    return respondTo(request, [
        "html": { render("Todos/Show", self.todo) },
        "json": { renderJSON(self.todo) }
    ])
}
...

Controller Filters

Swifton Controllers support beforeAction and afterAction filters, which run filters before or after action correspodingly. Filter is a closure that returns Response?. Controller proceeds execution only if filter returns self.next (which is actually nil), otherwise it returns Response object and doesn't proceed execution of other filters and action.

filter("setTodo") { request in
    // Redirect to "/todos" list if Todo instance is not found
    guard let t = Todo.find(request.params["id"]) else { return self.redirectTo("/todos") }
    self.todo = t as? Todo
    // Run next filter or action
    return self.next
}

Models

Swifton is ORM agnostic web framework. You can use any ORM of your choice. Swifton comes with simple in-memory MemoryModel class that you can inherit and use for your apps. Simple as this:

class User: MemoryModel {
}

...

User.all.count // 0
var user = User.create(["name": "Saulius", "surname": "Grigaitis"])
User.all.count // 1
user["name"] // "Saulius"
user["surname"] // "Grigaitis"
user.update(["name": "James", "surname": "Bond"])
user["surname"] // "Bond"
User.destroy(user)
User.all.count // 0

Few options if you need persistence:

Views

Swifton supports Mustache like templates via Stencil template language. View is rendered with controller's method render(template_path, object). Object needs either to conform to HTMLRenderable protocol, either be [String: Any] type where Any allows to pass complex structures.

<tbody>
  {% for todo in todos %}
    <tr>
      <td>{{ todo.title }}</td>
      <td>{{ todo.completed }}</td>
      <td><a href="/todos/{{ todo.id }}">Show</a></td>
      <td><a href="/todos/{{ todo.id }}/edit">Edit</a></td>
      <td><a data-confirm="Are you sure?" rel="nofollow" data-method="delete" href="/todos/{{ todo.id }}">Destroy</a></td>
    </tr>
  {% endfor %}
</tbody>

Views are loaded from Views directory by default, you can also change this default setting by changing value of SwiftonConfig.viewsDirectory (preferable in main.swift file). Currently views are not cached, so you don't need to restart server or recompile after views are changed.

Static assets (JavaScript, CSS, images etc.) are loaded from Public directory by default, you can also change this default setting by changing value of SwiftonConfig.publicDirectory (preferable in main.swift file).

JSON support

renderJSON(object) generates and returns JSON of an object. Object must conform to JSONRenderable protocol.

action("show") { request in
    return respondTo(request, [
        "html": { render("Todos/Show", self.todo) },
        "json": { renderJSON(self.todo) }
    ])
}

Middleware

main.swift is probably best place to put middleware. Simply wrap Router instance with your middleware, you can even nest multiple middlewares.

...
serve { request in
    router.respond(request) 
}
...

Application Server

Swifton comes with VeniceX based HTTP server. Swifton supports S4 HTTP standards for Swift so you can easily use any S4 supporting server.

Building for production

Build release configuration for better performance:

$ swift build --configuration release

Deployment

Heroku

Example TodoApp can be deployed to Heroku using the heroku-buildpack-swift.

Click the button below to automatically set up this example to run on your own Heroku account.

Deploy to Heroku

Docker

Swifton can be deployed with Docker. Some examples how to deploy it with Docker:

Comments
  • A woman getting murdered as a project image?

    A woman getting murdered as a project image?

    Would you reconsider using this photo to represent this project? Is this the message you want to send to someone who sees this project page for the first time?

    EDIT: Not to mention that it's probably a copyright infringement.

    opened by selfawaresoup 39
  • Add MimeType checking in Router

    Add MimeType checking in Router

    This adds support for the mimeType() function on String? types. The previous implementation allowed to pass nil values to the internal MimeType(ext: String?) -> String function but .mimeType() could never be called on a nil String because that was an extension on the String type. This allows us to do this

    let testString :String? = nil
    testString.mimeType() //  -> "application/octet-stream"
    

    Instead of this

    let testString : String? = nil
    (testString ?? "").mimeType() // -> "application/octet-stream"
    

    Or worse

    let testString : String? = nil
    testString.extension?.mimeType ?? "application/octet-stream" // ->"application/octet-stream"
    

    This new implementation is used in the router and fixes #25

    opened by califrench 10
  • Add support for Stencil include tag

    Add support for Stencil include tag

    By giving all Stencil views a default context with a TemplateLoader we can add global support for the Stencil {% include ... %} tag. This saves us from having to create a template loader on each request or having to modify the ApplicationController like so:

    import Swifton
    import Inquiline
    import Stencil
    import PathKit
    
    class ApplicationController: Controller {
        override init() { super.init() }
    
        override func render(template: String, _ context: [String: Any]) -> Response {
            var newContext = context
            newContext["loader"] = TemplateLoader(paths: ["Views"])
            return super.render(template, newContext)
        }
    
        override func render(template: String) -> Response {
            return super.render(template, ["loader": TemplateLoader(paths: ["Views"]), "page": Path(template).components.first?.lowercaseString])
        }
    
    }
    

    Please refer to Stencil for more information.

    opened by califrench 8
  • Update README.md with a code quality badge

    Update README.md with a code quality badge

    Please consider adding the badge and check out the generated quality report. I hope codebeat can help all contributors deliver quality code and help newcomers dive into the codebase by starting with small refactorings.

    codebeat badge

    Disclaimer: I am the co-author of the tool and we've recently released Swift support. I'd love to hear your feedback

    opened by grodowski 6
  • Files in public directory being opened as UTF8 Strings

    Files in public directory being opened as UTF8 Strings

    I'm trying to load images from the public directory but it seems from looking at the router code that anything loaded from the public directory needs to be source code—like css or js files.

    // In Router.swift
    if let body = String(data:contents!, encoding: NSUTF8StringEncoding) {
        return Response(.Ok, contentType: "text/plain; charset=utf8", body: body)
    }
    

    We should be able to also load images and other assets from that directory. Also CSS files and JS files should return a different mimetype than text/plain.

    opened by califrench 5
  • Change custom router to Zewo/Router

    Change custom router to Zewo/Router

    Migrated to DEVELOPMENT-SNAPSHOT-2016-05-03-a.

    Swifton router was dependent on URITemplate package, which is not maintained anymore, so I changed it to Zewo Router.

    Resolves #34

    opened by dostu 3
  • Add SwiftLint

    Add SwiftLint

    We need to enforce clean code.

    Add SwiftLint to automatically check code style.

    • Run SwiftLint autocorrect and fix minor violations
    • Add .swiftlint.yml config
    • Add Xcode build phase to run linter on each build
    opened by dostu 3
  • Build with error:error: NoSources(

    Build with error:error: NoSources("xxxxxxx/Packages/PathKit-0.6.1/Tests/Fixtures")

    Seem's like there's no suck dictionary or files, bug:

    vagrant@vagrant-ubuntu-wily-64:~/test_proj1/Packages/PathKit-0.6.1/Tests$ ll total 36 drwxrwxr-x 3 vagrant vagrant 4096 Mar 14 13:36 ./ drwxrwxr-x 8 vagrant vagrant 4096 Mar 14 13:36 ../ drwxrwxr-x 5 vagrant vagrant 4096 Mar 14 13:36 Fixtures/ -rw-rw-r-- 1 vagrant vagrant 733 Mar 14 13:36 Info.plist -rw-rw-r-- 1 vagrant vagrant 2293 Mar 14 13:36 ManipulationTests.swift -rw-rw-r-- 1 vagrant vagrant 12605 Mar 14 13:36 PathKitTests.swift vagrant@vagrant-ubuntu-wily-64:~/test_proj1/Packages/PathKit-0.6.1/Tests$ vagrant@vagrant-ubuntu-wily-64:~/test_proj1/Packages/PathKit-0.6.1/Tests$ vagrant@vagrant-ubuntu-wily-64:~/test_proj1/Packages/PathKit-0.6.1/Tests$ vagrant@vagrant-ubuntu-wily-64:~/test_proj1/Packages/PathKit-0.6.1/Tests$ cd Fixtures/ vagrant@vagrant-ubuntu-wily-64:~/test_proj1/Packages/PathKit-0.6.1/Tests/Fixtures$ ll total 20 drwxrwxr-x 5 vagrant vagrant 4096 Mar 14 13:36 ./ drwxrwxr-x 3 vagrant vagrant 4096 Mar 14 13:36 ../ drwxrwxr-x 3 vagrant vagrant 4096 Mar 14 13:36 directory/ -rw-rw-r-- 1 vagrant vagrant 0 Mar 14 13:36 file drwxrwxr-x 2 vagrant vagrant 4096 Mar 14 13:36 permissions/ drwxrwxr-x 2 vagrant vagrant 4096 Mar 14 13:36 symlinks/ vagrant@vagrant-ubuntu-wily-64:~/test_proj1/Packages/PathKit-0.6.1/Tests/Fixtures$

    opened by ipconfiger 3
  • error: no such module 'Spectre'

    error: no such module 'Spectre'

    For some reason I got...

    root@63612e37d9d8:/Swifton-TodoApp# swift --version
    Swift version 3.0-dev (LLVM b361b0fc05, Clang 11493b0f62, Swift fc261045a5)
    Target: x86_64-unknown-linux-gnu
    root@63612e37d9d8:/Swifton-TodoApp# ls
    Dockerfile  MIT-LICENSE  Package.swift  Packages  Procfile  Public  README.md  Sources  Views  app.json
    root@63612e37d9d8:/Swifton-TodoApp# swift build
    Compiling Swift Module 'TemplateLoadertest' (2 sources)
    Compiling Swift Module 'Nodestest' (4 sources)
    /Swifton-TodoApp/Packages/Stencil-0.5.2/Tests/TemplateLoader/InheritenceSpec.swift:8:21: warning: __FILE__ is deprecated and will be removed in Swift 3, please use #file
        let path = Path(__FILE__) + ".." + ".." + "fixtures"
                        ^~~~~~~~
                        #file
    /Swifton-TodoApp/Packages/Stencil-0.5.2/Tests/TemplateLoader/InheritenceSpec.swift:1:8: error: no such module 'Spectre'
    import Spectre
           ^
    /Swifton-TodoApp/Packages/Stencil-0.5.2/Tests/TemplateLoader/InheritenceSpec.swift:8:21: warning: __FILE__ is deprecated and will be removed in Swift 3, please use #file
        let path = Path(__FILE__) + ".." + ".." + "fixtures"
                        ^~~~~~~~
                        #file
    /Swifton-TodoApp/Packages/Stencil-0.5.2/Tests/TemplateLoader/InheritenceSpec.swift:1:8: error: no such module 'Spectre'
    import Spectre
           ^
    /Swifton-TodoApp/Packages/Stencil-0.5.2/Tests/Nodes/NodeSpec.swift:1:8: error: no such module 'Spectre'
    import Spectre
           ^
    /Swifton-TodoApp/Packages/Stencil-0.5.2/Tests/Nodes/NodeSpec.swift:1:8: error: no such module 'Spectre'
    import Spectre
           ^
    /Swifton-TodoApp/Packages/Stencil-0.5.2/Tests/Nodes/NodeSpec.swift:1:8: error: no such module 'Spectre'
    import Spectre
           ^
    /Swifton-TodoApp/Packages/Stencil-0.5.2/Tests/Nodes/NodeSpec.swift:1:8: error: no such module 'Spectre'
    import Spectre
           ^
    <unknown>:0: error: build had 2 command failures
    error: exit(1): /usr/bin/swift-build-tool -f /Swifton-TodoApp/.build/debug.yaml default
    
    

    Any hint? Thanks

    opened by katopz 3
  • UTF8 issue

    UTF8 issue

    Somewhere in the chain encoding is messed up - if I enter issue name "ąęčįčęėįčęėįčęš" in TodoApp I actually get "ąęčįčęėįčęėįčęš "

    bug help wanted 
    opened by sauliusgrigaitis 3
  • Added mime-type appropriate response for text files

    Added mime-type appropriate response for text files

    The ContentType enum has been updated to support all the mime-types in MimeType.swift in Response.swift

    The router has also bee updated to automatically return the correct content-type based on the file extension.

    Fixes #41

    opened by califrench 2
  • "static nature of swift" remark

    I appreciate your frankness (and I'm sure frustration) of your remark:

    IMPORTANT! We don't see any way how to make web development as great as Ruby on Rails or Django with a very static nature of current Swift. We hope that things will change at some point and we will return to active development.

    Could you please expound upon this and provide more reasoning as to how (and perhaps maybe why) this is the case? Personally I'm curious about how Swift has this lock-in when compared to Crystal (albeit Crystal is modeled after Ruby and I can see somewhat the difference). I'm just hoping to see things through your eyes better after working on this so diligently as you have and to understand the walls you hit more clearly.

    opened by ylluminate 5
  • Problems using Swift Project Manager.

    Problems using Swift Project Manager.

    Using the last version of Swift (Swift version 3.0-dev)

    When executing: swift build

    Cloning https://github.com/necolt/Stencil.git
    HEAD is now at ee7c76f Merge pull request #3 from weby/master
    Resolved version: 0.5.6
    Cloning https://github.com/necolt/PathKit.git
    HEAD is now at c99537a Merge pull request #2 from weby/master
    Resolved version: 0.6.4
    Empty manifest file is not supported anymore. Use `swift package init` to autogenerate.
    error: The package at `/bitnami/swift/Swifton/Packages/PathKit.git' has no Package.swift for the specific version: 0.6.4
    

    It seems that the version of the dependencies are correct.

    opened by juan131 3
  • Using plain methods in controller instead of actions() with closures

    Using plain methods in controller instead of actions() with closures

    I was thinking if it's possible to map routes directly to methods of Controller. For example:

    router.get("/", TodosController().index)
    etc
    

    Resources can be implemented by adopting protocols. I.e. TodosController adopts Resource protocol and implements some or all of the protocol's methods. router.resources() will expect a class which adopts that protocol.

    Filtering can be implemented by looking up method names via reflection.

    What do you think of this approach?

    opened by zmeyc 9
  • Router not returning assets that aren't text

    Router not returning assets that aren't text

    This is kind of the same as #25 but that issue encompassed two issues:

    1. All static files were returned in a response with a "text/plain" mimeType -> Now fixed
    2. Media assets can't be returned because the following line failed
    // Router.swift:111
    ...
    if let body = String(data:contents!, encoding: NSUTF8StringEncoding) {
    ...
    

    Any file that is a jpeg or png etc... Returns a nil body on this line which leads to this code returning a notFound Response:

    // Router.swift:95
    if let staticFile = serveStaticFile(newRequest) {
        return staticFile as! Response
    }
    
    return notFound(newRequest) as! Response
    

    So we are still having an issue with not returning media assets this isn't in line with the README

    Static assets (JavaScript, CSS, images etc.) are loaded from Public directory by default This is crucial and needs to be fixed before Swifton can be useful. I can't imagine a website without any media assets!

    I'm willing to investigate this much further but we may need to modify the router to return ResponseTypes from the router instead of Inquiline Response types... Of course any other thoughts are welcome!

    opened by califrench 1
  • Demo App ideas

    Demo App ideas

    TodoApp is good starting point, but would be really great to build fully featured app that developers could use as a reference. Please suggest ideas and discuss about it here. Few initial ideas:

    • TodoApp - we can extend current TodoApp, but it's well know that a Todo App doesn't represent complexities that usual real world apps has.
    • E-Commerce - boring, but definitely would help to findout missing features in Swifton
    • New Startup - this would be most exciting, any hustlers here that would like to get their idea implemented?
    opened by sauliusgrigaitis 4
Owner
Saulius Grigaitis
Saulius Grigaitis
iOS/Mac OS framework for Rails

NSRails is a lightweight framework that makes mapping client-side objects to remote Rails objects a breeze, and calling CRUD operations and others sup

Dan Hassin 523 Nov 19, 2022
Lightweight library for web server applications in Swift on macOS and Linux powered by coroutines.

Why Zewo? • Support • Community • Contributing Zewo Zewo is a lightweight library for web applications in Swift. What sets Zewo apart? Zewo is not a w

Zewo 1.9k Dec 22, 2022
Socket framework for Swift using the Swift Package Manager. Works on iOS, macOS, and Linux.

BlueSocket Socket framework for Swift using the Swift Package Manager. Works on iOS, macOS, and Linux. Prerequisites Swift Swift Open Source swift-5.1

Kitura 1.3k Dec 26, 2022
Swift backend / server framework (Pure Swift, Supports Linux)

NetworkObjects NetworkObjects is a #PureSwift backend. This framework compiles for OS X, iOS and Linux and serves as the foundation for building power

Alsey Coleman Miller 258 Oct 6, 2022
RestKit is a framework for consuming and modeling RESTful web resources on iOS and OS X

RestKit RestKit is a modern Objective-C framework for implementing RESTful web services clients on iOS and Mac OS X. It provides a powerful object map

The RestKit Project 10.2k Dec 29, 2022
A Swift web framework and HTTP server.

A Swift Web Framework and HTTP Server Summary Kitura is a web framework and web server that is created for web services written in Swift. For more inf

Kitura 7.6k Jan 6, 2023
A minimal, fast and unopinionated web framework for Swift

![Fire Image] (http://i.imgur.com/1qR6Nl4.png) Blackfire An extremely fast Swift web framework ?? Getting Started If you're familiar with express.js t

Elliott Minns 908 Dec 2, 2022
A Swift Multiplatform Single-threaded Non-blocking Web and Networking Framework

Serverside non-blocking IO in Swift Ask questions in our Slack channel! Lightning (formerly Edge) Node Lightning is an HTTP Server and TCP Client/Serv

SkyLab 316 Oct 6, 2022
Minimal web framework and middleware for Swift

Kunugi Kunugi(椚) is minimal web framework and middleware systems for Swift. This is inpired by Node.js' Koa. Kunugi doesn't provide http server its se

Yusuke Ito 35 Apr 18, 2022
💧 A server-side Swift web framework.

Vapor is a web framework for Swift. It provides a beautifully expressive and easy to use foundation for your next website, API, or cloud project. Take

Vapor 22.4k Jan 2, 2023
Super lightweight web framework in Swift based on SWSGI

Ambassador Super lightweight web framework in Swift based on SWSGI Features Super lightweight Easy to use, designed for UI automatic testing API mocki

Envoy 170 Nov 15, 2022
libuv base Swift web HTTP server framework

Notice Trevi now open a Trevi Community. Yoseob/Trevi project split up into respective Trevi, lime, middlewares and sys packages at our community. If

leeyoseob 46 Jan 29, 2022
SwiftSoup: Pure Swift HTML Parser, with best of DOM, CSS, and jquery (Supports Linux, iOS, Mac, tvOS, watchOS)

SwiftSoup is a pure Swift library, cross-platform (macOS, iOS, tvOS, watchOS and Linux!), for working with real-world HTML. It provides a very conveni

Nabil Chatbi 3.7k Dec 28, 2022
A tool to build projects on MacOS and a remote linux server with one command

DualBuild DualBuild is a command line tool for building projects on MacOS and a remote Linux server. ##Setup Install the repository git clone https://

Operator Foundation 0 Dec 26, 2021
SwiftCANLib is a library used to process Controller Area Network (CAN) frames utilizing the Linux kernel open source library SOCKETCAN.

SwiftCANLib SwiftCANLib is a library used to process Controller Area Network (CAN) frames utilizing the Linux kernel open source library SOCKETCAN. Th

Tim Wise 4 Oct 25, 2021
Swift Express is a simple, yet unopinionated web application server written in Swift

Documentation <h5 align="right"><a href="http://demo.swiftexpress.io/">Live ?? server running Demo <img src="https://cdn0.iconfinder.com/data/icons/

Crossroad Labs 850 Dec 2, 2022
This generic SOAP client allows you to access web services using a your iOS app, Mac OS X app and AppleTV app.

This generic SOAP client allows you to access web services using a your iOS app, Mac OS X app and Apple TV app. With this Framework you can create iPh

Prioregroup.com 479 Nov 22, 2022
Robust Swift networking for web APIs

Conduit Conduit is a session-based Swift HTTP networking and auth library. Within each session, requests are sent through a serial pipeline before bei

Mindbody 52 Oct 26, 2022
A lightweight library for writing HTTP web servers with Swift

Taylor Disclaimer: Not actively working on it anymore. You can check out some alternatives Swift 2.0 required. Working with Xcode 7.1. Disclaimer: It

Jorge Izquierdo 925 Nov 17, 2022