afast

package module
v0.0.0-...-474e199 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2024 License: MIT Imports: 7 Imported by: 0

README

AFast Web Framework

Example

package main

import (
	"fmt"

	"github.com/ahriroot/afast-go"
)

type CustomeMiddleware struct {
}

func (m CustomeMiddleware) Request(req *afast.Request) (*afast.Request, *afast.Response, error) {
	fmt.Printf("Before middleware: %v\n", req)
	return nil, nil, nil
}

func (m CustomeMiddleware) Response(req *afast.Request, res *afast.Response) (*afast.Response, error) {
	fmt.Printf("After middleware: %v\n", req)
	return nil, nil
}

type CustomeView struct {
}

func (v CustomeView) Get(req *afast.Request) (*afast.Response, error) {
	fmt.Printf("Received request2: %v\n", req)
	return &afast.Response{}, nil
}

func Handle(req *afast.Request) (*afast.Response, error) {
	fmt.Printf("Received request: %v\n", req)
	return &afast.Response{}, nil
}

type CustomeWebsocket struct {
}

func (ws CustomeWebsocket) OnError(req *afast.Request, err error) {
	fmt.Printf("Received error: %v\n", err)
}

func (ws CustomeWebsocket) OnConnect(req *afast.Request, sender *afast.AFastWebsocketSender) error {
	fmt.Printf("Received connect: %v\n", req)
	return nil
}

func (ws CustomeWebsocket) OnMessage(req *afast.Request, message string, sender *afast.AFastWebsocketSender) (*string, error) {
	fmt.Printf("Received message: %v\n", message)
	sender.Send("Hello, world!")
	sender.SendJson(map[string]string{"key": "value"})
	return nil, nil
}

func main() {
	app := afast.NewApp()

	app.Get("/a/b/c", Handle, CustomeMiddleware{})
	app.View("/a/b/d", CustomeView{}, false)
	app.Websocket("/ws", CustomeWebsocket{})

	m := app.Map()
	fmt.Println(m)

	app.Listen("localhost:8080")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AFastHandler

type AFastHandler = func(req *Request) (*Response, error)

type AFastMiddleware

type AFastMiddleware interface {
	Request(req *Request) (*Request, *Response, error)
	Response(req *Request, resp *Response) (*Response, error)
}

type AFastView

type AFastView interface {
}

type AFastWebsocket

type AFastWebsocket interface {
	OnError(req *Request, err error)
	OnConnect(req *Request, sender *AFastWebsocketSender) error
	OnMessage(req *Request, message string, sender *AFastWebsocketSender) (*string, error)
}

type AFastWebsocketSender

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

func (*AFastWebsocketSender) Close

func (s *AFastWebsocketSender) Close() error

func (*AFastWebsocketSender) Send

func (s *AFastWebsocketSender) Send(message string) error

func (*AFastWebsocketSender) SendJson

func (s *AFastWebsocketSender) SendJson(data interface{}) error

type Address

type Address = string

type App

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

func NewApp

func NewApp() *App

func (*App) Delete

func (a *App) Delete(path string, handler AFastHandler, middlewares ...AFastMiddleware)

func (*App) Get

func (a *App) Get(path string, handler AFastHandler, middlewares ...AFastMiddleware)

func (*App) Group

func (a *App) Group(path string, middlewares ...AFastMiddleware)

func (*App) Listen

func (a *App) Listen(addr Address)

func (*App) Map

func (a *App) Map() string

func (*App) MapJson

func (a *App) MapJson() string

func (*App) Patch

func (a *App) Patch(path string, handler AFastHandler, middlewares ...AFastMiddleware)

func (*App) Post

func (a *App) Post(path string, handler AFastHandler, middlewares ...AFastMiddleware)

func (*App) Put

func (a *App) Put(path string, handler AFastHandler, middlewares ...AFastMiddleware)

func (*App) Run

func (a *App) Run(options Options)

func (*App) Use

func (a *App) Use(middlewares ...AFastMiddleware)

func (*App) View

func (a *App) View(path string, v AFastView, end bool, middlewares ...AFastMiddleware)

func (*App) Websocket

func (a *App) Websocket(path string, websocket AFastWebsocket, middlewares ...AFastMiddleware)

type Handle

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

func (*Handle) ServeHTTP

func (h *Handle) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Options

type Options struct {
	Address Address
}

type Request

type Request struct {
	Method string
	Path   string
	Params map[string]interface{}
	Query  map[string][]string
}

func NewRequest

func NewRequest(r *http.Request, params *map[string]interface{}) *Request

func (*Request) Param

func (r *Request) Param(name string) string

func (*Request) ParamInt

func (r *Request) ParamInt(name string) int

type Response

type Response struct {
	Status int
}

func (*Response) ToBytes

func (r *Response) ToBytes() []byte

type Router

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

func NewRouter

func NewRouter() *Router

func (*Router) Delete

func (r *Router) Delete(path string, handler AFastHandler, middlewares ...AFastMiddleware)

func (*Router) Get

func (r *Router) Get(path string, handler AFastHandler, middlewares ...AFastMiddleware)

func (*Router) Group

func (r *Router) Group(path string, middlewares ...AFastMiddleware) *Router

func (*Router) Index

func (r *Router) Index(path string, params *map[string]interface{}) (*Router, *map[string]interface{}, error)

func (*Router) Map

func (r *Router) Map() string

func (*Router) MapJson

func (r *Router) MapJson() string

func (*Router) Patch

func (r *Router) Patch(path string, handler AFastHandler, middlewares ...AFastMiddleware)

func (*Router) Post

func (r *Router) Post(path string, handler AFastHandler, middlewares ...AFastMiddleware)

func (*Router) Put

func (r *Router) Put(path string, handler AFastHandler, middlewares ...AFastMiddleware)

func (*Router) Use

func (r *Router) Use(middlewares ...AFastMiddleware)

func (*Router) View

func (r *Router) View(path string, v AFastView, end bool, middlewares ...AFastMiddleware)

func (*Router) Websocket

func (r *Router) Websocket(path string, w AFastWebsocket, middlewares ...AFastMiddleware)

type Server

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

func (*Server) Run

func (s *Server) Run(options Options) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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