way

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2024 License: MIT Imports: 19 Imported by: 0

README

Way Framework

Way Mascot

Go Reference

Version: 0.3.0-rc0

Note. Currently there is no stable version of Way during it's inital development. Do not use this project in it's current state, please wait until a version number committed is higher than or equal to 1.0.0

Overview

Way is a lightweight, Go-based web framework that integrates with the Gorilla Mux router and provides simplified mechanisms for handling HTTP requests inspired by the echo frameworkwhile adding database operations.

Features

  • Custom context for HTTP handlers
  • Simplified route declaration (GET, POST, PUT, DELETE, etc.)
  • Integrated SQL database operations
  • Graceful shutdown and startup management

Getting Started

Installation

To start using the Way framework, install it using go get:

go get -u github.com/swayedev/way
Basic Usage

Here's a simple example to get you started:

package main

import (
    "github.com/swayedev/way"
)

func main() {
    w := way.New()

    w.GET("/", func(ctx *way.Context) {
        ctx.Response.Write([]byte("Hello, World!"))
    })

    w.Start(":8080")
}

Or with a handler function

package main

import "github.com/swayedev/way"

func main() {
	w := way.New()
	w.GET("/", helloHandler)

	w.Start(":8080")
}

func helloHandler(c *way.Context) {
	c.Response.Header().Set("Content-Type", "application/json")
	c.Response.Write([]byte("Hello World"))
}

Routing

Way simplifies route handling with predefined methods for standard HTTP verbs:

w.GET("/path", yourGetHandler)
w.POST("/path", yourPostHandler)
// ... and so on for PUT, DELETE, PATCH, OPTIONS, HEAD

Database Operations

Passing an existing sql db

This section explains how to pass an existing SQL database to the application. It provides instructions and guidelines on how to configure the application to use an existing database instead of creating a new one.



Opening a Connection
err := w.SqlOpen()
if err != nil {
    // Handle error
}
Executing Queries
result, err := w.SqlExec(context.Background(), "your SQL query here", args...)
if err != nil {
    // Handle error
}
Querying Data
rows, err := w.SqlQuery(context.Background(), "your SQL query here", args...)
if err != nil {
    // Handle error
}
// Remember to close rows

Graceful Shutdown

To gracefully shut down your server:

err := w.Shutdown(context.Background())
if err != nil {
    // Handle error
}

Contributing

Contributions to the Way framework are welcome! Please read our Contributing Guide for details on our code of conduct and the process for submitting pull requests.

License

This project is licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateEncryptedCookie added in v0.3.1

func CreateEncryptedCookie(
	w http.ResponseWriter,
	secureCookie securecookie.SecureCookie,
	name string,
	value map[string]interface{},
	path string,
	maxAge int,
	httpOnly bool,
	secure bool) (*http.Cookie, error)

func CreateEncryptedCookieWithDefaults added in v0.3.1

func CreateEncryptedCookieWithDefaults(
	w http.ResponseWriter,
	secureCookie securecookie.SecureCookie,
	name string,
	value map[string]interface{}) (*http.Cookie, error)

func GetEnv added in v0.3.1

func GetEnv(key, defaultValue string) string

GetEnv retrieves the environment variable value or a default value if not set

func ReadEncryptedCookie added in v0.3.1

func ReadEncryptedCookie(r *http.Request, secureCookie securecookie.SecureCookie, name string) (map[string]string, error)

Types

type Context

type Context struct {
	Response http.ResponseWriter
	Request  *http.Request

	Session *Session
	Logger  *log.Logger
	// contains filtered or unexported fields
}

Response is the standard go HTTP response writer. Request is the standard go HTTP request. db is the way database connection. Session is the way session.

func NewContext

func NewContext(w http.ResponseWriter, r *http.Request, d *database.DB, s *Session, l *log.Logger) *Context

func (*Context) Data added in v0.1.0

func (c *Context) Data(code int, data []byte)

func (*Context) Decrypt added in v0.2.4

func (c *Context) Decrypt(encrypted string, passphrase string) ([]byte, error)

func (*Context) DeleteCookie added in v0.2.4

func (c *Context) DeleteCookie(name string)

func (*Context) Encrypt added in v0.2.4

func (c *Context) Encrypt(data []byte, passphrase string) (string, error)

func (*Context) GetCookie added in v0.2.4

func (c *Context) GetCookie(name string) (*http.Cookie, error)

func (*Context) GetDB added in v0.2.4

func (c *Context) GetDB() *database.DB

func (*Context) GetSession added in v0.2.4

func (c *Context) GetSession(name string) sessions.Store

func (*Context) HTML added in v0.1.0

func (c *Context) HTML(code int, htmlContent string)

