oksana

package module
v0.0.0-...-64dcad2 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2021 License: MIT Imports: 6 Imported by: 0

README

Build Status codecov GoDoc Go Report Card

Oksana

Microservice framework written in Go.

Meaning

Slavic meaning of Oksana "praise to God"

Example

func main() {
    o := oksana.New()

    // index handler
    o.GET("/", func(c oksana.Context) error {
        return c.JSON(200, "Hello World!")
    })

    o.Start()
}

Config

Oksana provides the ability to configure your service from a central config file. Copy .env.example to .env into the source of your service. You can place any environment variables into this file. The .env is loaded into a Config struct which can be read from anywhere in the service.

Routes

Add Routes
// Add GET route
o.GET('/endpoint', handler)

// Add DELETE route
o.DELETE('/endpoint', handler)

// Add PATCH route
o.PATCH('/endpoint', handler)

// Add POST route
o.POST('/endpoint', handler)

// Add PUT route
o.PUT('/endpoint', handler)

Route Groups

// Create Route Group
g := o.Group('/prefix')
g.GET('/endpoint', handler)  // GET /prefix/endpoint
g.POST('/endpoint', handler) // POST /prefix/endpoint
Middleware

Groups can have defined middleware. These middleware handlers will be executed for every route within the group:

g := o.Group('/prefix', middlewareHandler1, middlewareHandler1)

Groups can have middleware handlers that are executed for every route within the group:

g := o.Group('/prefix')
g.Middleware(middlwareHandler1)
g.Middleware(middlwareHandler2)

If you need middleware to be executed on specific routes you can add middleware to the route definition:

g := o.Group('/prefix')
g.GET('/endpoint', handler, middleware)

Development

Oksana uses golang's dep for dependency management. Make sure dep is installed on your local development machine.

To pull in the required dependencies, run the following command: dep ensure.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadConfig

func LoadConfig() map[string]string

LoadConfig reads and returns a .env file as a map

func NotFoundHandler

func NotFoundHandler(context *Context) (err error)

NotFoundHandler default 404 handler for not found routes

Types

type Configuration

type Configuration struct {
	Port string
}

Configuration struct holds values for configuring the application

type Context

type Context struct {
	Request  *http.Request
	Response *Response
	Params   url.Values
}

Context struct

func NewContext

func NewContext() *Context

NewContext creates a new context struct

func (*Context) Code

func (context *Context) Code(code int) (err error)

Code writes header with HTTP code

func (*Context) GetHeader

func (context *Context) GetHeader(header string) string

GetHeader returns specified header

func (*Context) GetParam

func (context *Context) GetParam(param string) string

GetParam return specified paramater

func (*Context) HTTPError

func (context *Context) HTTPError(code int, message string) (err error)

HTTPError returns a text/html error with requested code

func (*Context) HasParam

func (context *Context) HasParam(param string) bool

HasParam checks if param is set

func (*Context) JSON

func (context *Context) JSON(code int, i interface{}) (err error)

JSON returns response as serialized JSON

func (*Context) Redirect

func (context *Context) Redirect(code int, uri string) error

Redirect returns a HTTP code

func (*Context) SetHeader

func (context *Context) SetHeader(k string, v string)

SetHeader adds header to response

func (*Context) SetRequest

func (context *Context) SetRequest(request *http.Request)

SetRequest add request to context field

func (*Context) SetResponse

func (context *Context) SetResponse(writer http.ResponseWriter)

SetResponse add response to context field

func (*Context) String

func (context *Context) String(code int, s string) (err error)

String returns response as a string

type Group

type Group struct {
	Middleware []MiddlewareHandler
	Prefix     string
	Router     Router
}

Group holds information about the route group

func (*Group) DELETE

func (group *Group) DELETE(path string, handler Handler, middleware ...MiddlewareHandler)

DELETE adds a HTTP Get method to the group

func (*Group) GET

func (group *Group) GET(path string, handler Handler, middleware ...MiddlewareHandler)

GET adds a HTTP Get method to the group

func (*Group) OPTIONS

