Giter VIP home page Giter VIP logo

example's Introduction

Vapor Example

Fork this example project as a boilerplate for working with Vapor.

Check out the live demo running on Ubuntu.

Badges

Build Status PRs Welcome Slack Status

Deploy

Deploy

Documentation

View Vapor for documentation.

Requirements

Swift 3.0 preview 2 is required (Xcode 8 beta 2 on macOS).

Works on Ubuntu, Docker, Heroku, macOS.

Run the following script to check if you have Swift 3.0 beta 2 properly installed and configured.

curl -sL check.qutheory.io | bash

Building

Visit Getting Started in the documentation.

Compiling

If you have the Vapor Toolbox, use vapor new <project-name> to create your new application.

Then run vapor build and vapor run.

Otherwise, clone this repo and run swift build to compile your application, then run .build/debug/App.

Xcode 8

Run vapor xcode which will create the Xcode Project and open Xcode 8.

Xcode

Deploying

Check the Vapor documentation for more in-depth deployment instructions.

Upstart

To start your Vapor site automatically when the server is booted, add this file to your server.

You can check if Upstart is installed with

initctl --version

You may need to install Upstart if it is not already on your installation of Linux.

sudo apt-get install upstart

/etc/init/vapor-example.conf

description "Vapor Example"

start on startup

env PORT=8080

exec /home/<user_name>/vapor-example/.build/release/App --env=production

You additionally have access to the following commands for starting and stopping your server.

sudo stop vapor-example
sudo start vapor-example

The following script is useful for upgrading your website.

git pull
swift build --configuration release
sudo stop vapor-example
sudo start vapor-example

Heroku

Use the vapor heroku commands in the Vapor Toolbox to push to Heroku.

Docker

You can run this demo application locally in a Linux environment using Docker.

Make sure you have installed the Vapor Toolbox.

  1. Ensure Docker is installed on your local machine.
  2. Start the Docker terminal
  3. cd into vapor-example
  4. Create the Dockerfile vapor docker init
  5. Build the container vapor docker build
  6. Run the container vapor docker run
  7. Optionally enter the container vapor docker enter
  8. Configure VirtualBox to forward ports 8080 to 8080
  9. Visit http://0.0.0.0:8080

Nginx / Supervisor

You can also run your Vapor app through Nginx. It’s recommended you use Supervisor to run the app instance to protect against crashes and ensure it’s always running.

Supervisor

To setup Vapor running through Supervisor, follow these steps:

apt-get install -y supervisor

Edit the config below to match your environment and place it in /etc/supervisor/conf.d/your-app.conf:

[program:your-app]
command=/path/to/app/.build/release/App serve --ip=127.0.0.1 --port=8080
directory=/path/to/app
user=www-data
stdout_logfile=/var/log/supervisor/%(program_name)-stdout.log
stderr_logfile=/var/log/supervisor/%(program_name)-stderr.log

Now register the app with Supervisor and start it up:

supervisorctl reread
supervisorctl add your-app
supervisorctl start your-app # `add` may have auto-started, so disregard an “already started” error here

Nginx

With the app now running via Supervisor, you can use this sample nginx config to proxy it through Nginx:

server {
	server_name your.host;
	listen 80;

	root /path/to/app/Public;

	# Serve all public/static files via nginx and then fallback to Vapor for the rest
	try_files $uri @proxy;

	location @proxy {
		# Make sure the port here matches the port in your Supervisor config
		proxy_pass http://127.0.0.1:8080;
		proxy_pass_header Server;
		proxy_set_header Host $host;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_connect_timeout 3s;
		proxy_read_timeout 10s;
	}
}

example's People

Contributors

adriencanterot avatar eneko avatar hortontp avatar jchannon avatar ketzusaka avatar kevinvitale avatar kimar avatar loganwright avatar nathanflurry avatar rosslebeau avatar shnhrrsn avatar tanner0101 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

example's Issues

Would love example of CRUD

I'm sorry if an issue isn't the right place for this! I would personally implement and make a PR for this, but I don't know how — hence the request.

