• Stars
    star
    349
  • Rank 121,516 (Top 3 %)
  • Language
    Go
  • License
    MIT License
  • Created about 10 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Composable framework for writing HTTP handlers in Go.

siesta

GoDoc build codecov

Siesta is a framework for writing composable HTTP handlers in Go. It supports typed URL parameters, middleware chains, and context passing.

Getting started

Siesta offers a Service type, which is a collection of middleware chains and handlers rooted at a base URI. There is no distinction between a middleware function and a handler function; they are all considered to be handlers and have access to the same arguments.

Siesta accepts many types of handlers. Refer to the GoDoc documentation for Service.Route for more information.

Here is the simple program in the examples directory. It demonstrates the use of a Service, routing, middleware, and a Context.

package main

import (
	"fmt"
	"log"
	"net/http"
	"time"

	"github.com/VividCortex/siesta"
)

func main() {
	// Create a new Service rooted at "/"
	service := siesta.NewService("/")

	// Route accepts normal http.Handlers.
	// The arguments are the method, path, description,
	// and the handler.
	service.Route("GET", "/", "Sends 'Hello, world!'",
		func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintln(w, "Hello, world!")
	})

	// Let's create some simple "middleware."
	// This handler will accept a Context argument and will add the current
	// time to it.
	timestamper := func(c siesta.Context, w http.ResponseWriter, r *http.Request) {
		c.Set("start", time.Now())
	}

	// This is the handler that will actually send data back to the client.
	// It also takes a Context argument so it can get the timestamp from the
	// previous handler.
	timeHandler := func(c siesta.Context, w http.ResponseWriter, r *http.Request) {
		start := c.Get("start").(time.Time)
		delta := time.Now().Sub(start)
		fmt.Fprintf(w, "That took %v.\n", delta)
	}

	// We can compose these handlers together.
	timeHandlers := siesta.Compose(timestamper, timeHandler)

	// Finally, we'll add the new handler we created using composition to a new route.
	service.Route("GET", "/time", "Sends how long it took to send a message", timeHandlers)

	// service is an http.Handler, so we can pass it directly to ListenAndServe.
	log.Fatal(http.ListenAndServe(":8080", service))
}

Siesta also provides utilities to manage URL parameters similar to the flag package. Refer to the params example for a demonstration.

Contributing

We only accept pull requests for minor fixes or improvements. This includes:

  • Small bug fixes
  • Typos
  • Documentation or comments

Please open issues to discuss new features. Pull requests for new features will be rejected, so we recommend forking the repository and making changes in your fork for your use case.

License

Siesta is licensed under the MIT license. The router, which is adapted from httprouter, is licensed separately.

More Repositories

1

go-database-sql-tutorial

A tutorial for Go's database/sql package
CSS
807
star
2

angular-recaptcha

AngularJS directive to add a reCaptcha widget to your form
JavaScript
496
star
3

godaemon

Daemonize Go applications deviously.
Go
494
star
4

ewma

Exponentially Weighted Moving Average algorithms for Go.
Go
435
star
5

johnny-deps

Barebones dependency manager for Go.
Perl
214
star
6

mysqlerr

MySQL Server Error Constants for Golang
Go
195
star
7

ebooks

LaTeX source files for VividCortex's ebooks
TeX
181
star
8

gohistogram

Streaming approximate histograms in Go
Go
174
star
9

robustly

Run functions resiliently in Go, catching and restarting panics
Go
157
star
10

pm

Processlist manager with TCP listener
Go
80
star
11

multitick

A multiplexor for aligned time.Time tickers in Go
Go
70
star
12

trace

Easily trace goroutines at runtime
Go
50
star
13

dbcontrol

A wrapper around Go's database/sql package that provides connection pool limits
Go
29
star
14

lastseen

Last-seen sketch implementation in Go
Go
16
star
15

wlr

Weighted linear regression
Go
15
star
16

approx-queueing-theory

Approximations to the Erlang C queue length formula for M/M/n queues
Go
11
star
17

golibpcap

This is a fork of the https://code.google.com/p/golibpcap/ project.
Go
11
star
18

pm-web

A simple web front-end for pm, a process manager for Go
JavaScript
10
star
19

grunt-circleci

[DEPRECATED] A Grunt plugin for checking build statuses in CircleCI
JavaScript
5
star
20

grafana-datasource

Datasource plugin for connecting your Grafana dashboard to SolarWind's DPM
TypeScript
4
star
21

puppet

Puppet example for the installation of the VividCortex repository
Puppet
2
star
22

hubot-vividcortex

VividCortex hubot script!
CoffeeScript
2
star
23

snappy-go

Clone of the snappy package for Go from code.google.coml (DEPRECATED: we're moving to VividCortex/snappy)
Go
2
star
24

docker

Docker container for VividCortex agents
Dockerfile
1
star