HTML sends an HTML response with the specified status code.

func (*Context) HashByte added in v0.2.4

func (c *Context) HashByte(value []byte) [32]byte

func (*Context) HashString added in v0.2.4

func (c *Context) HashString(value string) [32]byte

func (*Context) HashStringToString added in v0.2.6

func (c *Context) HashStringToString(value string) string

func (*Context) Image added in v0.1.0

func (c *Context) Image(code int, contentType string, imageData []byte)

func (*Context) JSON added in v0.1.0

func (c *Context) JSON(code int, i interface{})

func (*Context) Log added in v0.3.1

func (c *Context) Log() *log.Logger

func (*Context) Parms added in v0.3.1

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

func (*Context) PgxExec added in v0.2.0

func (c *Context) PgxExec(ctx context.Context, query string, args ...interface{}) (pgconn.CommandTag, error)

func (*Context) PgxExecNoResult added in v0.2.0

func (c *Context) PgxExecNoResult(ctx context.Context, query string, args ...interface{}) error

func (*Context) PgxQuery added in v0.2.0

func (c *Context) PgxQuery(ctx context.Context, query string, args ...interface{}) (pgx.Rows, error)

func (*Context) PgxQueryRow added in v0.2.0

func (c *Context) PgxQueryRow(ctx context.Context, query string, args ...interface{}) pgx.Row

func (*Context) ProxyMedia added in v0.2.0

func (c *Context) ProxyMedia(mediaURL string)

func (*Context) Redirect added in v0.1.0

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

func (*Context) SetCookie added in v0.2.4

func (c *Context) SetCookie(cookie *http.Cookie)

func (*Context) SetHeader added in v0.1.0

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

func (*Context) SetSession added in v0.2.4

func (c *Context) SetSession(s *Session)

func (*Context) SqlExec added in v0.1.0

func (c *Context) SqlExec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

func (*Context) SqlExecNoResult added in v0.1.0

func (c *Context) SqlExecNoResult(ctx context.Context, query string, args ...interface{}) error

func (*Context) SqlQuery added in v0.1.0

