govel

package module
v0.0.0-...-6f53790 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2023 License: MIT Imports: 22 Imported by: 4

README

Govel

Govel is a lightweight and robust web framework for Go designed to provide only the essential code you need. Please note that this framework is still in development.

Features

  • Minimalistic design: Govel aims to provide a minimalistic codebase, allowing you to add only the features you require for your project.
  • Robustness: Despite its simplicity, Govel is built to be reliable and handle common web application tasks efficiently.
  • Extensible: You can easily extend the framework by adding your own middleware, routing, and other components.

Examples

For usage examples of Govel, please visit our example repository.

We also encourage you to explore the example code to understand how to leverage the framework's capabilities in your own projects.

Documentation

Overview

* This file contains all the functions and structs that are used to add modules to the framework.

  • In this file are all the models and functions for testing.

Index

Constants

View Source
const (
	ContinueRequest = 0

	StopRequest = 1
)

ContinueRequest and StopRequest are aliases in every sense of 0 and 1, used to indicate whether to continue or stop a request.

Variables

View Source
var (

	// Store is the session store.
	Store *sessions.CookieStore
)

Functions

func Delete

func Delete(path string, action routeFunction) *routeModel

func Get

func Get(path string, action routeFunction) *routeModel

func GetKeyFromYAML

func GetKeyFromYAML(key string) interface{}

GetKeyFromYAML returns the value of the key from the YAML config file.

If the key is empty, it returns the whole YAML config file as a map.

func Group

func Group(prefix string, action func()) *groupModel

Group Creates a new group.

func InitModules

func InitModules(m ...initModuleFunc)

InitModules will add the modules to the framework.

func LoadConfigFIle

func LoadConfigFIle(file string)

LoadConfigFile reads the .yaml file and starts the web server based on its configuration.

func Post

func Post(path string, action routeFunction) *routeModel

func Put

func Put(path string, action routeFunction) *routeModel

func Route

func Route(r string, data SMap) string

Gets route's url by its name.

func Set404NotFound

func Set404NotFound(function routeFunction)

Sets a "404 url not found" function.

func SetGlobalMiddlewares

func SetGlobalMiddlewares(function ...middlewareFunction)

Sets global middlewares.

func SetMethodNotAllowed

func SetMethodNotAllowed(function routeFunction)

Sets a general "method not allowed" function.

func SetPanicHandler

func SetPanicHandler(function panicHandler)

Sets a general "handle panic" function.

Types

type Context

type Context struct {
	ResponseWriter http.ResponseWriter

	Request *http.Request

	// SharedPayload shares data between middlewares and routes.
	//
	// SharedPayload gets cleaned up after each request.
	SharedPayload map[string]interface{}

	// Buf is a buffer that is used to store the response body.
	Buf *bytes.Buffer

	// Headers stores the response headers.
	Headers map[string]string
	// contains filtered or unexported fields
}

func (*Context) Bytes

func (c *Context) Bytes(s int, b []byte)

Bytes sends bytes to the response.

func (*Context) ContentType

func (c *Context) ContentType(content string)

ContentType sets the Content-Type header.

func (*Context) GetHeader

func (c *Context) GetHeader(key string) string

GetHeader returns a header by its name.

func (*Context) Json

func (c *Context) Json(s int, j interface{}) error

func (*Context) NewForm

func (c *Context) NewForm() (Form, error)

NewForm creates a new "Form" model for HTTP requests with "application/x-www-form-urlencoded".

func (*Context) NewMultiPartFormDataForm

func (c *Context) NewMultiPartFormDataForm(maxMemory int64) (MultipartFormData, error)

Creates a new "MultipartFormData" model for HTTP requests with "multipart/form-data".

For a detailed explanation of the value of the "maxMemory" variable see https://pkg.go.dev/net/http#Request.ParseMultipartForm.

func (*Context) Param

func (c *Context) Param(key string) string

Param returns a route parameter by its name.

func (*Context) Params

func (c *Context) Params() map[string]string

Params returns all route parameters.

func (*Context) Query

func (c *Context) Query(key string) string

Query returns a query param by its name.

func (*Context) RawJson

func (c *Context) RawJson(j interface{})

func (*Context) Redirect

func (c *Context) Redirect(url string, code int)

Redirect redirects the user to a specific URL.

func (*Context) RedirectToRoute

func (c *Context) RedirectToRoute(name string, params SMap, code int)

RedirectToRoute redirects the user to a specific route.

Route is called internally to get the route.

func (*Context) RemoteAddr

func (c *Context) RemoteAddr() (ip string, port int)

RemoteAddr returns the address of the client.

func (*Context) Session

func (c *Context) Session(session string) (Session, error)

