api

package
v0.1.21 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2023 License: MIT Imports: 29 Imported by: 0

Documentation

Index

Constants

View Source
const (
	OpenAPIRouteType = "openapi"
	FixedRouteType   = "fixed"
)

Variables

View Source
var (
	ErrServiceNotFound                  = errors.New("service not found")
	ErrResourceNotFound                 = errors.New("resource not found")
	ErrResourceMethodNotFound           = errors.New("resource method not found")
	ErrOpenAPISpecIsEmpty               = errors.New("OpenAPI spec is empty")
	ErrInvalidHTTPVerb                  = errors.New("invalid HTTP verb")
	ErrInvalidURLResource               = errors.New("invalid URL resource")
	ErrReservedPrefix                   = errors.New("reserved prefix")
	ErrOnlyFixedResourcesAllowedEditing = errors.New("only fixed resources are allowed editing")
	ErrGettingFileFromURL               = errors.New("error getting file from url")
)

Functions

func ComposeFileSavePath

func ComposeFileSavePath(descr *ServiceDescription, paths *config.Paths) string

ComposeFileSavePath composes a save path for a file.

func ComposeOpenAPISavePath

func ComposeOpenAPISavePath(descr *ServiceDescription, baseDir string) string

ComposeOpenAPISavePath composes a save path for an OpenAPI specification. The resulting filename is always index.<spec extension>.

func ConditionalLoggingMiddleware

func ConditionalLoggingMiddleware(cfg *config.Config) func(http.Handler) http.Handler

ConditionalLoggingMiddleware is a middleware that conditionally can disable logger. For example, in tests or when fetching static files.

func GetJSONPayload

func GetJSONPayload[T any](req *http.Request) (*T, error)

GetJSONPayload parses JSON payload from the request body into the given type.

func HandleErrorAndLatency

func HandleErrorAndLatency(svcConfig *config.ServiceConfig, w http.ResponseWriter) bool

HandleErrorAndLatency handles error and latency defined in the service configuration. Returns true if error was handled.

func MustFileStructure

func MustFileStructure(paths *config.Paths) error

MustFileStructure creates the necessary directories and files

Types

type APIResponse

type APIResponse struct {
	*BaseResponse
}

APIResponse is a response type for API responses.

func NewAPIResponse

func NewAPIResponse(w http.ResponseWriter) *APIResponse

NewAPIResponse creates a new APIResponse instance.

func (*APIResponse) Send

func (r *APIResponse) Send(data []byte)

Send sends the data to the client.

func (*APIResponse) WithHeader

func (r *APIResponse) WithHeader(key string, value string) *APIResponse

WithHeader adds a header to the response.

func (*APIResponse) WithStatusCode

func (r *APIResponse) WithStatusCode(code int) *APIResponse

WithStatusCode sets the status code of the response.

type App

type App struct {
	Router *Router
	Paths  *config.Paths
	// contains filtered or unexported fields
}

App is the main application struct

func NewApp

func NewApp(config *config.Config) *App

NewApp creates a new App instance from Config and registers predefined blueprints.

func (*App) AddBluePrint

func (a *App) AddBluePrint(bluePrint RouteRegister) error

AddBluePrint adds a new blueprint to the application.

func (*App) Run

func (a *App) Run()

Run starts the application and the server. Blocks until the server is stopped.

type BaseHandler

type BaseHandler struct {
}

BaseHandler is a base handler type to be embedded in other handlers.

func NewBaseHandler

func NewBaseHandler() *BaseHandler

func (*BaseHandler) Error

func (h *BaseHandler) Error(code int, message string, w http.ResponseWriter)

Error sends an error response.

func (*BaseHandler) JSONResponse

func (h *BaseHandler) JSONResponse(w http.ResponseWriter) *JSONResponse

JSONResponse is a response type for JSON responses.

func (*BaseHandler) Response

func (h *BaseHandler) Response(w http.ResponseWriter) *APIResponse

Response is a response type for API responses.

func (*BaseHandler) Success

func (h *BaseHandler) Success(message string, w http.ResponseWriter)

Success sends a Success response.

type BaseResponse

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

BaseResponse is a base response type.

type BufferedWriter

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

BufferedWriter is a writer that captures the response. Used to capture the template execution result.

func NewBufferedResponseWriter

func NewBufferedResponseWriter() *BufferedWriter

NewBufferedResponseWriter creates a new buffered writer.

func (*BufferedWriter) Header

func (bw *BufferedWriter) Header() http.Header

Header returns the header.

func (*BufferedWriter) Write

func (bw *BufferedWriter) Write(p []byte) (int, error)

Write writes the data to the buffer.

func (*BufferedWriter) WriteHeader

func (bw *BufferedWriter) WriteHeader(statusCode int)

WriteHeader writes the status code.

type ContextHandler

type ContextHandler struct {
	*BaseHandler
	// contains filtered or unexported fields
}

ContextHandler handles context routes.

type ContextListResponse

type ContextListResponse struct {
	Items []string `json:"items"`
}

ContextListResponse is a response for context list.

type GenerateResponse

type GenerateResponse struct {
	Request  *openapi.GeneratedRequest  `json:"request"`
	Response *openapi.GeneratedResponse `json:"response"`
}

type HomeHandler

type HomeHandler struct {
	*BaseHandler
	// contains filtered or unexported fields
}

HomeHandler handles home routes.

type JSONResponse

type JSONResponse struct {
	*BaseResponse
}

JSONResponse is a response type for JSON responses.

func NewJSONResponse

func NewJSONResponse(w http.ResponseWriter) *JSONResponse

NewJSONResponse creates a new JSONResponse instance.

func (*JSONResponse) Send

func (r *JSONResponse) Send(data any)

