httpswift / swifter

Tiny http server engine written in Swift programming language.
BSD 3-Clause "New" or "Revised" License
3.91k stars 537 forks source link

Sinatra #27

Closed jchannon closed 8 years ago

jchannon commented 9 years ago

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 commented 9 years ago

hi @jchannon

I've not seen it yet.

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

I will take my time on this.

best

jchannon commented 9 years ago

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

Would be good to see a Swift inspired fwk too :smile:

damian-kolakowski commented 9 years ago

@jchannon wow, I really like this:

Get["/greet/{name}"] = 
jchannon commented 9 years ago

Lets do it! :smile:

tombuildsstuff commented 9 years ago

+1

jayrhynas commented 9 years ago

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 commented 9 years ago

Of course you could also do it like Nancy:

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

    Get["/login"] = { ... }
    Post["/login"] = { ... }
  }
}
jchannon commented 9 years ago

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 https://github.com/glock45/swifter/issues/27#issuecomment-115397523.

18601673727 commented 9 years ago

@glock45 consider Laravel or RoR?

mwelser commented 9 years ago

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 commented 9 years ago

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

+1

sdgandhi commented 9 years ago

@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 commented 8 years ago

@sdgandhi we are going to get rid of the dependencies - https://github.com/glock45/swifter/issues/60

damian-kolakowski commented 8 years ago

Basic support for Sinatra routing has been added recently: https://github.com/glock45/swifter/commit/df24cedc2b71cf8e0dbebccb0cba905918842595

damian-kolakowski commented 8 years ago

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 commented 8 years ago

Is it possible to use wildcard/regexp in routes?

Thanks and Happy Holidays!

damian-kolakowski commented 8 years ago

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