Documentation
¶
Overview ¶
Package http implements the HTTP server handlers for different resource endpoints
Index ¶
- Constants
- Variables
- func AdminUserNames(ctx context.Context, dbConn *sql.DB) ([]string, error)
- func AdminUsers(ctx context.Context, dbConn *sql.DB) ([]models.User, error)
- func Compress(level int, types ...string) func(next http.Handler) http.Handler
- func ErrorResponse[T any](w http.ResponseWriter, apiErr *APIError, logger *slog.Logger, data []T)
- func Querier[T any](ctx context.Context, dbConn *sql.DB, query Query, logger *slog.Logger) ([]T, error)
- func VerifyOwnership(ctx context.Context, user string, clusterIDs []string, uuids []string, ...) bool
- type APIError
- type CEEMSServer
- type Compressor
- type Config
- type EncoderFunc
- type Query
- type Response
- type WebConfig
Constants ¶
const ( ErrorNone errorType = "" ErrorForbidden errorType = "forbidden" ErrorTimeout errorType = "timeout" ErrorCanceled errorType = "canceled" ErrorExec errorType = "execution" ErrorBadData errorType = "bad_data" ErrorInternal errorType = "internal" ErrorNotFound errorType = "not_found" ErrorNotAcceptable errorType = "not_acceptable" )
List of predefined errors.
Variables ¶
var ( ErrMaxQueryWindow = errors.New("maximum query window exceeded") ErrMalformedTimeStamp = errors.New("malformed timestamp") )
var ( ErrNoUser = errors.New("no user identified") ErrNoPrivs = errors.New("current user does not have admin privileges") ErrInvalidRequest = errors.New("invalid request") ErrInvalidQueryField = errors.New("invalid query fields") ErrMissingData = errors.New("missing data in the request") ErrNoAuth = errors.New("user do not have permissions to view metrics of this job/pod/vm") ErrNoAccess = errors.New("user do not have permissions to access this resource") ErrInvalidClusterID = errors.New("invalid ceems cluster id") )
Custom errors.
Functions ¶
func AdminUserNames ¶
AdminUserNames returns a slice of admin users names.
func AdminUsers ¶
AdminUsers returns a slice of admin users fetched from DB. Errors must always be checked to ensure no row scanning has failed.
func Compress ¶
Compress returns a new handler with compression. Passing a compression level of 5 is sensible value.
func ErrorResponse ¶ added in v0.12.0
ErrorResponse returns error response for by setting errorString and errorType in response.
Types ¶
type APIError ¶ added in v0.12.0
type APIError struct {
Typ errorType
Err error
}
APIError response.
type CEEMSServer ¶
type CEEMSServer struct {
// contains filtered or unexported fields
}
CEEMSServer struct implements HTTP server for stats.
func (*CEEMSServer) Shutdown ¶
func (s *CEEMSServer) Shutdown(ctx context.Context) error
Shutdown server.
func (*CEEMSServer) Start ¶
func (s *CEEMSServer) Start(_ context.Context) error
Start launches CEEMS HTTP server godoc
@title CEEMS API
@version 1.0
@description OpenAPI specification (OAS) for the CEEMS REST API.
@description
@description See the Interactive Docs to try CEEMS API methods without writing code, and get
@description the complete schema of resources exposed by the API.
@description
@description If basic auth is enabled, all the endpoints require authentication.
@description
@description All the endpoints, except `health`, `swagger`, `debug` and `demo`,
@description must send a user-agent header.
@description
@description A demo instance of CEEMS API server is provided for the users to test. This
@description instance is running at `https://ceems-demo.myaddr.tools:7443` and it is the
@description default server that will serve the requests originating from current OAS client.
@description
@description Some of the valid users for this demo instance are:
@description - arnold
@description - betty
@description - edna
@description - gazoo
@description - wilma
@description
@description Every request must contain a `X-Grafana-User` header with one of the usernames
@description above as the value to the header. This is how CEEMS API server recognise the user.
@description
@description Some of the valid projects for this demo instance are:
@description - bedrock
@description - cornerstone
@description
@description Demo instance have CORS enabled to allow cross-domain communication from the browser.
@description All responses have a wildcard same-origin which makes them completely public and
@description accessible to everyone, including any code on any site.
@description
@description To test admin resources, users can use `admin` as `X-Grafana-User`.
@description
@description Timestamps must be specified in milliseconds, unless otherwise specified.
@contact.name Mahendra Paipuri
@contact.url https://github.com/ceems-dev/ceems/issues
@contact.email mahendra.paipuri@gmail.com
@license.name GPL-3.0 license
@license.url https://www.gnu.org/licenses/gpl-3.0.en.html
@securityDefinitions.basic BasicAuth
@host ceems-demo.myaddr.tools:7443
@BasePath /api/v1
@schemes https
@externalDocs.url https://ceems-dev.github.io/ceems/
@x-logo {"url": "https://raw.githubusercontent.com/ceems-dev/ceems/refs/heads/main/website/static/img/logo.png", "altText": "CEEMS logo"}
type Compressor ¶
type Compressor struct {
// contains filtered or unexported fields
}
Compressor represents a set of encoding configurations.
func NewCompressor ¶
func NewCompressor(level int, types ...string) *Compressor
NewCompressor creates a new Compressor that will handle encoding responses.
The level should be one of the ones defined in the flate package. The types are the content types that are allowed to be compressed.
func (*Compressor) Handler ¶
func (c *Compressor) Handler(next http.Handler) http.Handler
Handler returns a new middleware that will compress the response based on the current Compressor.
func (*Compressor) SetEncoder ¶
func (c *Compressor) SetEncoder(encoding string, fn EncoderFunc)
SetEncoder can be used to set the implementation of a compression algorithm.
The encoding should be a standardised identifier. See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding
For example, add the Brotli algorithm:
import brotli_enc "gopkg.in/kothar/brotli-go.v0/enc"
compressor := middleware.NewCompressor(5, "text/html")
compressor.SetEncoder("br", func(w io.Writer, level int) io.Writer {
params := brotli_enc.NewBrotliParams()
params.SetQuality(level)
return brotli_enc.NewBrotliWriter(params, w)
})
type EncoderFunc ¶
An EncoderFunc is a function that wraps the provided io.Writer with a streaming compression algorithm and returns it.
In case of failure, the function should return nil.
type Response ¶
type Response[T any] struct { Status string `json:"status"` Data []T `extensions:"x-nullable,x-omitempty" json:"data"` ErrorType errorType `extensions:"x-nullable,x-omitempty" json:"errorType,omitempty"` Error string `extensions:"x-nullable,x-omitempty" json:"error,omitempty"` Warnings []string `extensions:"x-nullable,x-omitempty" json:"warnings,omitempty"` }
Response defines the response model of CEEMSAPIServer.
type WebConfig ¶
type WebConfig struct {
Addresses []string
WebSystemdSocket bool
WebConfigFile string
LandingConfig *web.LandingConfig
EnableDebugServer bool
UserHeaderNames []string
ExternalURL *url.URL
RoutePrefix string
MaxQueryPeriod model.Duration
RequestsLimit int
CORSOrigin *regexp.Regexp
EnableCompression bool
CompressionLevel int
URL string `yaml:"url"`
HTTPClientConfig config.HTTPClientConfig `yaml:",inline"`
}
WebConfig makes HTTP web config from CLI args.