Coder Social home page Coder Social logo

xiami121 / vapor Goto Github PK

View Code? Open in Web Editor NEW

This project forked from vapor/vapor

0.0 1.0 0.0 6.45 MB

An elegant web framework for Swift >=2.2 that works on OS X and Ubuntu.

Home Page: qutheory.io

License: MIT License

Swift 99.65% CSS 0.23% HTML 0.12%

vapor's Introduction

Vapor

Vapor

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

  • Pure Swift (No makefiles, module maps)
  • Modular
  • Beautifully expressive

Badges

Build Status Issue Stats PRs Welcome Slack Status codebeat badge

Introduction

Vapor is the first true web framework for Swift. It provides a beautifully expressive foundation for your app without tying you to any single server implementation. To learn more about Vapor's modularity, check out the Vapor Zewo Server or Vapor's protocol-oriented Drivers.

To start your own project with Vapor, fork the boilerplate code from Vapor Example.

Work in Progress

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

Documentation

Visit the Documentation for extensive documentation on getting setup, using, and contributing to Vapor.

Installation

Swift 2.2

Homebrew

brew tap qutheory/tap
brew install vapor-swift-2

Manual / Ubuntu

git clone https://github.com/qutheory/vapor
cd vapor
git checkout swift-2-2
sudo make install

List the available commands of the vapor CLI.

vapor help

Swift 3.0

Simply add Vapor as a dependency to your project's Package.swift.

.Package(url: "https://github.com/qutheory/vapor.git", majorVersion: 0)

For more detailed installation instructions, visit the Getting Started section in the Documentation.

Application

Starting the application takes two lines.

main.swift

import Vapor

let app = Application()
app.start()

You can also choose which port the server runs on.

app.start(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 expressive.

main.swift

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

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

JSON

Responding with JSON is easy.

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

This responds to all GET requests to http://example.com/version with the JSON dictionary {"version": "1.0"} and Content-Type: application/json.

Type Safe Routing

Vapor supports Frank inspired type-safe routing.

app.get("users", Int, "posts", String, "comments") { request, userId, postName in 
    return "You requested the comments for user #\(userId)'s post named \(postName))"
}

Here we will respond to all GET requests to http://example.com/users/<userId>/posts/<postName>/comments

You can also extend your own types to conform to Vapor's StringInitializable protocol. Here is an example where the User class conforms.

app.get("users", User) { request, user in
    return "Hello \(user.name)"
}

Now requesting a User is expressive and concise.

Views

You can also respond with HTML pages.

app.get("/") { request in
    return try app.view("index.html")
}

Or Stencil templates.

index.stencil

<html>
	<h1>{{ message }}</h1>
</html>
app.get("/") { request in
    return try app.view("index.stencil", context: ["message": "Hello"])
}

If you have VaporStencil added, just put the View file in the Resources folder at the root of your project and it will be served.

Response

A manual response can be returned if you want to set something like cookies.

app.get("cookie") { request in
	let response = Response(status: .OK, text: "Cookie was set")
	response.cookies["test"] = "123"
	return response
}

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.

Data

To access JSON, Query, and form-encoded data from the Request.

app.post("hello") { request in
	guard let name = request.data["name"]?.string else {
		return "Please include a name"
	}

	return "Hello, \(name)!"
}

Session

Sessions will be kept track of using the vapor-session cookie. The default session driver is a MemorySessionDriver. You can change the driver by setting Session.driver to a different object that conforms to SessionDriver.

if let name = request.session["name"] {
	//name was in session
}

//store name in session
request.session["name"] = "Vapor"

Database

Vapor was designed alongside Fluent, an Eloquent inspired ORM that empowers simple and expressive database management.

import Fluent

if let user = User.find(5) {
    print("Found \(user.name)")

    user.name = "New Name"
    user.save()
}

Underlying Fluent is a powerful Query builder.

let user = Query<User>().filter("id", notIn: [1, 2, 3]).filter("age", .GreaterThan, 21).first

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

app.get("heartbeat", closure: HeartbeatController().index)

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

func index(request: Request) -> ResponseRepresentable

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

HearbeatController.swift

import Vapor

class HeartbeatController: Controller {

	func index(request: Request) throws -> ResponseRepresentable {
		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.

app.resource("user", controller: UserController())

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

  • .Get /user - an index of users
  • .Get /user/:id - a single user etc

Middleware

Create a class conforming to Middleware to hook into server requests and responses. Append your classes to the server.middleware array in the order you want them to run..

class MyMiddleware: Middleware {
    func handle(handler: Request -> Response) -> (Request -> Response) {
        return { request in
            print("Incoming request from \(request.address)")

            let response = handler(request)

            print("Responding with status \(response.status)")

            return response
        }
    }
}

app.middleware.append(MyMiddleware)

Middleware can also be applied to a specific set of routes by using the app.middleware(_: handler:) method.

app.get("welcome") { ... }

app.middleware([AuthMiddleware]) {
   app.get("user") { ... }
}

In this example the AuthMiddleware will be applied to the user route but not the welcome route.

Providers

Providers and Drivers allow almost any component of Vapor to be extended or replaced.

app.providers.append(VaporFastServer.Provider)

Compatibility

Vapor has been tested on OS X 10.11, Ubuntu 14.04, and Ubuntu 15.10.

My website http://tanner.xyz as well as http://qutheory.io are currently running using Vapor on DigitalOcean.

Author

Made by Tanner Nelson

vapor's People

Contributors

tanner0101 avatar loganwright avatar shnhrrsn avatar ketzusaka avatar czechboy0 avatar artkay avatar kylebshr avatar svanimpe avatar joannis avatar damuellen avatar chaselatta avatar coryalder avatar matteocrippa avatar prayagverma avatar rothomp3 avatar steffendsommer avatar timominous avatar tnantoka avatar tqtifnypmb 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.