Coder Social home page Coder Social logo

handler's Introduction

handler Build Status GoDoc

http.Handlers for Go

Domains

Handlers are separated into their own domains. They are:

  • log - handlers used for logging purposes
    • Access - logs each request to the provided logger.
    • Panic - catches panics, logs the stack traces to a provided logger, and returns an Internal Server Error. Optionally prints the stack trace in the response body.
  • encoding - handlers dealing with encoding
    • Gzip - compresses the response body
  • lang - handlers for language/translation support
    • I18N - deals with language handling, redirecting to a url with a supported language code. Provides the supported languages and current one in the request context.

Example

Using the I18N handler to process a requested language.

When a user visits '/', a language will be picked from the list of supported ones, based on what the browser has set in the Accepted-Language header, or fall back to the first one in the list. If, for example, the selected language was English, a redirect will be sent to '/en/', where the final handler extracts the selected language from the request context.

package main

import (
	"net/http"

	"golang.org/x/text/language"
	"golang.org/x/text/language/display"

	"github.com/urandom/handler/lang"
)

func main() {
	handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		val := r.Context().Value(lang.ContextKey).(lang.ContextValue)

		w.Write([]byte("Current language: " + display.Self.Name(val.Current)))
	})
  
	http.Handle("/", lang.I18N(handler, lang.I18NOpts{
		Languages: []language.Tag{language.German, language.French, language.English},
	}))
  
	http.ListenAndServe(":8080", nil)
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.