goserver

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2017 License: MIT Imports: 7 Imported by: 2

README

Vardius - goserver

Build Status Coverage Status

The fastest Go Server/API micro framwework, HTTP request router, multiplexer, mux.

ABOUT

Contributors:

Want to contribute ? Feel free to send pull requests!

Have problems, bugs, feature ideas? We are using the github issue tracker to manage them.

HOW TO USE

GoDoc

Basic example

package main

import (
    "fmt"
    "log"
    "net/http"
	
    "github.com/vardius/goserver"
)

func Index(w http.ResponseWriter, r *http.Request, c *goserver.Context) {
    fmt.Fprint(w, "Welcome!\n")
}

func Hello(w http.ResponseWriter, r *http.Request, c *goserver.Context) {
    fmt.Fprintf(w, "hello, %s!\n", c.Params["name"])
}

func main() {
    server := goserver.New()
    server.GET("/", Index)
    server.GET("/hello/:name", Hello)

    log.Fatal(http.ListenAndServe(":8080", server))
}

HTTP2 example

package main

import (
    "fmt"
    "log"
    "net/http"
	
    "golang.org/x/net/http2"
    "github.com/vardius/goserver"
)

func Index(w http.ResponseWriter, r *http.Request, c *goserver.Context) {
    fmt.Fprint(w, "Welcome!\n")
}

func Hello(w http.ResponseWriter, r *http.Request, c *goserver.Context) {
    fmt.Fprintf(w, "hello, %s!\n", c.Params["name"])
}

func main() {
    server := goserver.New()
    server.GET("/", Index)
    server.GET("/hello/:name", Hello)

    http2.ConfigureServer(server, &http2.Server{})
    log.Fatal(server.ListenAndServeTLS("server.crt", "server.key"))
}

Serve files

package main

import (
    "fmt"
    "log"
    "net/http"
	
    "github.com/vardius/goserver"
)

func Index(w http.ResponseWriter, r *http.Request, c *goserver.Context) {
    fmt.Fprint(w, "Welcome!\n")
}

func Hello(w http.ResponseWriter, r *http.Request, c *goserver.Context) {
    fmt.Fprintf(w, "hello, %s!\n", c.Params["name"])
}

func main() {
    server := goserver.New()
    server.GET("/", Index)
    server.GET("/hello/:name", Hello)
	//If route not found and the request method equals Get
	//server will serve files from directory
	//second parameter decide if prefix should be striped
    server.ServeFiles("static", false)

    log.Fatal(http.ListenAndServe(":8080", server))
}

Multi-domain / Sub-domains

// We need an object that implements the http.Handler interface.
// Therefore we need a type for which we implement the ServeHTTP method.
// We just use a map here, in which we map host names (with port) to http.Handlers
type HostSwitch map[string]http.Handler

// Implement the ServerHTTP method on our new type
func (hs HostSwitch) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	// Check if a http.Handler is registered for the given host.
	// If yes, use it to handle the request.
	if handler := hs[r.Host]; handler != nil {
		handler.ServeHTTP(w, r)
	} else {
		// Handle host names for wich no handler is registered
		http.Error(w, "Forbidden", 403) // Or Redirect?
	}
}

func main() {
	// Initialize a server as usual
        server := goserver.New()
	server.GET("/", Index)
	server.GET("/hello/:name", Hello)

	// Make a new HostSwitch and insert the server (our http handler)
	// for example.com and port 12345
	hs := make(HostSwitch)
	hs["example.com:12345"] = server

	// Use the HostSwitch to listen and serve on port 12345
	log.Fatal(http.ListenAndServe(":12345", hs))
}

Basic Authentication

Useing middleware
package main

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

        "github.com/vardius/goserver"
)

type (
	statusError struct {
		code int
		err  error
	}
)

func BasicAuth(w http.ResponseWriter, r *http.Request, c *Context) Error {
	requiredUser := "gordon"
	requiredPassword := "secret!"
	
	// Get the Basic Authentication credentials
	user, password, hasAuth := r.BasicAuth()
	
	if hasAuth && user == requiredUser && password == requiredPassword {
		return nil;
	} else {
		w.Header().Set("WWW-Authenticate", "Basic realm=Restricted")
		return statusError{http.StatusUnauthorized, errors.New(http.StatusUnauthorized)}
	}
}

func Index(w http.ResponseWriter, r *http.Request, c *goserver.Context) {
	fmt.Fprint(w, "Not protected!\n")
}

func Protected(w http.ResponseWriter, r *http.Request, c *goserver.Context) {
	fmt.Fprint(w, "Protected!\n")
}

func main() {
	server := goserver.New()
	server.GET("/", Index)	
	server.GET("/protected", Protected)
	server.Use("/protected", 0, BasicAuth)	

	log.Fatal(http.ListenAndServe(":8080", server))
}
Not useing middleware
package main

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

        "github.com/vardius/goserver"
)

func BasicAuth(h goserver.HandlerFunc, requiredUser, requiredPassword string) goserver.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request, c *goserver.Context) {
		// Get the Basic Authentication credentials
		user, password, hasAuth := r.BasicAuth()

		if hasAuth && user == requiredUser && password == requiredPassword {
			// Delegate request to the given handle
			h(w, r, c)
		} else {
			// Request Basic Authentication otherwise
			w.Header().Set("WWW-Authenticate", "Basic realm=Restricted")
			http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
		}
	}
}

func Index(w http.ResponseWriter, r *http.Request, c *goserver.Context) {
	fmt.Fprint(w, "Not protected!\n")
}

func Protected(w http.ResponseWriter, r *http.Request, c *goserver.Context) {
	fmt.Fprint(w, "Protected!\n")
}

func main() {
	user := "gordon"
	pass := "secret!"

	server := goserver.New()
	server.GET("/", Index)
	server.GET("/protected", BasicAuth(Protected, user, pass))

	log.Fatal(http.ListenAndServe(":8080", server))
}

License

This package is released under the MIT license. See the complete license in the package:

LICENSE

Documentation

Index

Constants

View Source
const (
	GET     = "GET"
	POST    = "POST"
	PUT     = "PUT"
	DELETE  = "DELETE"
	PATCH   = "PATCH"
	OPTIONS = "OPTIONS"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	Params map[string]string
	Route  Route
}

type Error

type Error interface {
	error
	Status() int
}

type HandlerFunc

type HandlerFunc func(http.ResponseWriter, *http.Request, *Context)

type MiddlewareFunc

type MiddlewareFunc func(http.ResponseWriter, *http.Request, *Context) Error

type PanicHandlerFunc

type PanicHandlerFunc func(http.ResponseWriter, *http.Request, interface{})

type Route

type Route interface {
	Path() string
	Regexp() string
	IsRoot() bool
	Parent() Route
	Nodes() map[string]Route
	IsEndPoint() bool
}

type Server

type Server interface {
	POST(path string, f HandlerFunc)
	GET(path string, f HandlerFunc)
	PUT(path string, f HandlerFunc)
	DELETE(path string, f HandlerFunc)
	PATCH(path string, f HandlerFunc)
	OPTIONS(path string, f HandlerFunc)
	Use(path string, priority int, f MiddlewareFunc)
	ServeHTTP(http.ResponseWriter, *http.Request)
	ServeFiles(path string, strip bool)
	NotFound(http.Handler)
	NotAllowed(http.Handler)
	OnPanic(PanicHandlerFunc)
	Routes() map[string]Route
}

func New

func New() Server

Jump to

Keyboard shortcuts

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