All Projects → straightdave → presley

straightdave / presley

Licence: MIT license
Presley - A lightweight web framework for Windows

Programming Languages

powershell
5483 projects

Projects that are alternatives of or similar to presley

Vox
Simple and lightweight Go web framework inspired by koa
Stars: ✭ 74 (+184.62%)
Mutual labels:  sinatra, webframework
gitlab-live
Interactive online shell for GitLab API
Stars: ✭ 21 (-19.23%)
Mutual labels:  sinatra
jpl-space-calendar
An app for parsing and publishing the JPL Space Calendar in JSON and ICalendar formats.
Stars: ✭ 13 (-50%)
Mutual labels:  sinatra
sickbay
Get the HTTP status of a bunch of URLs in a single JSON response. Ideal for monitoring a lot of services at once.
Stars: ✭ 19 (-26.92%)
Mutual labels:  sinatra
mango
mango is a powerful and simple golang web framework
Stars: ✭ 20 (-23.08%)
Mutual labels:  webframework
godzilla
a powerful go web framework
Stars: ✭ 22 (-15.38%)
Mutual labels:  webframework
GCMS
PHP FASTEST CMS with Ajax support
Stars: ✭ 19 (-26.92%)
Mutual labels:  webframework
veterinary-list-api
Veterinary List REST API
Stars: ✭ 18 (-30.77%)
Mutual labels:  sinatra
rack-cargo
🚚 Batch requests for Rack apps (works with Rails, Sinatra, etc)
Stars: ✭ 17 (-34.62%)
Mutual labels:  sinatra
sinatra-bootstrap
My opinionated Sinatra base application
Stars: ✭ 14 (-46.15%)
Mutual labels:  sinatra
example-ruby-sinatra
A simple Ruby app for Deis, the open source PaaS
Stars: ✭ 18 (-30.77%)
Mutual labels:  sinatra
0pdd
Puzzle Driven Development (PDD) Chatbot Assistant for Your GitHub Repositories
Stars: ✭ 108 (+315.38%)
Mutual labels:  sinatra
rubynepal.github.io
Official website of Ruby Nepal
Stars: ✭ 21 (-19.23%)
Mutual labels:  sinatra
webvalve
Betterment's framework for locally developing and testing service-oriented apps in isolation with WebMock and Sinatra-based fakes
Stars: ✭ 111 (+326.92%)
Mutual labels:  sinatra
sinatra-dev-cheatsheet
A quick-and-dirty cheat sheet for creating HTML/CSS websites, and developing using Sinatra and ActiveRecord.
Stars: ✭ 44 (+69.23%)
Mutual labels:  sinatra
fp-sin
Simple Sinatra shell with all the goodies.
Stars: ✭ 13 (-50%)
Mutual labels:  sinatra
nim-servy
Servy is a fast, simple and lightweight micro web-framework for Nim
Stars: ✭ 30 (+15.38%)
Mutual labels:  webframework
tsukuyomi
Asynchronous Web framework for Rust
Stars: ✭ 81 (+211.54%)
Mutual labels:  webframework
pifi-radio
MPD web client to listen to radio, written in React and Sinatra.
Stars: ✭ 36 (+38.46%)
Mutual labels:  sinatra
gitron
A web game using GitHub APIs based on Tron 🥏
Stars: ✭ 20 (-23.08%)
Mutual labels:  sinatra

Presley

Presley is a DSL for quickly creating web applications in PowerShell with minimal effort on Windows:

# myapp.ps1
. .\presley.ps1

get '/' {
  'love me tender'
}

run

And start the application:

PS> .\myapp.ps1

View at: http://localhost:9999/

Screenshot

Todo: Next step is to wrap functionalities into a container (either class or module). Then to make it downloadable from PowerShellGallery

Routes

In Presley, a route is an HTTP method paired with a URL-matching pattern. Each route is associated with a block:

get '/' {
  .. show something ..
}

post '/' {
  .. create something ..
}

put '/' {
  .. replace something ..
}

delete '/' {
  .. annihilate something ..
}

Routes are matched in the order they are defined. The first route that matches the request is invoked.

Route patterns may include named parameters, accessible via the $params hash:

get '/hello/:name' {
  # matches "GET /hello/foo" and "GET /hello/bar"
  # $params['name'] is 'foo' or 'bar'
  "Hello $params['name']"
}

You can also access all parameters via param keyword inside blocks:

get '/hello/:name' {
  param($arg)
  
  # matches "GET /hello/foo" and "GET /hello/bar"
  # $arg['name'] is 'foo' or 'bar'
  "Hello $($arg['name'])!"
}

Routes may also utilize query parameters:

get '/posts' {
  # matches "GET /posts?title=foo&author=bar"
  $title  = $params['title']    # => 'foo'
  $author = $params['author']   # => 'bar'
  
}

$params contains more than query or path variables: form data in POST/PUT requests.
Further more, pre-defined variable $body contains content in POST body.

halt and redirect_to

Built-in function halt can stop processing and respond at once:

get '/halt' {
  halt @{code = 404; body = "you'll never find"}
}

Another built-in function, redirect_to can redirect clients to a relative path:

get '/goto' {
  redirect_to '/'    # => send 302 and Location='/' to clients
}

Return Values

The return value of a route block determines at least the response body passed on to the HTTP client.

Most commonly, this is a string, as in the above examples. But other values are also accepted. You can return any object that would either be converted to a string.

Further more, you can return a hashtable with 'headers (hashtable)', 'code (Int)' and 'body (string)', like:

get '/' {
  @{ 
    headers = @{ 'Content-Type' = 'application/json' };
    code    = 200;
    body    = "{ 'name':'dave', 'age':26 }"
  }
}

In PowerShell, the value of the last statement in a block would be the 'return' value of that block.

Return as JSON

You can use json function to quickly create a response hash for JSON data:

get '/date' {
  json $(Get-Date)
}

It uses ConvertTo-Json cmdlet behind the curtain.

Views / Templates

Each template language is exposed via its own rendering method. These methods simply return a string:

get '/' {
  eps 'index'
}

This renders template/index.eps.

Instead of a template name, you can also just pass in the template content directly:

get '/' {
  $code = "<%= $(Get-Date).ToString('s') %>"
  eps $code
}

This may be not implemented yet.

Available Template Languages

So far only EPS adapter is implemented. You should install EPS before using this way rendering:

PS> Install-Module EPS

Sample EPS Templates

<h1><%= $name %></h1>
<h2><%= $age %></h2>

<ul>
  <% 1..5 | % { %>
  <li><%= $_ %></li>
  <% } %>
</ul>

might output:

<h1>dave</h1>
<h2>26</h2>

<ul>
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  <li>5</li>
</ul>

Accessing Variables in Templates

Templates are NOT evaluated within the same context as route handlers. Instance variables set in route handlers should passed to templates:

get '/:id' {
  $foo = $params['id']
  eps 'products' @{ id = $foo }
}

For more information, please go to project EPS in github

Setting Body, Status Code and Headers

Very similar to Sinatra, other than only returning a string as response body, you can return a hashtable which contains headers, status code and body text:

get '/foo' {
  $response = @{}
  $response["code"] = 233
  $response["headers"] = @{ "Content-Type" = "application/json"; h33 = "2333333"}
  $response["body"] = "[{'name':'dave'}, {'name':'davide'}]"

  $response
}

Or simply:

get '/bar' {
  @{ 
    headers = @{'ContentType' = 'application/json'}
    body    = "{`"msg`":`"a msg`"}"
  }
}

Contribute

Anything is welcomed.

Author: [email protected]

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].