Send sends the data as JSON to the client. WriteHeader must be called before any writing happens and just once.

func (*JSONResponse) WithHeader

func (r *JSONResponse) WithHeader(key string, value string) *JSONResponse

WithHeader adds a header to the response.

func (*JSONResponse) WithStatusCode

func (r *JSONResponse) WithStatusCode(code int) *JSONResponse

WithStatusCode sets the status code of the response.

type OpenAPIHandler

type OpenAPIHandler struct {
	*BaseHandler
	// contains filtered or unexported fields
}

OpenAPIHandler handles OpenAPI routes serve.

type ResourceGeneratePayload

type ResourceGeneratePayload struct {
	Replacements map[string]any `json:"replacements"`
}

ResourceGeneratePayload is a payload for generating resources. It contains a map of replacements. It is used only with generating resources endpoint. It's merged together with contexts but has higher priority.

type ResourceResponse

type ResourceResponse struct {
	Method      string `json:"method"`
	Path        string `json:"path"`
	Extension   string `json:"extension"`
	ContentType string `json:"contentType"`
	Content     string `json:"content"`
}

type RouteDescription

type RouteDescription struct {
	Method      string                     `json:"method"`
	Path        string                     `json:"path"`
	Type        string                     `json:"type"`
	ContentType string                     `json:"contentType"`
	Overwrites  bool                       `json:"overwrites"`
	File        *connexions.FileProperties `json:"-"`
}

RouteDescription describes a route for the UI Application. Path is relative to the service prefix.

type RouteDescriptions

type RouteDescriptions []*RouteDescription

RouteDescriptions is a slice of RouteDescription. Allows to add custom methods.

func (RouteDescriptions) Sort

func (rs RouteDescriptions) Sort()

Sort sorts the routes by path and method. The order is: GET, POST, other methods (alphabetically)

type RouteRegister

type RouteRegister func(router *Router) error

type Router

type Router struct {
	*chi.Mux

	// Config is a pointer to the global Config instance.
	Config *config.Config
	// contains filtered or unexported fields
}

Router is a wrapper around chi.Mux that adds some extra functionality.

func NewRouter

func NewRouter(config *config.Config) *Router

NewRouter creates a new Router instance from Config.

func (*Router) AddService

func (r *Router) AddService(item *ServiceItem)

func (*Router) GetContexts

func (r *Router) GetContexts() map[string]map[string]any

func (*Router) GetDefaultContexts

func (r *Router) GetDefaultContexts() []map[string]string

func (*Router) GetServices

func (r *Router) GetServices() map[string]*ServiceItem

func (*Router) RemoveContext

func (r *Router) RemoveContext(name string)

RemoveContext removes registered context namespace from the router. Removing it from the service configurations seems not needed at the moment as it won't affect any resolving.

func (*Router) RemoveService

func (r *Router) RemoveService(name string)

func (*Router) SetContexts

func (r *Router) SetContexts(contexts map[string]map[string]any, defaultContexts []map[string]string) *Router

func (*Router) SetServices

func (r *Router) SetServices(services map[string]*ServiceItem) *Router

type SavedResourceResponse

type SavedResourceResponse struct {
	Success bool   `json:"success"`
	Message string `json:"message"`
	ID      int    `json:"id"`
}

type ServiceDescription

type ServiceDescription struct {
	Method    string
	Path      string
	Ext       string
	IsOpenAPI bool
}

ServiceDescription is a struct created from the service payload to facilitate file path composition.

type ServiceEmbedded

type ServiceEmbedded struct {
	Name string `json:"name"`
}

type ServiceHandler

type ServiceHandler struct {
	*BaseHandler
	// contains filtered or unexported fields
}

ServiceHandler handles service routes.

type ServiceItem

type ServiceItem struct {
	Name         string                       `json:"name"`
	Routes       RouteDescriptions            `json:"routes"`
	OpenAPIFiles []*connexions.FileProperties `json:"-"`
	// contains filtered or unexported fields
}

ServiceItem represents a service with the route collection. Service can hold multiple OpenAPI specs.

func (*ServiceItem) AddOpenAPIFile

func (i *ServiceItem) AddOpenAPIFile(file *connexions.FileProperties)

AddOpenAPIFile adds OpenAPI file to the service.

func (*ServiceItem) AddRoutes

func (i *ServiceItem) AddRoutes(routes RouteDescriptions)

AddRoutes adds routes to the service. There's no check for duplicates.

type ServiceItemResponse

type ServiceItemResponse struct {
	Name             string   `json:"name"`
	OpenAPIResources []string `json:"openApiResources"`
}

type ServiceListResponse

type ServiceListResponse struct {
	Items []*ServiceItemResponse `json:"items"`
}

type ServicePayload

type ServicePayload struct {
	IsOpenAPI   bool                     `json:"isOpenApi"`
	Method      string                   `json:"method"`
	Path        string                   `json:"path"`
	Response    []byte                   `json:"response"`
	ContentType string                   `json:"contentType"`
	File        *connexions.UploadedFile `json:"file"`
	URL         string                   `json:"url"`
}

ServicePayload is a struct that represents a new service payload.

type ServiceResourcesResponse

type ServiceResourcesResponse struct {
	Service          *ServiceEmbedded  `json:"service"`
	Endpoints        RouteDescriptions `json:"endpoints"`
	OpenAPISpecNames []string          `json:"openapiSpecNames"`
}

type SettingsHandler

type SettingsHandler struct {
	*BaseHandler
	// contains filtered or unexported fields
}

SettingsHandler handles settings routes.

type SimpleResponse

type SimpleResponse struct {
	Success bool   `json:"success"`
	Message string `json:"message"`
}

SimpleResponse is a simple response type to indicate the success of an operation.

Jump to

Keyboard shortcuts

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