pete / watts Goto Github PK
View Code? Open in Web Editor NEWResource-oriented, Rack-based, minimalist web framework.
Resource-oriented, Rack-based, minimalist web framework.
# Watts ## Intro Watts is a minimalist, Rack-based, resource-oriented web framework. It has fewer than 400 lines of code (including comments), no dependencies (besides Rack), and only does one thing: it maps resources. See doc/LICENSE for the license. See doc/examples if you're impatient. ## Goals Dead-simple web development. I don't want a framework to take up so much of my brain that there's no space left for my application. ## Resource Orientation If you think of resources as first-class objects, and then think of HTTP methods as operations on those objects, then this should probably make sense. Most of the web frameworks I have seen seem to be HTTP-method-oriented rather than resource-oriented. It seems odd to me (not to mention repetitive) that you would type "A GET on /foo does this, a POST on /foo does that, ..." rather than "There's a resource at /foo. That resource's GET does this, and its POST does that, ...". And because the second one made more sense to me, that's how I wrote Watts. Let me clarify if that was a bit vague, by showing how a Watts app comes into being: * You make a resource by sub-classing `Watts::Resource`. * On this resource, you define some basic operations, in terms of HTTP methods. * You create an app by sub-classing `Watts::App`. * You use the resource() method to tell Watts the path under which the resource can be found. There are a few easy-to-read examples in doc/examples. ## Pattern-matching That resource() method mentioned above? It does some pattern-matching on the different components of the path. They all arrive as strings, of course. The priority for matches is that Watts will attempt to match a string literally if possible. Next it tries to match any regex patterns that have been specified, and failing that, symbols are used as a catch-all. Here are a few patterns and the things they match: * '' matches "/" * 'foo' matches "/foo". * 'foo/bar' matches "/foo/bar". * [] matches "/". * ['foo'] matches "/foo". * ['foo', 'bar'] matches "/foo/bar". * ['foo', /^[0-9+]$/] matches "/foo/", followed by a string of digits. The matching part of the path will be passed as an argument to the method on the resource when it is called. * ['foo', :arg] matches "/foo/" followed by anything. Like with the regex, the argument is passed in. The symbol's actual value doesn't really matter to Watts; it is intended for documentation. ## REST, RFC 2616, TL;DR There's a lot of talk on the internets about what exactly REST is, why it's important, why we're doing it wrong, content-negotiation, discoverability, avoiding out-of-band communication, and all of that stuff. Watts makes it a little easier to comply with the spec than Rails or Sinatra if you know what the spec says, but it doesn't force it on you. (You should definitely care, but Watts won't make you.) ## Design Lots of web frameworks move very quickly, have a large number of features, a large number of bugs resulting from the large number of features, and an onerous upgrade process when updating to a new version. Watts has a much more strict policy: do not add a feature unless it is obviously correct. Except for the earliest versions, every new feature that made it into Watts has been implemented in two or three Watts applications, and looks very different than it would look if it had been implemented based on speculation. (I've been hacking for a long time; this may seem obvious to older hackers, but you have to build something a few times before you understand it well enough to put it into a framework.) If you feel that Watts sorely misses some features, the codebase is very small, and as a result very amenable to extension. ## Bugs I'm sure they're present. You can email me about them, or ping me on GitHub, or send a patch. There do not seem to be too many. There is a test suite. ## About the Name I named it after a character in a video game that I liked as a kid (and still like). It's also the name of a city not far from where I live. Also: joules per second. The Watts in question: ░░░█░░██▒█░░█░░ ███░██░░▓░██░██ ░░█▒██▓█▒▓██▒█░ ██░▓░░▒░▓▒░░▓░█ ░█▒██▓▓█▒▓▓██▒█ █░▓░░▒▒░▓▒▒░░▓░ ░█▒▒▒███▒███▒▒█ █░▓▓▓░░░▓░░░▓▓░ ░░█▒█▒▒▒▒▒▒█▒█░ ██░▓░▓▓▓▓▓▓░▓░█ ░░░█▒██████▒█░░ ███░▓░░░░░░▓░██ ░░█▒██▒██▒██▒█░ ██░▓░░▓░░▓░░▓░█ ░░░███▒██▒███░░ ███░░░▓░░▓░░░██ ░█▒▓█▒████▒█▓█░ █░▓▒░▓░░░░▓░▒░█ █▒▒▓▓█▒▒▒▒█▓██░ ░▓▓▒▒░▓▓▓▓░▒░░█ █▒▒█▓██▒▒█▓█▒▒█ ░▓▓░▒░░▓▓░▒░▓▓░ █▒██▓█▓██▓▓█▒▒█ ░▓░░▒░▒░░▒▒░▓▓░ ░█░██▒████████░ █░█░░▓░░░░░░░░█ ░░░█▓▓▓▓█▓▓█░░░ ███░▒▒▒▒░▒▒░███ ░░░░█▓▓▓███░░░░ ████░▒▒▒░░░████ ░░████████████░ ██░░░░░░░░░░░░█ ## Author Pete Elmore. Feel free to email me using pete at debu dot gs. I'm on GitHub at http://github.com/pete . Also there's http://debu.gs/ , which runs Watts. ### Acknowledgements The commits coming just from me is a bit misleading! Suggestions and tweaks have been provided by friends and coworkers: * John Dewey ( http://bitbucket.org/retr0h ) * Jim Radford ( http://github.com/radford ) * Johnathon Britz ( http://github.com/johnathonbritz ) * Justin George ( http://github.com/jaggederest ) And, as with Hoshi ( http://github.com/pete/hoshi ), I think I'll continue the tradition of crediting the music I was blasting while writing out the first draft: Softball and Dance☆Man.
require 'watts'
class Simple < Watts::App
class EZResource < Watts::Resource
get {
response.headers['Content-Type'] ="text/html;charset=utf-8"
#"Hello, World!\n"
ERB.new(File.read("view/test.erb")).result
}
end
resource('/', EZResource)
end
curl http://localhost:9292/
HTTP/1.1 200 OK
Content-Type: text/plain
Content-Length: 14
I was trying to run Watts with WEBrick instead of Mongrel, but for some reason I couldn't get any routes to work except for '/'.
I tested it out with the matching.rb
example and it had the same issue. Worked fine with Mongrel, didn't work with WEBrick. The only change was Rack::Handler::Mongrel.run
=> Rack::Handler::WEBrick.run
Of course, maybe that's the wrong way to do that, I don't know.
class View < Hoshi::View :html4
def hello
doc {
head { title "Hello, #{title}!" }
body {
h1 "#{username}:Here is your greeting:"
p "Hello, World!"
}
}
render
end
end
Cool project Pete!
Tested with ruby 1.8.7-p72
cd watts
cp doc/examples/hello_world.rb .
gem install metaid rack mongrel
cd lib
ruby hello_world.rb
pointing my browser to http://127.0.0.1:8080/ and I see:
Read error: #<NoMethodError: private method `split' called for 14:Fixnum>
gems:
metaid-1.0
rack-1.2.1
mongrel-1.1.5
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.