rest

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2025 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Copyright © 2024 Vicknesh Suppramaniam <[email protected]>

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright © 2024 Vicknesh Suppramaniam <[email protected]>

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright © 2024 Vicknesh Suppramaniam <[email protected]>

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright © 2024 Vicknesh Suppramaniam <[email protected]>

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright © 2025 Vicknesh Suppramaniam <[email protected]>

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright © 2024 Vicknesh Suppramaniam <[email protected]>

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright © 2024 Vicknesh Suppramaniam <[email protected]>

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright © 2024 Vicknesh Suppramaniam <[email protected]>

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright © 2024 Vicknesh Suppramaniam <[email protected]>

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright © 2024 Vicknesh Suppramaniam <[email protected]>

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

View Source
const (
	// PatternAll - helper constant for all
	PatternAll = "/*"
	// HealthEndpoint - default endpoint for healthchecks
	HealthEndpoint = "healthcheck"
)
View Source
const (
	// DefaultAddr - listen on all IPv4 and IPv6 interfaces
	DefaultAddr = "[::]"

	// DefaultPort - default port to listen on
	DefaultPort = 8081
)

Variables

This section is empty.

Functions

func HealthCheckHandler

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

HealthCheckHandler - helper function to handle Options method

Types

type CORS

type CORS struct {
	AllowedOrigins   []string
	AllowedMethods   []string
	AllowedHeaders   []string
	MaxAge           int  // maximum number of seconds the results can be cached
	Debug            bool // debug CORS detauls
	AllowCredentials bool // indicates if this REST service should release the response to cross-origin requests (typically JavaScript code); cross-origin are those that don't match the defined origins
	// contains filtered or unexported fields
}

CORS - Cross Origin Resource Sharing configuration

func NewCORS

func NewCORS() (c *CORS)

NewCORS - create new instance for stroring CORS information

func (*CORS) SetCustomHeaders added in v1.2.1

func (c *CORS) SetCustomHeaders(header *Header)

SetCustomHeaders - set custom headers to be returned with CORS request

func (*CORS) SetHeaders

func (c *CORS) SetHeaders(v []string)

SetHeaders - sets HTTP headers allowed for this REST service

func (*CORS) SetMethods

func (c *CORS) SetMethods(v []string)

SetMethods - sets HTTP methods allowed for this REST service

func (*CORS) SetOrigins

func (c *CORS) SetOrigins(v []string)

SetOrigins - sets origins allowed for this REST service

type Config

type Config struct {
	CORS          *CORS
	MaxConcurrent int
	Timeout       time.Duration

	WS *WSConfig
	// contains filtered or unexported fields
}

Config - listener specific configuration

func NewConfig

func NewConfig() (cfg *Config)

NewConfig - creates new instance of config

func (*Config) EnableCompress

func (cfg *Config) EnableCompress(compress bool)

EnableCompress - enable or disable gzip compression

func (*Config) EnableWebSockets added in v1.2.0

func (c *Config) EnableWebSockets(path string, allowedOrigins []string)

func (*Config) MaxBody added in v1.2.0

func (cfg *Config) MaxBody(maxBody int64)

MaxBody - sets maximum body size in MiB to prevent resource drainage

func (*Config) SetCORS

func (cfg *Config) SetCORS(c *CORS)

SetCORS - sets CORS information

func (*Config) SetRouter

func (cfg *Config) SetRouter(router *Router) (err error)

SetRouter - sets the required router with handlers for HTTP requests

type Group

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

func NewGroup

func NewGroup(base string, middlewares ...func(http.Handler) http.Handler) (group *Group)

NewGroup - create new instance of group

func (*Group) Set

func (group *Group) Set(method Method, pattern string, hFn http.HandlerFunc, middlewares ...func(http.Handler) http.Handler) (err error)

Set - sets a handler to a given pattern

type Handler

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

Handler - method, path and function to run

func NewHandler added in v1.2.1

func NewHandler() (handler *Handler)

NewHandler - create new instance of handler

func (*Handler) Set

func (handler *Handler) Set(method Method, pattern string, hFn http.HandlerFunc, middlewares ...func(http.Handler) http.Handler) (err error)

Set - sets a handler to a given pattern

type Header map[string]string

Header - sets custom header to be returned for the request

func NewHeader added in v1.0.3

func NewHeader() (header *Header)

