ada

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2025 License: MIT Imports: 17 Imported by: 0

README

ada

License Coverage GitHub Workflow Status Go Report Card Go PKG Web

Simple, flexible go web framework.

go get github.com/rakunlabs/ada

Usage

Check out the guide for more details.

package main

import (
	"net/http"

	"github.com/rakunlabs/ada"
)

func main() {
	server := ada.New()
	server.GET("/hello/{user}", SayHello)

	server.Start(":8080")
}

// /////////////////////

func SayHello(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("Hello, " + r.PathValue("user")))
}

Documentation

Index

Constants

View Source
const (
	MIMEApplicationJSON                  = "application/json"
	MIMEApplicationJSONCharsetUTF8       = MIMEApplicationJSON + "; " + charsetUTF8
	MIMEApplicationJavaScript            = "application/javascript"
	MIMEApplicationJavaScriptCharsetUTF8 = MIMEApplicationJavaScript + "; " + charsetUTF8
	MIMEApplicationXML                   = "application/xml"
	MIMEApplicationXMLCharsetUTF8        = MIMEApplicationXML + "; " + charsetUTF8
	MIMETextXML                          = "text/xml"
	MIMETextXMLCharsetUTF8               = MIMETextXML + "; " + charsetUTF8
	MIMEApplicationForm                  = "application/x-www-form-urlencoded"
	MIMEApplicationProtobuf              = "application/protobuf"
	MIMEApplicationMsgpack               = "application/msgpack"
	MIMETextHTML                         = "text/html"
	MIMETextHTMLCharsetUTF8              = MIMETextHTML + "; " + charsetUTF8
	MIMETextPlain                        = "text/plain"
	MIMETextPlainCharsetUTF8             = MIMETextPlain + "; " + charsetUTF8
	MIMEMultipartForm                    = "multipart/form-data"
	MIMEMultipartMixed                   = "multipart/mixed"
	MIMEOctetStream                      = "application/octet-stream"
)
View Source
const (
	HeaderContentType        = "Content-Type"
	HeaderContentDisposition = "Content-Disposition"
	HeaderXRequestID         = "X-Request-Id"
	HeaderVary               = "Vary"
	HeaderOrigin             = "Origin"
	HeaderLocation           = "Location"

	HeaderAccessControlAllowOrigin      = "Access-Control-Allow-Origin"
	HeaderAccessControlAllowCredentials = "Access-Control-Allow-Credentials"
	HeaderAccessControlExposeHeaders    = "Access-Control-Expose-Headers"
	HeaderAccessControlRequestMethod    = "Access-Control-Request-Method"
	HeaderAccessControlRequestHeaders   = "Access-Control-Request-Headers"
	HeaderAccessControlAllowMethods     = "Access-Control-Allow-Methods"
	HeaderAccessControlAllowHeaders     = "Access-Control-Allow-Headers"
	HeaderAccessControlMaxAge           = "Access-Control-Max-Age"
)

Variables

View Source
var (
	DefaultShutdownTimeout   = 10 * time.Second
	DefaultReadHeaderTimeout = 10 * time.Second
	ErrAlreadyStarted        = errors.New("server started already")
	ErrListen                = errors.New("listen")

	ListenerAddrContextKey = "listener_addr"
)
View Source
var DefaultErrHandler = func(c *Context, err error) {
	var errResp *HandlerError
	if errors.As(err, &errResp) {
		c.SetStatus(errResp.Code).SendJSON(errResp)
		return
	}

	c.SetStatus(http.StatusInternalServerError).SendJSON(map[string]string{"message": err.Error()})
}

Functions

func Chain added in v0.1.1

func Chain(middlewares ...func(next http.Handler) http.Handler) func(next http.Handler) http.Handler

Chain is a utility function to chain multiple middleware functions together.

Types

type Context added in v0.1.1

type Context struct {
	Request  *http.Request
	Response http.ResponseWriter
	// contains filtered or unexported fields
}

func NewContext added in v0.1.1

func NewContext(w http.ResponseWriter, r *http.Request) *Context

func (*Context) Bind added in v0.1.11

func (c *Context) Bind(obj any) error

Bind binds the request data to the provided struct based on content type and struct tags.

  • The obj parameter must be a pointer.

