Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sinatra #27

Closed
jchannon opened this issue Jun 9, 2015 · 17 comments
Closed

Sinatra #27

jchannon opened this issue Jun 9, 2015 · 17 comments

Comments

@jchannon
Copy link

jchannon commented Jun 9, 2015

Are you planning to or know of any efforts to create a Swift framework inspired by Ruby's Sinatra?

I see this has some similarities but wondered if you planned to take it further?

@damian-kolakowski
Copy link
Member

hi @jchannon

I've not seen it yet.

I've just had a quick look at it looks like a nice source of:

  • templates language support
  • DSL for requests handling

I will take my time on this.

best

@jchannon
Copy link
Author

I contribute to a C# framework - http://nancyfx.org which is inspired from Sinatra

Would be good to see a Swift inspired fwk too 😄

@damian-kolakowski
Copy link
Member

@jchannon wow, I really like this:

Get["/greet/{name}"] = 

@jchannon
Copy link
Author

Lets do it! 😄

@tombuildsstuff
Copy link

+1

@jayrhynas
Copy link

You could do it somewhat like this:

class HttpServer {
  enum Method : String {
    case Get    = "GET"
    case Head   = "HEAD"
    case Post   = "POST"
    case Put    = "PUT"
    case Delete = "DELETE"
  }

  struct Router {
    let method: Method

    var handlers: [(expression: NSRegularExpression, handler: Handler)] = []

    init(_ method: Method) {
      self.method = method
    }

    subscript(path: String) -> Handler? {
      get {
        return nil
      }
      set (newValue) {
        do {
          let regex = try NSRegularExpression(pattern: path, options: NSRegularExpressionOptions(rawValue: 0))
          if let handler = newValue {
            handlers.append(expression: regex, handler: handler)
          }
        } catch {

        }
      }
    }
  }

  var Get    = Router(.Get)
  var Head   = Router(.Head)
  var Post   = Router(.Post)
  var Put    = Router(.Put)
  var Delete = Router(.Delete)

  // ...
}
var server = HttpServer()

server.Get["/login"] = { ... }
server.Post["/login"] = { ... }

@jayrhynas
Copy link

Of course you could also do it like Nancy:

class MyServer: HttpServer {
  override init() {
    super.init()

    Get["/login"] = { ... }
    Post["/login"] = { ... }
  }
}

@jchannon
Copy link
Author

Nice!!

On Thursday, 25 June 2015, Jayson Rhynas notifications@github.com wrote:

Of course you could also do it like Nancy:

class MyServer: HttpServer {
override init() {
super.init()

Get["/login"] = { ... }
Post["/login"] = { ... }

}
}


Reply to this email directly or view it on GitHub
#27 (comment).

@18601673727
Copy link

@glock45 consider Laravel or RoR?

@mwelser
Copy link

mwelser commented Oct 9, 2015

Hi...are u still following the idea to make a Sinatra inspired swift version ?!

When apple finally releases Swift for linux then "swifter" would be perfect for creating micro services

@tristaaan
Copy link

When apple finally releases Swift for linux ... "swifter" would be perfect for creating micro services

+1

@sdgandhi
Copy link

@mwelser and @tristaaan Swifter will not work on Linux even after Swift is open sourced and ported because it depends on Foundation, which Apple will not port to Linux or open source.

@damian-kolakowski
Copy link
Member

@sdgandhi we are going to get rid of the dependencies - #60

@damian-kolakowski
Copy link
Member

Basic support for Sinatra routing has been added recently: df24ced

@damian-kolakowski
Copy link
Member

hi guys ( @jchannon @sdgandhi @tristaaan @mwelser @18601673727 @jchannon @jayrhynas @julien-c and others ! )

This thread is the main place for discussing routing mechanism so let me summarise what we already have an what's the bright future :)

With version 1.0.5 we have a support for the following routing mechanism:

Example 1 ( new ):

let server = HttpServer()
server.GET['/books/:id'] = { ... } // For GET http method.

Example 2 ( old - still supported ):

let server = HttpServer()
server['/books/:id'] = { ... }     // For any http method.

We also extracted IO part from HttpServer class to HttpServerIO class:

class HttpServerIO {
    // socket IO to accept connection {
    //  delegate request handling to select() function.
    // }
    ...
    public func select(...) {
        return ([:], { _ in HttpResponse.NotFound })
   }
}

class HttpServer: HttpServerIO {
    let router: HttpRouter()
    ...
    override public func select(....) {
        return router.select(....)
    }
}

As you can see the only task for HttpServer class is to expose the routing API and perform the routing. It means everybody can now create a new cool routing DSLs and add is as another MyHttpServer or make an extension for current HttpServer without making huge changes in HttpServer class:

extension HttpServer {
    ...
}

or

class MyServer: HttpServerIO {
     ...
}  

Below I got examples of the new ideas I've been thinking about:

let server = HttpServer()
server < users << :id  = {

}

or

enum Router  {
    case users( (Int) -> {

        })
}

@BeQ
Copy link

BeQ commented Dec 25, 2015

Is it possible to use wildcard/regexp in routes?

Thanks and Happy Holidays!

@damian-kolakowski
Copy link
Member

hi @BeQ

Initially we supported regexps but we removed it :) Wildcard is supported. Please have a look at the example: https://github.com/glock45/swifter/blob/master/Sources/DemoServer.swift#L104

best
dk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants