Documentation
¶
Overview ¶
Package netserver allows you to get a net/http server up and running with some helper methods available right from the start
Index ¶
- Variables
- func GetValueFromCtx[K comparable, V any](ctx context.Context, key K, defaultValue V) V
- func GetValueFromCtxByStringKey[V any](ctx context.Context, key string, defaultValue V) V
- type NetHttpServer
- type Renderer
- func (r *Renderer) GetReqCtxValueAsString(rq *http.Request, key string) string
- func (r *Renderer) JsonBytesWithSuccess(w http.ResponseWriter, rq *http.Request, httpCode int, jsonBody []byte)
- func (r *Renderer) JsonStringWithSuccess(w http.ResponseWriter, rq *http.Request, httpCode int, jsonBody string)
- func (r *Renderer) JsonStructWithSuccess(w http.ResponseWriter, rq *http.Request, httpCode int, ...)
- func (r *Renderer) JsonWithFailure(w http.ResponseWriter, rq *http.Request, httpCode int, errorCode string, ...)
- func (r *Renderer) JsonWithFailureUsingErrorType(w http.ResponseWriter, rq *http.Request, errTy appError.Typ)
- type Router
- func (r *Router) Group(fn func(r *Router))
- func (r *Router) Handle(pattern string, h http.Handler)
- func (r *Router) HandleFunc(pattern string, h http.HandlerFunc)
- func (r *Router) HasRoutes() bool
- func (r *Router) ServeHTTP(w http.ResponseWriter, rq *http.Request)
- func (r *Router) Type() string
- func (r *Router) Use(mw ...func(http.Handler) http.Handler)
- type SingleMessageResponse
Constants ¶
This section is empty.
Variables ¶
var Middleware *middleware
Functions ¶
func GetValueFromCtx ¶ added in v0.2.5
func GetValueFromCtx[K comparable, V any](ctx context.Context, key K, defaultValue V) V
GetValueFromCtx retrieves a value from context. K is restricted to 'comparable' types (int, string, structs, pointers, etc.). V is the type of the value you want to retrieve.
func GetValueFromCtxByStringKey ¶ added in v0.2.5
GetValueFromCtxByStringKey function forces the 'key' argument to be a string. It still uses 'V any' so you can retrieve an Int, String, or Struct using that string key.
Types ¶
type NetHttpServer ¶
type NetHttpServer struct {
Name string // Name of the server (used to identify it against another one, in case it is needed)
Router *Router // Associated Router
Render *Renderer // Renderer attached to the server. It is here only for ease-of-use
BlockOnStart bool // Should we block on start or not
// contains filtered or unexported fields
}
func NewServer ¶
func NewServer(port uint16, blockOnStart bool) *NetHttpServer
func (*NetHttpServer) PortString ¶
func (s *NetHttpServer) PortString() string
func (*NetHttpServer) Start ¶
func (s *NetHttpServer) Start() appError.Typ
type Renderer ¶
type Renderer struct{}
func (*Renderer) GetReqCtxValueAsString ¶
func (*Renderer) JsonBytesWithSuccess ¶
func (r *Renderer) JsonBytesWithSuccess(w http.ResponseWriter, rq *http.Request, httpCode int, jsonBody []byte)
JsonBytesWithSuccess is supposed to set the response code and []byte body value in the context response
func (*Renderer) JsonStringWithSuccess ¶
func (r *Renderer) JsonStringWithSuccess(w http.ResponseWriter, rq *http.Request, httpCode int, jsonBody string)
JsonStringWithSuccess is supposed to set the response code and string body value in the context response The parameter `jsonBody` is supposed to be a valid json.
func (*Renderer) JsonStructWithSuccess ¶
func (*Renderer) JsonWithFailure ¶
func (*Renderer) JsonWithFailureUsingErrorType ¶
type Router ¶
func (*Router) HandleFunc ¶
func (r *Router) HandleFunc(pattern string, h http.HandlerFunc)
type SingleMessageResponse ¶
type SingleMessageResponse struct {
Message string `json:"message"`
}
SingleMessageResponse is for sending a single message response to the client. Useful when just a single `200 OK` or `201 CREATED` would be ok but you still want to send a message to the client about what happened. e.g. "The blog post was created" or "The upload was successful" etc.