Session always returns a session, even if empty.

The session will not be saved if no changes are made.

func (*Context) SetFormValues

func (c *Context) SetFormValues(ptr interface{}, formValues map[string]interface{})

SetFormValues sets the values of the struct from a map by the "form" tag.

func (*Context) SetHeader

func (c *Context) SetHeader(key string, value string)

SetHeader sets a header by its name.

func (*Context) Status

func (c *Context) Status(s int) *Context

Status sets the status code.

func (*Context) Text

func (c *Context) Text(statusCode int, s string)

Test prints a blank text.

func (*Context) Textf

func (c *Context) Textf(statusCode int, format string, a ...interface{})

Textf Prints a formatted text string.

type Form

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

func (*Form) Get

func (f *Form) Get(value string) string

Gets the first value associated with the given key

func (*Form) GetAll

func (f *Form) GetAll() url.Values

Gets all POST data of the form

func (*Form) Validate

func (f *Form) Validate(rules Map, onError OnError) (data Map, errors map[string]string)

Validate validates a form with the provided rules.

type FormFile

type FormFile struct {
	File       multipart.File
	FileHeader *multipart.FileHeader
}

type Map

type Map map[string]interface{}

Map is an alias for map[string]interface{}.

type MultipartFormData

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

func (*MultipartFormData) Get

func (f *MultipartFormData) Get(value string) string

Gets the first value associated with the given key except files.

func (*MultipartFormData) GetAll

func (f *MultipartFormData) GetAll() interface{}

Gets all POST data of the form except files

func (*MultipartFormData) GetFile

func (f *MultipartFormData) GetFile(value string) (FormFile, error)

Gets the first file associated with the given key.

func (*MultipartFormData) Validate

func (f *MultipartFormData) Validate(rules Map, onError OnError) (data map[string]interface{}, errors map[string]string)

type OnError

type OnError map[string]SMap

OnError is an alias for map[string]SMap.

type SMap

type SMap map[string]string

SMap is an alias for map[string]string.

type Session

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

func (*Session) Delete

func (s *Session) Delete(key string)

Delete removes the value for the given key from the session.

func (*Session) Domain

func (s *Session) Domain(domain string)

Domain sets the domain for the session.

func (*Session) Expire

func (s *Session) Expire()

Expire causes the session to expire.

func (*Session) Get

func (s *Session) Get(key string) interface{}

Get returns the value for the given key from the session.

func (*Session) GetFlash

func (s *Session) GetFlash(key string) interface{}

GetFlash returns the flash value for the given key and deletes it.

func (*Session) IsNew

func (s *Session) IsNew() bool

IsNew returns true if the session is new.

func (*Session) MaxAge

func (s *Session) MaxAge(age int)

MaxAge sets the max age for the session.

func (*Session) Path

func (s *Session) Path(path string)

Path sets the path for the session.

func (*Session) SameSite

func (s *Session) SameSite(sameSite http.SameSite)

SameSite sets the same site flag for the session.

func (*Session) Secure

func (s *Session) Secure(secure bool)

Secure sets the secure flag for the session.

func (*Session) Set

func (s *Session) Set(key string, value interface{})

Set sets the value for the given key in the session.

func (*Session) SetFlash

func (s *Session) SetFlash(key string, value interface{})

SetFlash sets the value for the given key in the session with the "_flash" suffix.

type Test

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

Main struct for testing.

func LoadConfigFileForTests

func LoadConfigFileForTests(routes_func func(), t t, configFilePath string) Test

LoadConfigFileForTests returns a test model for testing.

func (*Test) FromTestingKey

func (t *Test) FromTestingKey(key string) testingValue

FromTestingKey returns the value inside the "key" inside the "testing" key of the .yaml file as a testingValue struct.

func (*Test) NewMultiPartFormData

func (t *Test) NewMultiPartFormData(httpMethod string, url string) multiPartFormDataForTesting

NewMultiPartFormDataForm returns a new multiPartFormDataForTesting for making requests of type multipart/form-data.

func (*Test) NewTestingForm

func (t *Test) NewTestingForm(httpMethod string, to string) (*formForTesting, error)

NewTestingForm returns a new formForTesting for making requests of type application/x-www-form-urlencoded.

func (*Test) TestRoute

func (t *Test) TestRoute(route string) bool

TestRoute makes an HTTP GET request to the route URL (if it exists) and checks if the response status code is 200.

The request will make it to the path http://127.0.0.1:port/path. So the server must be running locally.

func (*Test) UrlOk

func (t *Test) UrlOk(url string) bool

UrlOk makes a GET HTTTP request to url and checks that its status code equals 200.

If the status code is not 200 it will return false.

Jump to

Keyboard shortcuts

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