func (group *Group) OPTIONS(path string, handler Handler, middleware ...MiddlewareHandler)

OPTIONS adds a HTTP Get method to the group

func (*Group) PATCH

func (group *Group) PATCH(path string, handler Handler, middleware ...MiddlewareHandler)

PATCH adds a HTTP Get method to the group

func (*Group) POST

func (group *Group) POST(path string, handler Handler, middleware ...MiddlewareHandler)

POST adds a HTTP Get method to the group

func (*Group) PUT

func (group *Group) PUT(path string, handler Handler, middleware ...MiddlewareHandler)

PUT adds a HTTP Get method to the group

type Handler

type Handler func(*Context) error

Handler basic function to router handlers

type MiddlewareHandler

type MiddlewareHandler func(Handler) Handler

MiddlewareHandler defines a function to process middleware

type Oksana

type Oksana struct {
	Context    *Context
	Middleware []MiddlewareHandler
	Router     *Router
}

Oksana struct holds router and context for framework

func New

func New() (oksana *Oksana)

New creates a new service

func (*Oksana) DELETE

func (oksana *Oksana) DELETE(endpoint string, handler Handler, middleware ...MiddlewareHandler)

DELETE adds a HTTP Delete route to router

func (*Oksana) GET

func (oksana *Oksana) GET(endpoint string, handler Handler, middleware ...MiddlewareHandler)

GET adds a HTTP Get route to router

func (*Oksana) GetContext

func (oksana *Oksana) GetContext() *Context

GetContext returns current Context

func (*Oksana) Group

func (oksana *Oksana) Group(prefix string, middleware ...MiddlewareHandler) *Group

Group creates a route group with a common prefix

func (*Oksana) MiddlewareHandler

func (oksana *Oksana) MiddlewareHandler(middleware ...MiddlewareHandler)

MiddlewareHandler adds a middlware handler

func (*Oksana) OPTIONS

func (oksana *Oksana) OPTIONS(endpoint string, handler Handler, middleware ...MiddlewareHandler)

OPTIONS adds a HTTP Get route to router

func (*Oksana) PATCH

func (oksana *Oksana) PATCH(endpoint string, handler Handler, middleware ...MiddlewareHandler)

PATCH adds a HTTP Get route to router

func (*Oksana) POST

func (oksana *Oksana) POST(endpoint string, handler Handler, middleware ...MiddlewareHandler)

POST adds a HTTP Get route to router

func (*Oksana) PUT

func (oksana *Oksana) PUT(endpoint string, handler Handler, middleware ...MiddlewareHandler)

PUT adds a HTTP Get route to router

func (*Oksana) ServeHTTP

func (oksana *Oksana) ServeHTTP(writer http.ResponseWriter, request *http.Request)

func (*Oksana) Start

func (oksana *Oksana) Start(configuration ...Configuration)

Start initates the framework to start listening for requests

type Response

type Response struct {
	Committed bool
	Size      int64
	Status    int
	Writer    http.ResponseWriter
}

Response standard Oksana response struct

func NewResponse

func NewResponse(w http.ResponseWriter) (r *Response)

NewResponse creates new Response struct

func (*Response) Header

func (response *Response) Header() http.Header

Header returns the header information

func (*Response) Write

func (response *Response) Write(b []byte) (n int, err error)

Write writs the bytes (message) to the client

func (*Response) WriteHeader

func (response *Response) WriteHeader(code int)

WriteHeader writes a header to the response writer

type Route

type Route struct {
	Handler    Handler
	Method     string
	Path       string
	Middleware []MiddlewareHandler
}

Route holds all information about a defined route

type Router

type Router struct {
	Routes map[string]Route
}

Router holds all defined routes

func NewRouter

func NewRouter() *Router

NewRouter creates a new router

func (*Router) Add

func (router *Router) Add(method string, path string, handler Handler, middleware []MiddlewareHandler)

Add will add a new route to the Router.Routes map

func (*Router) GetRoute

func (router *Router) GetRoute(context *Context) (Route, bool)

GetRoute will search routes and return Route

Jump to

Keyboard shortcuts

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