Giter VIP home page Giter VIP logo

vapor's Introduction

Vapor

Vapor

A Laravel/Lumen Inspired Web Framework for Swift that works on iOS, OS X, and Ubuntu.

Getting Started

Clone the Example project to start making your application. This repository is for the framework module.

You must have Swift 2.2 or later installed. You can learn more about Swift 2.2 at Swift.org

Work in Progress

This is a work in progress, so don't rely on this for anything important. And pull requests are welcome!

Server

Starting the server takes two lines.

main.swift

import Vapor

let server = Server()
server.run()

You can also choose which port the server runs on.

server.run(port: 8080)

If you are having trouble connecting, make sure your ports are open. Check out apt-get ufw for simple port management.

Routing

Routing in Vapor is simple and very similar to Laravel.

main.swift

Route.get("welcome") { request in
	return "Hello"
}

//...start server

Here we will respond to all requests to http://example.com/welcome with the string "Hello".

JSON

Responding with JSON is easy.

Route.get("version") { request in
	return ["version": "1.0"]
}

This responds to all requests to http://example.com/version with the JSON dictionary {"version": "1.0"} and correct headers.

Views

You can also respond with HTML pages.

Route.get("/") { request in
	return View(path: "index.html")
}

Just put the index.html in the Resources folder at the root of your project and it will be served.

Public

All files put in the Public folder at the root of your project will be available at the root of your domain. This is a great place to put your assets (.css, .js, .png, etc).

Request

Every route call gets passed a Request object. This can be used to grab query and path parameters.

This is a list of the properties available on the request object.

public let method: Method
public var parameters: [String: String] = [:]
public var query: [String: String] = [:]

Controllers

Controllers are great for keeping your code organized. Route directives can take whole controllers or controller methods as arguments instead of closures.

main.swift

Route.get("/heartbeat/alternate", closure: HeartbeatController().index)

To pass a function name as a closure like above, the closure must have the function signature

func index(request: Request) -> AnyObject

Here is an example of a controller for returning an API heartbeat.

HearbeatController.swift

import Vapor

class HeartbeatController: Controller {

	override func index(request: Request) -> AnyObject {
		return ["lub": "dub"]
	}

}

Here the HeartbeatControllers's index method will be called when http://example.com/heartbeat/alternate is visited.

Resource Controllers

Resource controllers take advantage of CRUD-like index, show, store, update, destroy methods to make setting up REST APIs easy.

Route.resource("/user", controller: UserController()) //not yet implemented

This will create the appropriate GET, POST, DELETE, etc methods for individual and groups of users.

Deploying

Vapor has been successfully tested on Ubuntu 14.04 LTS (DigitalOcean) and Ubuntu 15.10 (VirtualBox).

To deploy to DigitalOcean, simply

  • Install Swift 2.2
    • wget the .tar.gz from Apple
    • Set the export PATH in your ~/.bashrc
    • (you may need to install binutils as well if you see ar not found)
  • Clone your fork of the vapor-example repository to the server
  • cd into the repository
    • Run swift build
    • Run .build/debug/MyApp
    • (you may need to run as sudo to use certain ports)
    • (you may need to install ufw to set appropriate ports)

My website http://tanner.xyz is currently running using this Vapor.

Swifter

This project is based on Swifter

vapor's People

Contributors

tanner0101 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.