func (c *Context) SqlQuery(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

func (*Context) SqlQueryRow added in v0.1.0

func (c *Context) SqlQueryRow(ctx context.Context, query string, args ...interface{}) *sql.Row

func (*Context) Status added in v0.1.0

func (c *Context) Status(code int)

func (*Context) String added in v0.2.0

func (c *Context) String(code int, i interface{})

func (*Context) XML added in v0.1.0

func (c *Context) XML(code int, i interface{})

type HandlerFunc

type HandlerFunc func(*Context)

HandlerFunc is a function type that represents a handler for a request. It takes a *Context parameter, which provides information about the request and allows the handler to generate a response.

type MiddlewareFunc

type MiddlewareFunc func(HandlerFunc) HandlerFunc

MiddlewareFunc represents a function that takes a HandlerFunc and returns a modified HandlerFunc.

type Session added in v0.3.1

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

defaultStore is the name of the default session store. defaultCookie is the name of the default session cookie. stores is a map of session stores. cookies is a map of secure cookies.

func NewSession added in v0.3.1

func NewSession() *Session

func (*Session) Cookie added in v0.3.1

func (w *Session) Cookie(name string) *securecookie.SecureCookie

func (*Session) Cookies added in v0.3.1

func (w *Session) Cookies() map[string]*securecookie.SecureCookie

func (*Session) CreateDefaultEncryptedCookie added in v0.3.1

func (w *Session) CreateDefaultEncryptedCookie(
	wr http.ResponseWriter,
	cookieName string,
	value map[string]interface{},
	path string,
	maxAge int,
	httpOnly bool,
	secure bool) (*http.Cookie, error)

func (*Session) CreateDefaultEncryptedCookieWithDefaults added in v0.3.1

func (w *Session) CreateDefaultEncryptedCookieWithDefaults(
	wr http.ResponseWriter,
	cookieName string,
	value map[string]interface{}) (*http.Cookie, error)

func (*Session) CreateEncryptedCookie added in v0.3.1

func (w *Session) CreateEncryptedCookie(
	wr http.ResponseWriter,
	name string,
	cookieName string,
	value map[string]interface{},
	path string,
	maxAge int,
	httpOnly bool,
	secure bool) (*http.Cookie, error)

func (*Session) CreateEncryptedCookieWithDefaults added in v0.3.1

func (w *Session) CreateEncryptedCookieWithDefaults(
	wr http.ResponseWriter,
	name string,
	cookieName string,
	value map[string]interface{}) (*http.Cookie, error)

func (*Session) DefaultCookie added in v0.3.1

func (w *Session) DefaultCookie() *securecookie.SecureCookie

func (*Session) DefaultSession added in v0.3.1

func (w *Session) DefaultSession() sessions.Store

func (*Session) DeleteCookie added in v0.3.1

func (w *Session) DeleteCookie(name string)

func (*Session) DeleteStore added in v0.3.1

func (w *Session) DeleteStore(name string)

func (*Session) ReadDefaultEncryptedCookie added in v0.3.1

func (w *Session) ReadDefaultEncryptedCookie(r *http.Request, name string, cookieName string) (map[string]string, error)

func (*Session) ReadEncryptedCookie added in v0.3.1

func (w *Session) ReadEncryptedCookie(r *http.Request, name string, cookieName string) (map[string]string, error)

func (*Session) SetCookie added in v0.3.1

func (w *Session) SetCookie(name string, s *securecookie.SecureCookie)

func (*Session) SetDefaultCookie added in v0.3.1

func (w *Session) SetDefaultCookie(s *securecookie.SecureCookie)

func (*Session) SetDefaultCookieName added in v0.3.1

func (w *Session) SetDefaultCookieName(name string)

func (*Session) SetDefaultStore added in v0.3.1

func (w *Session) SetDefaultStore(s sessions.Store)

func (*Session) SetDefaultStoreName added in v0.3.1

func (w *Session) SetDefaultStoreName(name string)

func (*Session) SetStore added in v0.3.1

func (w *Session) SetStore(name string, s sessions.Store)

func (*Session) Store added in v0.3.1

func (w *Session) Store(name string) sessions.Store

func (*Session) Stores added in v0.3.1

func (w *Session) Stores() map[string]sessions.Store

type Way

type Way struct {

	// Server is the HTTP server.
	Server *http.Server
	// Listener is the network listener.
	Listener net.Listener
	// Logger is the logger.
	Logger *log.Logger
	// contains filtered or unexported fields
}

func New

func New() *Way

New creates a new instance of Way. It initializes the sessions and sets session defaults if necessary. It returns a pointer to the newly created Way instance.

func (*Way) Close

func (w *Way) Close() error

Close immediately stops the server.

func (*Way) DELETE

func (w *Way) DELETE(path string, handler HandlerFunc)

func (*Way) Db added in v0.2.1

func (w *Way) Db() *database.DB

Db returns the database instance.

func (*Way) GET

func (w *Way) GET(path string, handler HandlerFunc)

HTTP method shortcuts

func (*Way) HEAD

func (w *Way) HEAD(path string, handler HandlerFunc)

func (*Way) HandleFunc

func (w *Way) HandleFunc(path string, handler HandlerFunc)

HandleFunc registers a new route with a matcher for the URL path.

func (*Way) InitDBFromConfig added in v0.3.1

func (w *Way) InitDBFromConfig() error

InitDBFromConfig initializes the database connection from environment variables.

func (*Way) Log added in v0.3.1

func (w *Way) Log() *log.Logger

Log returns the logger.

func (*Way) OPTIONS

func (w *Way) OPTIONS(path string, handler HandlerFunc)

func (*Way) PATCH

func (w *Way) PATCH(path string, handler HandlerFunc)

func (*Way) POST

func (w *Way) POST(path string, handler HandlerFunc)

func (*Way) PUT

func (w *Way) PUT(path string, handler HandlerFunc)

func (*Way) SetDB added in v0.3.1

func (w *Way) SetDB(db *database.DB)

SetDB sets the database connection for the Way object. It takes a pointer to a DB object as a parameter and assigns it to the db field of the Way object.

func (*Way) SetDBConnection added in v0.3.1

func (w *Way) SetDBConnection(db interface{}, driver string)

SetDBConnection sets a new database connection with the given driver.

func (*Way) SetListener added in v0.3.1

func (w *Way) SetListener(listener net.Listener)

SetListener sets the network listener.

func (*Way) SetLogger added in v0.3.1

func (w *Way) SetLogger(logger *log.Logger)

SetLogger sets the logger.

func (*Way) SetRouter added in v0.3.1

func (w *Way) SetRouter(router *mux.Router)

SetRouter sets the HTTP router.

func (*Way) SetServer added in v0.3.1

func (w *Way) SetServer(server *http.Server)

SetServer sets the HTTP server.

func (*Way) SetSession added in v0.3.1

func (w *Way) SetSession(s *Session)

SetSession sets the session.

func (*Way) Shutdown

func (w *Way) Shutdown(ctx context.Context) error

Shutdown stops the server gracefully.

func (*Way) Start

func (w *Way) Start(address string) error

Start starts the server.

func (*Way) Use added in v0.1.0

func (w *Way) Use(middleware ...MiddlewareFunc)

Use adds a middleware to the middleware stack.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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