Anyway, I was hoping to see an example of a resource implementation. In the README for Vapor it mentions a "user" resource with a controller for it, but I would love to see how that actually works.

Thanks!

error: noSources

Hi, I'm following https://vapor.readme.io/docs/hello-world with the Vapor CLI. I can build OK with Xcode 7.3 toolchain DEVELOPMENT-SNAPSHOT-2016-05-31-a

swiftenv version inside the hello-world gives me

DEVELOPMENT-SNAPSHOT-2016-05-31-a (set by /Users/khoa/XcodeProject2/hello-world/.swift-version)

However, vapor build or swift build gives error

error: noSources("/Users/khoa/XcodeProject2/hello-world/DerivedData")

Need help getting your project to build?
Join our Slack where hundreds of contributors
are waiting to help: http://slack.qutheory.io

Error: Could not build project.
Note: Make sure you are using Swift 3.0 Snapshot 05-31

But I can swift build this vapor-example and run it 🐼

Vapor docker build will failed with Error 'returned a non-zero code: 8'

I have installed Vapor Toolbox v1.0.3 and when trying to build for docker image it always end with below error, or 'vapor docker init' command is now depreciated.

swiftenv version - 3.0.2
macos - Sierra (10.12)

Error: Docker build failed: The command '/bin/sh -c SWIFT_ARCHIVE_NAME=$SWIFT_VERSION-$SWIFT_PLATFORM && SWIFT_URL=https://swift.org/builds/$SWIFT_BRANCH/$(echo "$SWIFT_PLATFORM" | tr -d .)/$SWIFT_VERSION/$SWIFT_ARCHIVE_NAME.tar.gz && wget $SWIFT_URL && wget $SWIFT_URL.sig && gpg --verify $SWIFT_ARCHIVE_NAME.tar.gz.sig && tar -xvzf $SWIFT_ARCHIVE_NAME.tar.gz --directory / --strip-components=1 && rm -rf $SWIFT_ARCHIVE_NAME* /tmp/* /var/tmp/*' returned a non-zero code: 8

Compilation errors when building in Docker container

After following your instructions in the README and executing the docker build -t vapor . command, I get the following compilation errors:

Compiling Swift Module 'App' (3 sources)
/vapor/App/Middleware/SampleMiddleware.swift:3:7: error: type 'SampleMiddleware' does not conform to protocol 'Middleware'
class SampleMiddleware: Middleware {
      ^
Vapor.Middleware:16:24: note: protocol requires function 'handle(_:for:)' with type '(Handler, for: Application) -> Handler'
    public static func handle(handler: Vapor.Request.Type.Handler, for application: Vapor.Application) -> Vapor.Request.Type.Handler
                       ^
/vapor/App/Middleware/SampleMiddleware.swift:5:13: note: candidate has non-matching type '(Handler) -> Handler'
        class func handle(handler: Request.Handler) -> Request.Handler {
                   ^
/vapor/App/main.swift:49:10: error: value of optional type 'Session?' not unwrapped; did you mean to use '!' or '?'?
        request.session["name"] = "Vapor"
                ^
                       !
/vapor/App/Middleware/SampleMiddleware.swift:3:7: error: type 'SampleMiddleware' does not conform to protocol 'Middleware'
class SampleMiddleware: Middleware {
      ^
Vapor.Middleware:16:24: note: protocol requires function 'handle(_:for:)' with type '(Handler, for: Application) -> Handler'
    public static func handle(handler: Vapor.Request.Type.Handler, for application: Vapor.Application) -> Vapor.Request.Type.Handler
                       ^
/vapor/App/Middleware/SampleMiddleware.swift:5:13: note: candidate has non-matching type '(Handler) -> Handler'
        class func handle(handler: Request.Handler) -> Request.Handler {
                   ^
<unknown>:0: error: build had 1 command failures
swift-build: error: exit(1): /usr/bin/swift-build-tool -f /vapor/.build/debug.yaml default
The command '/bin/sh -c ./build' returned a non-zero code: 1

This is my Swift version:

$ docker run -it swiftdocker/swift bash
root@25f55f5da3cb:/# swift --version
Swift version 3.0-dev (LLVM b361b0fc05, Clang 11493b0f62, Swift 24a0c3de75)
Target: x86_64-unknown-linux-gnu

Any idea what's wrong?

building example failed

I tried to build the project, but ran into the following issue:

vagrant@vagrant-ubuntu-trusty-64:/vagrant/vapor-example$ swift build

> warning: refname '0.1.9' is ambiguous.
> warning: refname '0.1.9' is ambiguous.
> warning: refname '0.1.0' is ambiguous.
> warning: refname '0.1.0' is ambiguous.
> /vagrant/vapor-example/Packages/Stencil/Package.swift:6:6: error: type of expression is ambiguous without more context
>     .Package(url: "https://github.com/kylef/PathKit.git", majorVersion: 0, minor: 6),
>     ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> swift-build: exit(1): ["/home/vagrant/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04/usr/bin/swiftc", "--driver-mode=swift", "-I", "/home/vagrant/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04/usr/lib/swift/pm", "-L", "/home/vagrant/swift-2.2-SNAPSHOT-2015-12-01-b-ubuntu14.04/usr/lib/swift/pm", "-lPackageDescription", "/vagrant/vapor-example/Packages/Stencil/Package.swift"]

I use the following Swift build:

Swift version 2.2-dev (LLVM 46be9ff861, Clang 4deb154edc, Swift 778f82939c)

Exclusion of "Tests" in manifest file must be removed to use swift-test

There's an exclusion of the "Tests" directory in the example's Package.swift file, which causes a confusing error if one tries to add tests inside such a directory. The swift-test tool simply claims that no tests exist.

Logan Wright indicated on the Slack channel that this is probably a simple oversight.

It would be great for the exclusion to be removed from the default manifest.

Fix issues with Vapor 0.1.0

Due to upgrading to Vapor 0.1.0 some issues were introduced I've fixed those as well as added a Deploy to Swifton.me badge in my fork: https://github.com/swiftonme/vapor-example

Please also consider making it possible to inject a Port e.g. using Commander as does github.com/kylef/Curassow-example-helloworld.

Minor adjustments were necessary (e.g. hard-coding port to 8000) to make it be deployable on Swifton.me. I'd be happy to have this badge here and see it running there. Although this project is still very new, momentarily rather a P.o.C and shouldn't be used in a productive environment.

You can check it out by either of those ways:

Install Swifton.me CLI and deploy via commandline:*

npm install -g swifton
swifton deploy https://github.com/swiftonme/vapor-example

Using the One-click deployment:

https://serve.swifton.me/oneclick?repository=https://github.com/swiftonme/vapor-example

By using the HTTP/S API:
See https://serve.swifton.me.

Add DB

Add an example of using Fluent with something like Postgresql. Perhaps a basic user system

Project Refactoring

Love the idea of Vapor. I'm a huge fan of Laravel, but since Apple announced Swift would go open source I've wanting to move my web stuff over to Swift, Vapor being based on Laravel is exactly what I've been waiting for.

That being said, I'd like to add support for more Laravel features to Vapor, things like console commands and asset management specifically. The current directory structure for the example app doesn't really lend itself well to supporting this stuff like this without getting messy fast.

A standard Laravel project looks something like this:

app
   Console
      Commands
   Http
      Controllers
      Middleware
   etc...
resources
   assets
   lang
   views
public

By using the "Sources" directory right now, everything has to live at a top level due to how Swift PM works. If you put "Console" and "Http" subdirectories, you end up with Console and Http modules -- not ideal, especially since code isn't shared between without having to import the module.

Swift PM does let you put subdirectories inside subdirectories in Sources and it won't cascade, so we could do Sources/App/Http, Sources/App/Console, etc. This basically solves the problem but leaves you with an extra directory.

According to the Source Layout rules, if we omit a Sources directory entirely, it builds the root directory as a single module. This means we could refactor the directory structure to more closely align with Laravel:

App
   Console
      Commands
   Http
      Controllers
      Middleware
Resources
Public

I'd also like to propose adding a "views" subdirectory to Resources to keep things more organized, since it's a logical place to put asset files that need to be compiled, such as scss.

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.