Giter Club home page Giter Club logo

Comments (17)

damian-kolakowski avatar damian-kolakowski commented on June 27, 2024

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

from swifter.

jchannon avatar jchannon commented on June 27, 2024

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

Would be good to see a Swift inspired fwk too 😄

from swifter.

damian-kolakowski avatar damian-kolakowski commented on June 27, 2024

@jchannon wow, I really like this:

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

from swifter.

jchannon avatar jchannon commented on June 27, 2024

Lets do it! 😄

from swifter.

tombuildsstuff avatar tombuildsstuff commented on June 27, 2024

+1

from swifter.

jayrhynas avatar jayrhynas commented on June 27, 2024

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"] = { ... }

from swifter.

jayrhynas avatar jayrhynas commented on June 27, 2024

Of course you could also do it like Nancy:

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

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

from swifter.

jchannon avatar jchannon commented on June 27, 2024

Nice!!

On Thursday, 25 June 2015, Jayson Rhynas [email protected] 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).

from swifter.

18601673727 avatar 18601673727 commented on June 27, 2024

@glock45 consider Laravel or RoR?

from swifter.

mwelser avatar mwelser commented on June 27, 2024

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

from swifter.

tristaaan avatar tristaaan commented on June 27, 2024

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

+1

from swifter.

sdgandhi avatar sdgandhi commented on June 27, 2024

@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.

from swifter.

damian-kolakowski avatar damian-kolakowski commented on June 27, 2024

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

from swifter.

damian-kolakowski avatar damian-kolakowski commented on June 27, 2024

Basic support for Sinatra routing has been added recently: df24ced

from swifter.

damian-kolakowski avatar damian-kolakowski commented on June 27, 2024

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) -> {

        })
}

from swifter.

BeQ avatar BeQ commented on June 27, 2024

Is it possible to use wildcard/regexp in routes?

Thanks and Happy Holidays!

from swifter.

damian-kolakowski avatar damian-kolakowski commented on June 27, 2024

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

from swifter.

Related Issues (20)

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.