func (*Context) Err added in v0.1.11

func (c *Context) Err(err error) error

func (*Context) SendBlob added in v0.2.0

func (c *Context) SendBlob(reader io.Reader) error

SendBlob streams data from an io.Reader to the response.

  • The caller is responsible for setting appropriate headers (e.g., Content-Type).

func (*Context) SendFile added in v0.2.0

func (c *Context) SendFile(name string, reader io.Reader) error

SendFile sends a single file to the client.

func (*Context) SendJSON added in v0.1.9

func (c *Context) SendJSON(data any) error

SendJSON sends a json response.

func (*Context) SendJSONP added in v0.1.9

func (c *Context) SendJSONP(data any, indent string) error

SendJSONP sends a json pretty-printed response.

func (*Context) SendJSONRaw added in v0.1.11

func (c *Context) SendJSONRaw(data io.Reader) error

func (*Context) SendNoContent added in v0.1.9

func (c *Context) SendNoContent() error

SendNoContent always sends a 204 No Content response without body.

func (*Context) SendString added in v0.1.11

func (c *Context) SendString(s string) error

func (*Context) SendZip added in v0.2.0

func (c *Context) SendZip(name string, files map[string]io.Reader) error

SendZip sends files to the client as a zip file.

  • If name is empty, defaults to "files.zip".

func (*Context) SetHeader added in v0.1.9

func (c *Context) SetHeader(kv ...string) *Context

SetHeader sets a response header.

SetHeader("Content-Type", "application/json", "X-Custom-Header", "value")

func (*Context) SetStatus added in v0.1.9

func (c *Context) SetStatus(code int) *Context

SetStatus sets the response status code.

  • Use http package constants for standard status codes like http.StatusOK

type HandlerError added in v0.1.11

type HandlerError struct {
	Code int   `json:"-"`
	Err  error `json:"-"`
}

func (HandlerError) Error added in v0.1.11

func (e HandlerError) Error() string

func (HandlerError) MarshalJSON added in v0.1.11

func (e HandlerError) MarshalJSON() ([]byte, error)

func (HandlerError) Unwrap added in v0.1.11

func (e HandlerError) Unwrap() error

type HandlerFunc added in v0.1.9

type HandlerFunc func(c *Context) error

type Logger added in v0.1.1

type Logger interface {
	Error(msg string, keysAndValues ...any)
	Info(msg string, keysAndValues ...any)
	Debug(msg string, keysAndValues ...any)
	Warn(msg string, keysAndValues ...any)
}

type Mux added in v0.1.1

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

func NewMux added in v0.1.1

func NewMux() *Mux

func (*Mux) CONNECT added in v0.1.1

func (m *Mux) CONNECT(path string, handler http.HandlerFunc, middlewares ...func(next http.Handler) http.Handler)

func (*Mux) DELETE added in v0.1.1

func (m *Mux) DELETE(path string, handler http.HandlerFunc, middlewares ...func(next http.Handler) http.Handler)

func (*Mux) ErrorHandler added in v0.1.9

func (m *Mux) ErrorHandler(handler func(c *Context, err error))

ErrorHandler sets the handler for 500 Internal Server Error responses.

  • If not set, it defaults to a generic error handler.
  • Only usable for ada.HandlerFunc handlers.

func (*Mux) GET added in v0.1.1

func (m *Mux) GET(path string, handler http.HandlerFunc, middlewares ...func(next http.Handler) http.Handler)

func (Mux) Group added in v0.1.1

func (m Mux) Group(pathGroup string, middlewares ...func(next http.Handler) http.Handler) *Mux

func (*Mux) HEAD added in v0.1.1

func (m *Mux) HEAD(path string, handler http.HandlerFunc, middlewares ...func(next http.Handler) http.Handler)

func (*Mux) Handle added in v0.1.1

func (m *Mux) Handle(path string, handler http.Handler, middlewares ...func(next http.Handler) http.Handler)

func (*Mux) HandleFunc added in v0.1.1

func (m *Mux) HandleFunc(path string, handler http.HandlerFunc, middlewares ...func(next http.Handler) http.Handler)

func (*Mux) HandleFuncWildcard added in v0.2.0

