server

package
v0.0.0-...-499d0cd Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2025 License: MIT Imports: 17 Imported by: 0

README

Go API Server for server

URL Shortener is an API for managing short URLs

Overview

This server was generated by the [openapi-generator] (https://openapi-generator.tech) project. By using the OpenAPI-Spec from a remote server, you can easily generate a server stub.

To see how to make this your own, look here:

README

Running the server

To run the server, follow these simple steps:

go run main.go

The server will be available on http://localhost:8080.

To run the server in a docker container

docker build --network=host -t server .

Once image is built use

docker run --rm -it server

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTypeAssertionError is thrown when type an interface does not match the asserted type
	ErrTypeAssertionError = errors.New("unable to assert type")
)

Functions

func AssertErrorConstraints

func AssertErrorConstraints(obj Error) error

AssertErrorConstraints checks if the values respects the defined constraints

func AssertErrorRequired

func AssertErrorRequired(obj Error) error

AssertErrorRequired checks if the required fields are not zero-ed

func AssertRecurseInterfaceRequired

func AssertRecurseInterfaceRequired[T any](obj interface{}, callback func(T) error) error

AssertRecurseInterfaceRequired recursively checks each struct in a slice against the callback. This method traverse nested slices in a preorder fashion.

func AssertRecurseValueRequired

func AssertRecurseValueRequired[T any](value reflect.Value, callback func(T) error) error

AssertRecurseValueRequired checks each struct in the nested slice against the callback. This method traverse nested slices in a preorder fashion. ErrTypeAssertionError is thrown if the underlying struct does not match type T.

func AssertUrlConstraints

func AssertUrlConstraints(obj Url) error

AssertUrlConstraints checks if the values respects the defined constraints

func AssertUrlRequired

func AssertUrlRequired(obj Url) error

AssertUrlRequired checks if the required fields are not zero-ed

func DefaultErrorHandler

func DefaultErrorHandler(w http.ResponseWriter, _ *http.Request, err error, result *ImplResponse)

DefaultErrorHandler defines the default logic on how to handle errors from the controller. Any errors from parsing request params will return a StatusBadRequest. Otherwise, the error code originating from the servicer will be used.

func EncodeJSONResponse

func EncodeJSONResponse(i interface{}, status *int, headers map[string][]string, w http.ResponseWriter) error

EncodeJSONResponse uses the json encoder to write an interface to the http response with an optional status code

func IsZeroValue

func IsZeroValue(val interface{}) bool

IsZeroValue checks if the val is the zero-ed value.

func Logger

func Logger(inner http.Handler, name string) http.Handler

func NewRouter

func NewRouter(routers ...Router) chi.Router

NewRouter creates a new router for any number of api routers

func ReadFormFileToTempFile

func ReadFormFileToTempFile(r *http.Request, key string) (*os.File, error)

ReadFormFileToTempFile reads file data from a request form and writes it to a temporary file

func ReadFormFilesToTempFiles

func ReadFormFilesToTempFiles(r *http.Request, key string) ([]*os.File, error)

ReadFormFilesToTempFiles reads files array data from a request form and writes it to a temporary files

Types

type Constraint

type Constraint[T Number | string | bool] func(actual T) error

func WithMaximum

func WithMaximum[T Number](expected T) Constraint[T]

func WithMinimum

func WithMinimum[T Number](expected T) Constraint[T]

type Error

type Error struct {
	Code int32 `json:"code"`

	Message string `json:"message"`
}

type ErrorHandler

type ErrorHandler func(w http.ResponseWriter, r *http.Request, err error, result *ImplResponse)

ErrorHandler defines the required method for handling error. You may implement it and inject this into a controller if you would like errors to be handled differently from the DefaultErrorHandler

type ImplResponse

type ImplResponse struct {
	Code    int
	Headers map[string][]string
	Body    interface{}
}

ImplResponse defines an implementation response with error code and the associated body

func Response

func Response(code int, body interface{}) ImplResponse

Response return a ImplResponse struct filled

func ResponseWithHeaders

func ResponseWithHeaders(code int, headers map[string][]string, body interface{}) ImplResponse

ResponseWithHeaders return a ImplResponse struct filled, including headers

type Number

type Number interface {
	~int32 | ~int64 | ~float32 | ~float64
}

type Operation

type Operation[T Number | string | bool] func(actual string) (T, bool, error)

func WithDefaultOrParse

func WithDefaultOrParse[T Number | string | bool](def T, parse ParseString[T]) Operation[T]

func WithParse

func WithParse[T Number | string | bool](parse ParseString[T]) Operation[T]

func WithRequire

func WithRequire[T Number | string | bool](parse ParseString[T]) Operation[T]

type ParseString

type ParseString[T Number | string | bool] func(v string) (T, error)

type ParsingError

type ParsingError struct {
	Param string
	Err   error
}

ParsingError indicates that an error has occurred when parsing request parameters

func (*ParsingError) Error

func (e *ParsingError) Error() string

func (*ParsingError) Unwrap

func (e *ParsingError) Unwrap() error

type RequiredError

type RequiredError struct {
	Field string
}

RequiredError indicates that an error has occurred when parsing request parameters

func (*RequiredError) Error

func (e *RequiredError) Error() string

type Route

type Route struct {
	Method      string
	Pattern     string
	HandlerFunc http.HandlerFunc
}

A Route defines the parameters for an api endpoint

type Router

type Router interface {
	Routes() Routes
}

Router defines the required methods for retrieving api routes

type Routes

type Routes map[string]Route

Routes is a map of defined api endpoints

type URLAPIController

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

URLAPIController binds http requests to an api service and writes the service results to the http response

func NewURLAPIController

func NewURLAPIController(s URLAPIServicer, opts ...URLAPIOption) *URLAPIController

NewURLAPIController creates a default api controller

func (*URLAPIController) CreateUrl

func (c *URLAPIController) CreateUrl(w http.ResponseWriter, r *http.Request)

CreateUrl - Create a new url

func (*URLAPIController) DeleteUrl

func (c *URLAPIController) DeleteUrl(w http.ResponseWriter, r *http.Request)

DeleteUrl - Delete a url

func (*URLAPIController) GetUrl

func (c *URLAPIController) GetUrl(w http.ResponseWriter, r *http.Request)

GetUrl - Get a url

func (*URLAPIController) GetUrlData

func (c *URLAPIController) GetUrlData(w http.ResponseWriter, r *http.Request)

GetUrlData - Returns a url metadata

func (*URLAPIController) Routes

func (c *URLAPIController) Routes() Routes

Routes returns all the api routes for the URLAPIController

type URLAPIOption

type URLAPIOption func(*URLAPIController)

URLAPIOption for how the controller is set up.

func WithURLAPIErrorHandler

func WithURLAPIErrorHandler(h ErrorHandler) URLAPIOption

WithURLAPIErrorHandler inject ErrorHandler into controller

type URLAPIRouter

type URLAPIRouter interface {
	GetUrl(http.ResponseWriter, *http.Request)
	DeleteUrl(http.ResponseWriter, *http.Request)
	GetUrlData(http.ResponseWriter, *http.Request)
	CreateUrl(http.ResponseWriter, *http.Request)
}

URLAPIRouter defines the required methods for binding the api requests to a responses for the URLAPI The URLAPIRouter implementation should parse necessary information from the http request, pass the data to a URLAPIServicer to perform the required actions, then write the service results to the http response.

type URLAPIServicer

type URLAPIServicer interface {
	GetUrl(context.Context, string, string) (ImplResponse, error)
	DeleteUrl(context.Context, string, string) (ImplResponse, error)
	GetUrlData(context.Context, string, string) (ImplResponse, error)
	CreateUrl(context.Context, Url, string) (ImplResponse, error)
}

URLAPIServicer defines the api actions for the URLAPI service This interface intended to stay up to date with the openapi yaml used to generate it, while the service implementation can be ignored with the .openapi-generator-ignore file and updated with the logic required for the API.

type Url

type Url struct {
	Url string `json:"url"`

	Alias string `json:"alias"`

	CreatedAt time.Time `json:"created_at,omitempty"`

	UpdatedAt time.Time `json:"updated_at,omitempty"`

	DeletedAt time.Time `json:"deleted_at,omitempty"`
}

Jump to

Keyboard shortcuts

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