goframe

package module
v0.1.7 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2021 License: MIT Imports: 16 Imported by: 4

README

goframe

Go Report Card

Overview

goframe is a bare bones wrapper based on frame and glues together some useful features from different frameworks to act as a micro HTTP framework.

Install

go get -u github.com/galentuo/goframe

Usage

import "github.com/galentuo/goframe"

Examples

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewApp

func NewApp(name string, strictSlash bool, cr Config) *app

NewApp returns an instance of app. app is where it all happens!

strictSlash defines the trailing slash behavior for new routes. When true, if the route path is "/path/", accessing "/path" will perform a redirect to the former and vice versa. In other words, your application will always see the path as specified in the route.

Config is a nullable field; if null it uses a default configReader = NewConfigReader(app.name, "./configs/", app.name, "_")

func NewConfig added in v0.1.4

func NewConfig(fileName, configPath, envPrefix, envSeparatorChar string) *configReader

NewConfig is used to create a new instance of Config Config reads the configs from config files or env variables.

config := NewConfig(fileName, configPath, envPrefix, envSeparatorChar)

func NewContext

func NewContext(ctx context.Context, ll logger.LogLevel) *defaultContext

NewContext is used to get an instance of the default implementation of goframe.Context

func NewError added in v0.1.5

func NewError(httpCode int, errCode string, errMessage string) *goframeError

NewError is used to define a new goframe error

func NewHTTPService

func NewHTTPService(name string) *httpService

NewHTTPService creates a goframe HTTP Service; where `name` would be the default prefix of the Router.

func NewRouter

func NewRouter(strictSlash bool) *defaultRouter

NewRouter returns an instance of an implementation of Router interface.

func NewServerContext

func NewServerContext(ctx context.Context,
	ll logger.LogLevel,
	res http.ResponseWriter,
	req *http.Request,
) *defaultServerContext

NewServerContext is used to get an instance of the default implementation of goframe.ServerContext

Types

type BackgroundService

type BackgroundService interface {
	Service
	Run() error
}

BackgroundService is a goframe Service used for running background workers.

type Config added in v0.1.4

type Config interface {
	// GetString gets a string config
	GetString(key string) string
	// GetBool gets a boolean config
	GetBool(key string) bool
	// GetInt gets an integer config
	GetInt(key string) int
	// GetStringSlice gets a string slice ([]string) config
	GetStringSlice(key string) []string
	// GetIntSlice gets an integer slice ([]int) config
	GetIntSlice(key string) []int
	// GetStringMap gets a string Map (map[string]interface{}) config
	GetStringMap(key string) map[string]interface{}
}

Config is used to read the configs from config files or env variables.

config := NewConfig(fileName, configPath, envPrefix, envSeparatorChar)

type Context

type Context interface {
	context.Context
	Logger() *logger.Logger
	Set(string, interface{})
	Get(interface{}) interface{}
}

Context is goframe implementation of context. This is to be used across goframe

type HTTPService

type HTTPService interface {
	Service

	// CustomPrefix replaces the default path prefix by the
	// custom one passed in. The routes on the service
	// would have the `Service Name` as a default prefix.
	CustomPrefix(string)

	Route(path, httpMethod string, handler Handler)

	// Use the specified Middleware for the `HTTPService`.
	// The specified middleware will be inherited by any calls
	// that are made on the HTTPService.
	Use(mw ...MiddlewareFunc)
	// Group creates a new `HTTPService` that inherits from it's parent `HTTPService`.
	// This is useful for creating groups of end-points that need to share
	// common functionality, like middleware.
	/*
		g := a.Group()
		g.Use(AuthorizeAPIMiddleware)
	*/
	NewGroup() *httpService
	// contains filtered or unexported methods
}

HTTPService is a goframe Service used for running a http server.

type Handler added in v0.1.5

type Handler func(ServerContext) error

Handler is the basis for a HTTPService Endpoint. A Handler will be given a ServerContext interface that represents the given request/response. It is the responsibility of the Handler to handle the request/response correctly. This could mean rendering a template, JSON, etc... or it could mean returning an error.

func (c ServerContext) error {
	return c.Response().GenericJSON("Hello World!")
}

type MiddlewareFunc

type MiddlewareFunc func(Handler) Handler

MiddlewareFunc defines the interface for a piece of goframe Middleware.

func DoSomething(next Handler) Handler {
	return func(c ServerContext) error {
		// do something before calling the next handler
		err := next(c)
		// do something after call the handler
		return err
	}
}

type MiddlewareStack

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

MiddlewareStack manages the middleware stack for an app/Group.

func (*MiddlewareStack) Use

func (ms *MiddlewareStack) Use(mw ...MiddlewareFunc)

Use the specified Middleware for the `HTTPService`. The specified middleware will be inherited by any calls that are made on the HTTPService.

type ResponseWriter

type ResponseWriter interface {
	SuccessJSON(httpStatusCode int, data interface{}, message string) error
	ErrorJSON(err error) error
	Generic(httpStatusCode int, data []byte) error
}

ResponseWriter interface is used by a goframe Handler to construct an HTTP response.

A ResponseWriter may not be used after the Router.ServeHTTP method has returned.

type Router

type Router interface {
	Handle(method string, path string, handler http.Handler)
	ServeHTTP(w http.ResponseWriter, r *http.Request)
}

Router registers routes to be matched and dispatches a handler.

type ServerContext

type ServerContext interface {
	Context
	// Request returns *http.Request
	Request() *http.Request
	// Response returns goframe.ResponseWriter
	Response() ResponseWriter
	// Params returns all of the parameters for the request,
	// including both named params and query string parameters.
	Params() url.Values
	// Param returns a param, either named or query string,
	// based on the key.
	Param(key string) string
}

ServerContext is a goframe Context with more specific context related to a http server.

type Service

type Service interface {
	// Name is the name of the service;
	// this would be the prefix in case of HTTPService
	Name() string
	// SetInCtx sets required data into the service context;
	// It can be used to pass env elements like connections, configs, etc.
	SetInCtx(key string, value interface{})
	// contains filtered or unexported methods
}

Service is the basic unit of a goframe app. It forms the base of any services like * HTTPService for a http server * BackgroundService for running workers

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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