netserver

package
v0.2.12 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2026 License: MIT Imports: 14 Imported by: 0

README

Net Server

This is a HTTP server structure which is based on the net/http golang package and thus does not depend on any external package.

Usage

To use the netserver, you need to first create an instance of the server like this:

// Set port and blockOnStart params according to your need
s := NewSever(8080, true) 

Then create a controller like this:

// Create a new Controller cl

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

Constants

This section is empty.

Variables

View Source
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

func GetValueFromCtxByStringKey[V any](ctx context.Context, key string, defaultValue V) V

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 (r *Renderer) GetReqCtxValueAsString(rq *http.Request, key string) string

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 (r *Renderer) JsonStructWithSuccess(w http.ResponseWriter, rq *http.Request, httpCode int, structToMarshal interface{})

func (*Renderer) JsonWithFailure

func (r *Renderer) JsonWithFailure(w http.ResponseWriter, rq *http.Request, httpCode int, errorCode string, errorMessage string, devMessage string)

func (*Renderer) JsonWithFailureUsingErrorType

func (r *Renderer) JsonWithFailureUsingErrorType(w http.ResponseWriter, rq *http.Request, errTy appError.Typ)

type Router

type Router struct {
	*http.ServeMux
	// contains filtered or unexported fields
}

func NewRouter

func NewRouter() *Router

func (*Router) Group

func (r *Router) Group(fn func(r *Router))

func (*Router) Handle

func (r *Router) Handle(pattern string, h http.Handler)

func (*Router) HandleFunc

func (r *Router) HandleFunc(pattern string, h http.HandlerFunc)

func (*Router) HasRoutes

func (r *Router) HasRoutes() bool

func (*Router) ServeHTTP

func (r *Router) ServeHTTP(w http.ResponseWriter, rq *http.Request)

func (*Router) Type

func (r *Router) Type() string

func (*Router) Use

func (r *Router) Use(mw ...func(http.Handler) http.Handler)

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.

Jump to

Keyboard shortcuts

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