func (m *Mux) HandleFuncWildcard(path string, handler http.HandlerFunc, middlewares ...func(next http.Handler) http.Handler)

HandleFuncWildcard is registering all paths under the given path.

func (*Mux) HandleWildcard added in v0.2.0

func (m *Mux) HandleWildcard(path string, handler http.Handler, middlewares ...func(next http.Handler) http.Handler)

HandleWildcard is registering all paths under the given path.

func (*Mux) HandleWithMethod added in v0.1.2

func (m *Mux) HandleWithMethod(method, path string, handler http.HandlerFunc, middlewares ...func(next http.Handler) http.Handler)

func (*Mux) NotFound added in v0.1.1

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

NotFound sets the handler for 404 Not Found responses.

  • If not set, it defaults to http.NotFound.

func (*Mux) OPTIONS added in v0.1.1

func (m *Mux) OPTIONS(path string, handler http.HandlerFunc, middlewares ...func(next http.Handler) http.Handler)

func (*Mux) PATCH added in v0.1.1

func (m *Mux) PATCH(path string, handler http.HandlerFunc, middlewares ...func(next http.Handler) http.Handler)

func (*Mux) POST added in v0.1.1

func (m *Mux) POST(path string, handler http.HandlerFunc, middlewares ...func(next http.Handler) http.Handler)

func (*Mux) PUT added in v0.1.1

func (m *Mux) PUT(path string, handler http.HandlerFunc, middlewares ...func(next http.Handler) http.Handler)

func (*Mux) ServeHTTP added in v0.1.1

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

ServeHTTP implements the http.Handler interface for Mux.

func (*Mux) TRACE added in v0.1.1

func (m *Mux) TRACE(path string, handler http.HandlerFunc, middlewares ...func(next http.Handler) http.Handler)

func (*Mux) Use added in v0.1.1

func (m *Mux) Use(middlewares ...func(next http.Handler) http.Handler)

func (*Mux) Wrap added in v0.1.9

func (m *Mux) Wrap(handler HandlerFunc) func(http.ResponseWriter, *http.Request)

Wrap converts ada.HandlerFunc to http.HandlerFunc.

type Option

type Option func(*option)

func WithLogger

func WithLogger(logger Logger) Option

func WithShutdownTimeout added in v0.1.1

func WithShutdownTimeout(timeout time.Duration) Option

WithShutdownTimeout sets the shutdown timeout, default is 10 seconds.

type OptionStart added in v0.1.7

type OptionStart func(*optionStart)

func WithBaseContext added in v0.1.8

func WithBaseContext(ctx context.Context) OptionStart

WithBaseContext sets the base context, default is context.Background().

  • Default is context.Background().

func WithContext added in v0.1.8

func WithContext(ctx context.Context) OptionStart

WithContext sets the context, usable for stopping the server.

  • Same as StartWithContext's ctx

func WithHTTPServerFunc added in v0.1.8

func WithHTTPServerFunc(fn func(server *http.Server) *http.Server) OptionStart

func WithNetwork added in v0.1.1

func WithNetwork(network string) OptionStart

WithNetwork sets the network, default is "tcp".

func WithReadHeaderTimeout added in v0.1.11

func WithReadHeaderTimeout(d time.Duration) OptionStart

WithReadHeaderTimeout sets the ReadHeaderTimeout for the http.Server.

  • Default is 5 seconds.

type Server

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

func New

func New(opts ...Option) *Server

func NewWithFunc added in v0.1.1

func NewWithFunc(ctx context.Context, fn func(ctx context.Context, mux *Mux) error, opts ...Option) (*Server, error)

func (*Server) Start

func (s *Server) Start(addr string, opts ...OptionStart) error

Start starts the server with the given address.

  • If the server fails to start, an error will be returned.

func (*Server) StartWithContext added in v0.1.1

func (s *Server) StartWithContext(ctx context.Context, addr string, opts ...OptionStart) error

StartWithContext starts the server with the given context and address.

  • If the context is canceled, the server will be stopped.
  • If the server fails to start, an error will be returned.

func (*Server) Stop

func (s *Server) Stop() error

Directories

Path Synopsis
handler
swagger module
middleware
cors module
folder module
log module
recover module
requestid module
telemetry module
utils

Jump to

Keyboard shortcuts

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