doris

package module
v0.16.1 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: MIT Imports: 30 Imported by: 1

README

doris

Go Reference

HTTP server for Connect APIs and websites.

Install

go get github.com/altipla-consulting/doris

Register services

Each API package receives the Doris server and mounts its service with its own interceptors. MountAPI creates a separate Connect server for that mount. Doris always runs its standard timeout, request trimming, and error reporting interceptors before the interceptors passed to MountAPI, and those extra interceptors apply only to the services registered by the mount function.

For example, after generating Connect v2 bindings and implementing the service and the auth.STS server interceptor:

func RegisterServer(server *doris.Server) {
    doris.MountAPI(server, func(s *connect.Server) {
        engine_dataconnect.RegisterEngineDataServiceHandler(s, NewServer())
    }, auth.STS("engine_data.read"))
}

An API without additional interceptors simply omits the third argument to MountAPI. Application startup calls each package's registration function:

server := doris.NewServer()
enginedata.RegisterServer(server)
server.Serve()

CORS for Connect APIs

Create a subrouter, attach the CORS middleware to it, and pass that subrouter to MountAPI. ConnectCORS provides the allowed methods and headers for Connect; pass the browser origins that should be allowed to call the API.

import "github.com/rs/cors"

func RegisterServer(server *doris.Server) {
    sub := server.Subrouter()
    sub.Use(cors.New(doris.ConnectCORS([]string{"https://example.com"})).Handler)

    doris.MountAPI(sub, func(s *connect.Server) {
        engine_dataconnect.RegisterEngineDataServiceHandler(s, NewServer())
    }, auth.STS("engine_data.read"))
}

The subrouter inherits the server middleware and applies CORS only to the APIs mounted on it. Other routes registered on the server keep their own behavior. Leave the subrouter unrestricted by HTTP method so the CORS middleware can handle browser OPTIONS preflight requests as well as API calls.

Contributing

You can make pull requests or create issues in GitHub. Any code you send should be formatted using make gofmt.

License

MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BearerInterceptor added in v0.14.0

func BearerInterceptor(token string) connect.ClientInterceptor

func ConnectCORS

func ConnectCORS(origins []string) cors.Options

ConnectCORS configures the standard CORS options for Connect APIs.

func Error added in v0.11.1

func Error(w http.ResponseWriter, status int)

func Handler added in v0.11.0

func Handler(handler HandlerError) http.Handler

Handler adapts an error-returning function to an HTTP handler and reports returned errors.

func MountAPI added in v0.16.0

func MountAPI(router apiRoutes, fn MountFn, interceptors ...connect.ServerInterceptor)

MountAPI registers API routes on an application router served by Doris. The standard Doris interceptors run before the supplied interceptors.

func Param added in v0.16.1

func Param(r *http.Request, name string) string

Param returns a matched route parameter, or an empty string if it is missing.

func RequestFromContext added in v0.16.0

func RequestFromContext(ctx context.Context) *http.Request

RequestFromContext returns the request attached by Doris, or nil outside a Doris handler.

func ServerInterceptors

func ServerInterceptors() []connect.ServerInterceptor

Types

type HandlerError added in v0.11.0

type HandlerError func(w http.ResponseWriter, r *http.Request) error

type MountFn added in v0.3.0

type MountFn func(server *connect.Server)

MountFn registers one or more Connect services on a server.

type Option

type Option func(s *Server, sp *ServerPort, internal bool)

Option of a server.

func WithInternal added in v0.8.0

func WithInternal(opts ...Option) Option

WithInternal apply the options to the internal server with metrics and health checks. For example it can be used to change the port of the internal server. It only makes sense if enabled at the server level, not in any individual server port.

func WithListener added in v0.4.0

func WithListener(listener net.Listener) Option

WithListener configures the listener to use for the web server. It is useful to serve in custom configurations like a Unix socket or Tailscale.

func WithPort added in v0.7.0

func WithPort(port string) Option

WithPort changes the default port of the application. If the env variable PORT is defined it will override anything configured here.

type Router added in v0.10.0

type Router struct {
	*mux.Router
}

func (*Router) Delete added in v0.16.0

func (r *Router) Delete(path string, handler HandlerError)

Delete registers a DELETE route.

func (*Router) Get added in v0.16.0

func (r *Router) Get(path string, handler HandlerError)

Get registers a GET route, which also handles HEAD requests.

func (*Router) Handler added in v0.15.0

func (r *Router) Handler(handler http.Handler)

Handler registers an application handler as the fallback for routes not handled by Doris.

func (*Router) Head added in v0.16.0

func (r *Router) Head(path string, handler HandlerError)

Head registers a HEAD route.

func (*Router) Options added in v0.16.0

func (r *Router) Options(path string, handler HandlerError)

Options registers an OPTIONS route.

func (*Router) Patch added in v0.16.0

func (r *Router) Patch(path string, handler HandlerError)

Patch registers a PATCH route.

func (*Router) Post added in v0.16.0

func (r *Router) Post(path string, handler HandlerError)

Post registers a POST route.

func (*Router) Put added in v0.16.0

func (r *Router) Put(path string, handler HandlerError)

Put registers a PUT route.

func (*Router) Subrouter added in v0.16.0

func (r *Router) Subrouter() *mux.Router

Subrouter creates an unrestricted route group that inherits the server middleware.

type Server

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

Server is the root server of the application.

func NewServer

func NewServer(opts ...Option) *Server

NewServer creates a new root server in the default port. It won't start until you call Serve() on it.

func (*Server) Close added in v0.13.7

func (server *Server) Close()

Close gracefully shuts down the server using the same procedure as when receiving a close signal.

func (*Server) Context

func (server *Server) Context() context.Context

Context returns a context that will be canceled when the server is stopped.

func (*Server) GoBackground added in v0.5.0

func (server *Server) GoBackground(fn func(ctx context.Context) error)

GoBackground runs a background goroutine that will be canceled when the server is stopped. The function should return when the context is canceled. If the function returns an error, the server will be stopped prematurely. The server will not exit until all background goroutines have finished.

func (*Server) RegisterPort added in v0.7.0

func (server *Server) RegisterPort(port string, opts ...Option) *ServerPort

Register a new child server in a different port.

func (*Server) Serve

func (server *Server) Serve()

Serve starts the server and blocks until it is stopped with a signal.

type ServerPort added in v0.7.0

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

ServerPort is a child server in a different custom port.

Jump to

Keyboard shortcuts

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