func (*Header) Add added in v1.0.3

func (header *Header) Add(key, value string)

Add - adds a header

func (*Header) Has added in v1.0.3

func (header *Header) Has(key string) (exist bool)

Has - checks if a given header exists

type Listener

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

Listener - implementation of REST listener

func New

func New() (l *Listener)

New - create new instance of the REST listener

func (*Listener) CORS

func (l *Listener) CORS(c *CORS)

func (*Listener) Init

func (l *Listener) Init(logger *slog.Logger, address string, port int, tlsConfig *tls.Config) (err error)

Init - initializes this listener with any necessary configuration parameters

func (*Listener) Name

func (l *Listener) Name() (str string)

Name - returns the name of this listener

func (*Listener) SetConfig

func (l *Listener) SetConfig(config any) (err error)

SetConfig - sets configuration details for this listener

func (*Listener) SetCustomHeaders added in v1.0.3

func (l *Listener) SetCustomHeaders(header *Header)

SetCustomHeaders - sets custom headers to be injected to all requests

func (*Listener) SetWSHandler added in v1.2.0

func (l *Listener) SetWSHandler(h WSHandler)

SetWSHandler - application-level per-connection WebSocket handler

func (*Listener) Start

func (l *Listener) Start() (err error)

Start - starts this listener

func (*Listener) Stop added in v1.2.0

func (l *Listener) Stop(ctx context.Context) error

Optional graceful stop

type Method

type Method uint8
const (
	MethodUnknown Method = iota
	MethodGet
	MethodPost
	MethodPut
	MethodPatch
	MethodDelete
	MethodHead
	MethodOptions
	MethodConnect
)

func (Method) String

func (method Method) String() (str string)

type Router

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

Router - specific path handler

func NewChi

func NewChi(cr *chi.Mux) (router *Router)

NewChi - set raw `chi.Router` for specific need and greater flexibility

func NewRouter

func NewRouter(base string, middlewares ...func(http.Handler) http.Handler) (router *Router)

NewRouter - create new instance of router under the given base pattern

func (*Router) AddGroup added in v1.2.1

func (router *Router) AddGroup(group *Group)

AddGroup - adds given group to the base of the router, with custom middlewares for an entire group (optional) with group middlewares being executed before handler middlewares

func (*Router) AddHealthCheck

func (router *Router) AddHealthCheck(rootPath string, hFn http.HandlerFunc)

AddHealthCheck - creates a healthcheck endpoint at the root of the router using the GET verb, without using `base` but still respecting all middlewares for the router only

func (*Router) SetHandler

func (router *Router) SetHandler(h *Handler)

SetHandler - set given handlers for this router instance

type WSConfig added in v1.2.0

type WSConfig struct {
	Enabled        bool
	Path           string        // e.g. "/ws"
	AllowedOrigins []string      // exact origins; "*" = allow all (not recommended)
	BearerSecret   string        // optional: if set, require Authorization: Bearer <secret>
	IdleTimeout    time.Duration // optional: idle limit for a conn; 0 = disabled
	MaxReadBytes   int64         // per-message read limit; 0 = library default
	WriteTimeout   time.Duration // per write; 0 = no extra timeout
	KeepAlive      time.Duration // ping interval; 0 = disabled
	Backlog        int           // outbound queue per client; drop if full
}

WSConfig - WebSocket settings

type WSHandler added in v1.2.0

type WSHandler func(ctx context.Context, c *websocket.Conn)

WSHandler - app-level handler per accepted connection. Your application sets this; it runs per-connection after a successful upgrade.

type WSHub added in v1.2.0

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

func NewWSHub added in v1.2.0

func NewWSHub(log *slog.Logger, cfg *WSConfig) *WSHub

func (*WSHub) Add added in v1.2.0

func (h *WSHub) Add(c *websocket.Conn) *wsClient

func (*WSHub) BroadcastBinary added in v1.2.0

func (h *WSHub) BroadcastBinary(b []byte)

func (*WSHub) BroadcastText added in v1.2.0

func (h *WSHub) BroadcastText(b []byte)

func (*WSHub) Close added in v1.2.0

func (h *WSHub) Close()

func (*WSHub) Remove added in v1.2.0

func (h *WSHub) Remove(cl *wsClient)

func (*WSHub) Run added in v1.2.0

func (h *WSHub) Run()

Jump to

Keyboard shortcuts

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