rum

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2021 License: MIT Imports: 12 Imported by: 3

README

rum

PkgGoDev Build Status codecov Go Report Card LICENSE

Package rum implements an HTTP server. The rum server is compatible with net/http and faster than net/http.

Features

Benchmark

qpsp99

Get started

Install
go get github.com/hslam/rum
Import
import "github.com/hslam/rum"
Usage
Simple Example
package main

import (
	"github.com/hslam/rum"
	"net/http"
)

func main() {
	m := rum.New()
	m.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Hello World"))
	})
	m.Run(":8080")
}

curl http://localhost:8080

Hello World
Example
package main

import (
	"github.com/hslam/rum"
	"net/http"
)

func main() {
	m := rum.New()
	m.SetPoll(true)
	m.Recovery(rum.Recovery)
	m.NotFound(func(w http.ResponseWriter, r *http.Request) {
		http.Error(w, "Not Found : "+r.URL.String(), http.StatusNotFound)
	})
	m.Use(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Access-Control-Allow-Origin", "*")
	})
	m.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Hello World"))
	})
	m.HandleFunc("/hello/:name", func(w http.ResponseWriter, r *http.Request) {
		params := m.Params(r)
		w.Write([]byte("Hello " + params["name"]))
	}).GET()
	m.Group("/group", func(m *rum.Mux) {
		m.HandleFunc("/foo/:id", func(w http.ResponseWriter, r *http.Request) {
			params := m.Params(r)
			w.Write([]byte("group/foo id:" + params["id"]))
		}).GET()
		m.HandleFunc("/bar/:id", func(w http.ResponseWriter, r *http.Request) {
			params := m.Params(r)
			w.Write([]byte("group/bar id:" + params["id"]))
		}).GET()
	})
	m.Run(":8080")
}

curl http://localhost:8080/hello/rum

Hello rum

curl http://localhost:8080/group/foo/1

group/foo id:1

curl http://localhost:8080/group/bar/2

group/bar id:2
Use Other Router Example

The router must implement the http.Handler interface, for example using the http.ServeMux.

package main

import (
	"github.com/hslam/rum"
	"net/http"
)

func main() {
	router := http.NewServeMux()
	router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Hello World"))
	})
	server := rum.New()
	server.Handler = router
	server.Run(":8080")
}
License

This package is licensed under a MIT license (Copyright (c) 2020 Meng Huang)

Author

rum was written by Meng Huang.

Documentation

Overview

Package rum implements an HTTP server.

Index

Constants

This section is empty.

Variables

View Source
var DefaultServer = New()

DefaultServer is the default HTTP server.

View Source
var ErrGroupExisted = errors.New("Group Existed")

ErrGroupExisted is the error returned by Group when registers a existed group.

View Source
var ErrParamsKeyEmpty = errors.New("Params key must be not empty")

ErrParamsKeyEmpty is the error returned by HandleFunc when the params key is empty.

View Source
var RecoveryContextKey = &contextKey{"recovery"}

RecoveryContextKey is a context key.

Functions

func ListenAndServe

func ListenAndServe(addr string, handler http.Handler) error

ListenAndServe listens on the TCP network address addr and then calls Serve with handler to handle requests on incoming connections. Accepted connections are configured to enable TCP keep-alives.

The handler is typically nil, in which case the DefaultServeMux is used.

ListenAndServe always returns a non-nil error.

func ListenAndServeTLS

func ListenAndServeTLS(addr, certFile, keyFile string, handler http.Handler) error

ListenAndServeTLS acts identically to ListenAndServe, except that it expects HTTPS connections. Additionally, files containing a certificate and matching private key for the server must be provided. If the certificate is signed by a certificate authority, the certFile should be the concatenation of the server's certificate, any intermediates, and the CA's certificate.

func Recovery added in v0.0.2

func Recovery(w http.ResponseWriter, r *http.Request)

Recovery returns a recovery handler function that recovers from any panics and writes a 500 status code.

Types

type Entry

type Entry struct {
	// contains filtered or unexported fields
}

Entry represents an HTTP HandlerFunc entry.

func (*Entry) All

func (entry *Entry) All()

All adds all HTTP method to the entry.

func (*Entry) CONNECT

func (entry *Entry) CONNECT() *Entry

CONNECT adds a CONNECT HTTP method to the entry.

func (*Entry) DELETE

func (entry *Entry) DELETE() *Entry

DELETE adds a DELETE HTTP method to the entry.

func (*Entry) GET

func (entry *Entry) GET() *Entry

GET adds a GET HTTP method to the entry.

func (*Entry) HEAD

func (entry *Entry) HEAD() *Entry

HEAD adds a HEAD HTTP method to the entry.

func (*Entry) OPTIONS

func (entry *Entry) OPTIONS() *Entry

OPTIONS adds a OPTIONS HTTP method to the entry.

func (*Entry) PATCH

func (entry *Entry) PATCH() *Entry

PATCH adds a PATCH HTTP method to the entry.

func (*Entry) POST

func (entry *Entry) POST() *Entry

POST adds a POST HTTP method to the entry.

func (*Entry) PUT

func (entry *Entry) PUT() *Entry

PUT adds a PUT HTTP method to the entry.

func (*Entry) TRACE

func (entry *Entry) TRACE() *Entry

TRACE adds a TRACE HTTP method to the entry.

type Mux

type Mux struct {
	// contains filtered or unexported fields
}

Mux is an HTTP request multiplexer.

func NewMux

func NewMux() *Mux

NewMux returns a new Mux.

func (*Mux) Group

func (m *Mux) Group(group string, f func(m *Mux))

Group registers a group with the given pattern to the Mux.

func (*Mux) Handle

func (m *Mux) Handle(pattern string, handler http.Handler) *Entry

Handle registers a handler with the given pattern to the Mux.

func (*Mux) HandleFunc

func (m *Mux) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request)) *Entry

HandleFunc registers a handler function with the given pattern to the Mux.

func (*Mux) NotFound

func (m *Mux) NotFound(handler http.HandlerFunc)

NotFound registers a not found handler function to the Mux.

func (*Mux) Params

func (m *Mux) Params(r *http.Request) map[string]string

Params returns http request params.

func (*Mux) Recovery added in v0.0.2

func (m *Mux) Recovery(handler http.HandlerFunc)

Recovery registers a recovery handler function to the Mux.

func (*Mux) ServeHTTP

func (m *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP dispatches the request to the handler whose pattern most closely matches the request URL.

func (*Mux) Use

func (m *Mux) Use(handler http.HandlerFunc)

Use uses middleware.

type Rum

type Rum struct {
	*Mux
	Handler http.Handler
	// TLSConfig optionally provides a TLS configuration for use
	// by ServeTLS and ListenAndServeTLS. Note that this value is
	// cloned by ServeTLS and ListenAndServeTLS, so it's not
	// possible to modify the configuration with methods like
	// tls.Config.SetSessionTicketKeys. To use
	// SetSessionTicketKeys, use Server.Serve with a TLS Listener
	// instead.
	TLSConfig *tls.Config
	// contains filtered or unexported fields
}

Rum is an HTTP server.

func New

func New() *Rum

New returns a new Rum instance.

func (*Rum) Close

func (m *Rum) Close() error

Close closes the HTTP server.

func (*Rum) Run

func (m *Rum) Run(addr string) error

Run listens on the TCP network address addr and then calls Serve with m to handle requests on incoming connections. Accepted connections are configured to enable TCP keep-alives.

Run always returns a non-nil error.

func (*Rum) RunTLS

func (m *Rum) RunTLS(addr string, certFile, keyFile string) error

RunTLS is like Run but with a cert file and a key file.

func (*Rum) Serve

func (m *Rum) Serve(l net.Listener) error

Serve accepts incoming connections on the Listener l, creating a new service goroutine for each, or registering the conn fd to poll that will trigger the fd to read requests and then call handler to reply to them.

func (*Rum) ServeTLS

func (m *Rum) ServeTLS(l net.Listener, certFile, keyFile string) error

ServeTLS accepts incoming connections on the Listener l, creating a new service goroutine for each. The service goroutines perform TLS setup and then read requests, calling srv.Handler to reply to them.

Files containing a certificate and matching private key for the server must be provided if neither the Server's TLSConfig.Certificates nor TLSConfig.GetCertificate are populated. If the certificate is signed by a certificate authority, the certFile should be the concatenation of the server's certificate, any intermediates, and the CA's certificate.

ServeTLS always returns a non-nil error. After Shutdown or Close, the returned error is ErrServerClosed.

func (*Rum) SetFast

func (m *Rum) SetFast(fast bool)

SetFast enables the Server to use simple request parser.

func (*Rum) SetPoll

func (m *Rum) SetPoll(poll bool)

SetPoll enables the Server to use netpoll based on epoll/kqueue.

Jump to

Keyboard shortcuts

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