Ditto Wiki

Silver Bullet Tutorial

Disclaimer: This is not a real tutorial document.

Introduction

Silver Bullet is a general-purpose programming language, which has some specific features for web development like PHP, but doesn't suck.

Features

  • Interpreter written in RPython (JIT'd by PyPy)
  • Built-in web server for testing
  • Static type checking

Quick Start

$ wget http://silverbullet.org/install.sh - | sudo sh
$ echo "return 'Hello!'" > test.sb
$ sb test.sb &
Running test.sb at http://127.0.0.1:8080/
$ curl http://127.0.0.1:8080/
Hello!

Writing Applications

Basics

The simplest form of Silver Bullet application is just returning the content to respond.

return 'Hello!'

This application will generate 200 OK response containing the text "Hello!" in text/html content type.

You can also specify return code and content type by explicitly creating a Response object. The first argument is status code, the second is the content, the third is a dictionary of HTTP headers.

return Response(Response.OK, 'Hello!', {Content-Type: 'text/html'})

Response class has some convenience methods, for example, redirect.

return Response.redirect('http://www.google.com/')

Routing

Most web applications use URL to distinguish which content users want. By defining multiple sub-applications, Silver Bullet server will parse the requested URL and distribute the request into them.

def a @ /a { return 'a' }
def b @ /b { return 'b' }

In the above example, requesting /a will respond with 'a', /b will respond with 'b'.

You can also get parameters from URL. By default, parameters are in string type.

def hello @ /hello/<name> { return name }
맨위로
sb/tutorial.txt · 마지막 수정: 2011/07/21 13:16 작성자 ditto