rapidroot

package module
v0.0.0-...-27fb756 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2024 License: MIT Imports: 14 Imported by: 0

README

RapidRoot

Overview

RapidRoot is a Go package offering efficient HTTP routing capabilities for web applications. It supports various HTTP methods, dynamic routing, middleware, and more.

Features

  • HTTP Methods: Supports GET, POST, PUT, DELETE, PATCH, OPTIONS, HEAD, CONNECT, TRACE.
  • Middleware: Route-specific and group middleware functionality.
  • Dynamic Routing: Handles dynamic routes with path parameters.
  • Response Utilities: Includes built-in methods for common HTTP responses (JSON, XML, HTML, etc.).
  • Request and Response Wrappers: Enhances functionality and flexibility.
  • Cookie Management: Secure and customizable handling of cookies.
  • Efficient Request Pooling: Reduces garbage collection overhead.

Installation

go get github.com/Folium1/RapidRoot

Basic Usage

Importing the Package
import rr "github.com/Folium1/RapidRoot"
Creating a Router
router := rr.NewRouter()
Defining Routes
router.GET("/path", handlerFunction)
router.POST("/path", handlerFunction)
// Repeat for other HTTP methods
Starting the Server

For HTTP:

router.Run(":8080")

For HTTPS:

router.RunWithTLS(":443", "certFile", "keyFile")
Handler Function
func handlerFunction(req *rr.Request) {
    // Request handling logic here
}

Middleware

func loggingMiddleware(next rapidroot.HandlerFunc) rapidroot.HandlerFunc {
    return func(req *rapidroot.Request) {
        log.Printf("Request received: %s %s", req.Req.Method, req.Req.URL.Path)
        next(req) // Call the next handler
    }
}

Applying Middleware
router.Middleware("GET", "/path", yourMiddlewareFunction)
Group Middleware
router.GroupMiddleware("GET", "/api", middlewareFunction1, middlewareFunction2)

Advanced Features

  • Custom request and response manipulation.
  • Secure and flexible cookie handling.
  • Dynamic routing with easy parameter extraction.

Contributing

Contributions are welcome. Please adhere to Go's standard coding style and submit pull requests for any contributions.

License

RapidRoot is released under the MIT License.


Note: For detailed API documentation and advanced usage, refer to the source code comments

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetOutput

func SetOutput(w io.Writer)

Types

type HandlerFunc

type HandlerFunc func(*Request)

HandlerFunc is a function that can be registered to a router to handle HTTP requests.

type Middleware

type Middleware func(HandlerFunc) HandlerFunc

type Request

type Request struct {
	Writer http.ResponseWriter
	Req    *http.Request
	// contains filtered or unexported fields
}

Request is a struct for handlers, to interact with request.

func (*Request) Abort

func (r *Request) Abort()

Abort aborts request.

func (*Request) BINARY

func (r *Request) BINARY(code int, data []byte)

BINARY response with binary data and provided code.

func (*Request) Cookie

func (r *Request) Cookie(key string) (*http.Cookie, error)

Cookie returns one value from cookies.

func (*Request) Cookies

func (r *Request) Cookies() []*http.Cookie

Cookies returns slice of all cookies.

func (*Request) ERROR

func (r *Request) ERROR(code int, err error)

ERROR return an error with status code.

func (*Request) FILE

func (r *Request) FILE(code int, fileName string)

FILE response with file and provided code.

func (*Request) GetStatus

func (r *Request) GetStatus() int

func (*Request) HTML

func (r *Request) HTML(code int, name string, data any)

HTML parses data to HTML format and sends a response with the provided code. If there is no file with such name, it will abort with a 500 error status code.

func (*Request) HTMLTemplate

func (r *Request) HTMLTemplate(code int, templateName string, tmpl *template.Template, data any)

HTMLTemplate same as HTML, but you can put your html template to execute. You need to set template name to template.Template struct. And then pass it to this function.

Example:

  tmpl, err := template.ParseFiles("main.html", "footer.html")
	if err != nil {
		// handle err
	}

  r.HTMLTemplate(200,"main.html", tmpl, data)

If there is no file with such name, will abort with 500 error status code.

func (*Request) IsAborted

func (r *Request) IsAborted() bool

IsAborted returns true if request is aborted.

func (*Request) JSON

func (r *Request) JSON(code int, data any)

JSON parses data to json format and sends response with a provided code.

func (*Request) PostFormVal

func (r *Request) PostFormVal(key string) string

PostFormVal returns value from post form.

func (*Request) PostFormValues

func (r *Request) PostFormValues() url.Values

PostFormValues returns all values from post form.

func (*Request) QueryValue

func (r *Request) QueryValue(key string) string

QueryValue returns value from query.

func (*Request) QueryValues

func (r *Request) QueryValues() url.Values

QueryValues returns all values from query.

func (*Request) Redirect

func (r *Request) Redirect(code int, url string)

Redirect redirects request to another url. Only codes from 300 to 308 are valid.

func (*Request) RemoveAllCookies

func (r *Request) RemoveAllCookies()

RemoveAllCookies removes all cookies from request.

func (*Request) RemoveAllCookiesExcept

func (r *Request) RemoveAllCookiesExcept(exceptions ...string)

RemoveAllCookiesExcept removes all cookies from the response except the specified ones.

func (*Request) RemoveCookie

func (r *Request) RemoveCookie(key string)

RemoveCookie removes cookie by key.

func (*Request) SetCookie

func (r *Request) SetCookie(key string, val string, exp time.Time)

SetCookie put key-value to cookies.

func (*Request) SetCookieObj

func (r *Request) SetCookieObj(cookie *http.Cookie)

SetCookieObj puts cookie object to cookies.

func (*Request) SetCookiePath

func (r *Request) SetCookiePath(path string)

SetCookiePath sets the path for the cookie.

func (*Request) SetCookieWithOptions

func (r *Request) SetCookieWithOptions(key, val string, exp time.Time)

SetCookieWithOptions puts a key-value pair into the cookies with additional options.

func (*Request) SetCookiesHTTPOnly

func (r *Request) SetCookiesHTTPOnly(httpOnly bool)

SetCookiesHTTPOnly sets httpOnly to all cookies.

func (*Request) SetCookiesSameSite

func (r *Request) SetCookiesSameSite(same http.SameSite)

SetCookiesSameSite sets sameSite to all cookies.

func (*Request) SetCookiesSecure

func (r *Request) SetCookiesSecure(secure bool)

SetCookiesSecure sets secure to all cookies.

func (*Request) SetDefaultCookieOptions

func (r *Request) SetDefaultCookieOptions(options *http.Cookie)

SetDefaultCookieOptions sets default values for cookie attributes.

func (*Request) SetSecureFlagAutomatically

func (r *Request) SetSecureFlagAutomatically()

SetSecureFlagAutomatically sets the Secure flag based on the request's scheme.

func (*Request) SetStatus

func (r *Request) SetStatus(code int)

SetStatus should be used,if you don't use response functions of this package.

func (*Request) SetValue

func (r *Request) SetValue(key string, val any)

SetValue puts key-value to the Request.

func (*Request) Value

func (r *Request) Value(key string) any

Value returns value set to the Request struct.

func (*Request) Values

func (r *Request) Values() map[string]any

Values returns all values set to Request struct.

func (*Request) XML

func (r *Request) XML(code int, data any)

XML parses data to xml format and sends response with a provided code.

func (*Request) XMLIndent

func (r *Request) XMLIndent(code int, data any, prefix, indent string)

XMLIndent parses data to xml format and sends response with a provided code.

type Router

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

func NewRouter

func NewRouter() *Router

NewRouter returns a new router instance with default configuration.

func (*Router) CONNECT

func (r *Router) CONNECT(path string, handler HandlerFunc)

func (*Router) DELETE

func (r *Router) DELETE(path string, handler HandlerFunc)

func (*Router) GET

func (r *Router) GET(path string, handler HandlerFunc)

func (*Router) GroupMiddleware

func (r *Router) GroupMiddleware(method, path string, middleware ...Middleware)

GroupMiddleware adds middleware to the group of routes that have the same path prefix Example:

router := NewRouter()
router.GroupMiddleware(http.MethodGet, "/api", middleware1, middleware2)
router.GET("/api/users", usersHandler)
router.GET("/api/users/:id", userHandler)

// The middleware1 and middleware2 will be applied to both usersHandler and userHandler

func (*Router) HEAD

func (r *Router) HEAD(path string, handler HandlerFunc)

func (*Router) Middleware

func (r *Router) Middleware(method, path string, middleware ...Middleware)

Middleware adds middleware to the route with the specified path Example:

router := NewRouter()
router.Middleware(http.MethodGet, "/api/users", middleware1, middleware2)
router.GET("/api/users", usersHandler)

The middleware1 and middleware2 will be applied to usersHandler

func (*Router) OPTIONS

func (r *Router) OPTIONS(path string, handler HandlerFunc)

func (*Router) PATCH

func (r *Router) PATCH(path string, handler HandlerFunc)

func (*Router) POST

func (r *Router) POST(path string, handler HandlerFunc)

func (*Router) PUT

func (r *Router) PUT(path string, handler HandlerFunc)

func (*Router) Run

func (r *Router) Run(addr string)

Run starts the HTTP server.

func (*Router) RunWithTLS

func (r *Router) RunWithTLS(addr, certFile, keyFile string)

RunWithTLS starts the HTTPS server.

func (*Router) ServeHTTP

func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)

func (*Router) TRACE

func (r *Router) TRACE(path string, handler HandlerFunc)

Jump to

Keyboard shortcuts

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