sweb

package module
v0.0.0-...-169a3af Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 25, 2015 License: MIT Imports: 0 Imported by: 0

README

sweb

Simple Web Server

Just for in-house usage.

Example main.go

GoDoc

Documentation

Overview

Package sweb provides a in-house go web server skeleton and implementation, supports:

  • Server Graceful shutdown
  • Wrap httprouter for the named parameter support, e.g. "/hello/:name"
  • Middlware, and two basic middleware included: RecoveryWare, StatWare
  • Render for composable html templates
  • Single context object for each request derived from base context
  • Server named routes and reverse helper funcs

Basic Usage:

// init a parent context for the server
ctx := context.WithValue(context.Background(), "dbConn", db)
srv := server.New(ctx, false)

// hook some middlewares
srv.Middleware(server.NewRecoveryWare(false))
srv.Middleware(server.NewStatWare())

// add the routings
srv.Get("/hello/:name", "Hello", HelloHandler)

// Run the server supporting the graceful shutdown by default
srv.Run(":9000")

A sweb handler would be like:

func Hello(ctx context.Context, w http.ResponseWriter, request *http.Request) context.Context {
	name := server.Params(ctx, "name")
	userId := ctx.Value("userId").(int)
	fmt.Fprintf(w, "Hello, %q, userId = %d", name, userId)
	return ctx
}

A basic middleware would be like:

type IncrMiddleware struct {
}

func (m *IncrMiddleware) ServeHTTP(ctx context.Context, w http.ResponseWriter, r *http.Request, next server.Handler) context.Context {
	userId, ok := ctx.Value("userId").(int)
	if !ok {
		userId = 1
	}
	newCtx := context.WithValue(ctx, "userId", userId+1)
	return next(newCtx, w, r)
	// Also can do some hooking staff after the handler processed the request
}

Please checkout the detail godoc for the subpackages:

Directories

Path Synopsis
Package form provides simple helpers for the form validation and query params type casting.
Package form provides simple helpers for the form validation and query params type casting.
Package render provides html/templates rendering for the composable templates which can be called a set of templates.
Package render provides html/templates rendering for the composable templates which can be called a set of templates.
Package server provides basic wrapping for go web server.
Package server provides basic wrapping for go web server.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL