Skip to content
altipla-consultingPublic

About

HTTP server for Connect APIs and websites.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

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.

Applications usually expose several APIs with the same CORS policy. Instead of configuring CORS inside each package, create the subrouter once during startup and pass the same *mux.Router to every registration function:

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

api := server.Subrouter()
api.Use(cors.New(doris.ConnectCORS([]string{"https://example.com"})).Handler)
enginedata.RegisterServer(api)
billing.RegisterServer(api)

server.Serve()

Contributing

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

License

MIT License

About

HTTP server for Connect APIs and websites.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages