Menu

Tree [a8720f] v1.1.2 /
 History

HTTPS access


File Date Author Commit
 application 2021-12-13 yuyenews yuyenews [a8720f] init commit
 commons 2021-12-09 yuyenews yuyenews [442818] init commit
 example 2021-12-13 yuyenews yuyenews [a8720f] init commit
 network 2021-12-13 yuyenews yuyenews [b78389] init commit
 test 2021-12-05 yuyenews yuyenews [1b62c3] init commit
 .gitignore 2021-11-29 yuyenews yuyenews [017661] init commit
 Beerus.go 2021-12-02 yuyenews yuyenews [642253] init commit
 LICENSE 2021-11-29 yuyenews yuyenews [ac19e4] init commit
 README.md 2021-12-13 yuyenews yuyenews [b78389] init commit
 go.mod 2021-11-30 yuyenews yuyenews [e6aee1] init commit
 push.sh 2021-12-11 yuyenews yuyenews [ec4525] init commit

Read Me

Beerus ·

Beerus is a web framework developed entirely in go,
Based on net/http, it extends the management of routes, adds interceptors, session management,
receiving parameters with struct, parameter validation, etc.
It also provides WebSocket support to upgrade the http protocol to WebSocket and implement communication.

Installation

go get github.com/yuyenews/Beerus

Documentation

https://beeruscc.com/beerus

Examples

HTTP example

Create a function to manage the routing configuration

func CreateRoute() {
    // post route example
    route.POST("/example/post", func (req *commons.BeeRequest, res *commons.BeeResponse) {

        res.SendJson(`{"msg":"SUCCESS"}`)
    })

    // get route example
    route.GET("/example/get", func (req *commons.BeeRequest, res *commons.BeeResponse) {

        res.SendJson(`{"msg":"SUCCESS"}`)
    })
}

Start Service

func main() {
    // Interceptors, routes, etc. Loading of data requires its own calls
    routes.CreateRoute()

    // Listen the service and listen to port 8080
    beerus.ListenHTTP(8080)
}

If you want to put the parameters inside struct and complete the parameter checks

func CreateRoute() {
    // Example of parameter conversion to struct and parameter checksum
    route.POST("/example/post", func (req *commons.BeeRequest, res *commons.BeeResponse) {
        param := DemoParam{}

        // Extraction parameters, Generally used in scenarios where verification is not required or you want to verify manually
        params.ToStruct(req, &param, param)

        // Separate validation of data in struct, this feature can be used independently in any case and is not limited to the routing layer.
        var result = params.Validation(req, &param, param)
        if result != params.SUCCESS {
            res.SendErrorMsg(1128, result)
            return
        }

        // You can also convert + validate the parameters in one step
        // Extraction of parameters + validation
        var result = params.ToStructAndValidation(req, &param, param)
        if result != params.SUCCESS {
            res.SendErrorMsg(1128, result)
            return
        }


        res.SendJson(`{"msg":"SUCCESS"}`)
    })
}

// DemoParam If you have a struct like this, and you want to put all the parameters from the request into this struct
type DemoParam struct {
    // You can customize any field
    // the name of the field must be exactly the same as the name of the requested parameter, and is case-sensitive
    TestStringReception  string  `notnull:"true" msg:"TestStringReception Cannot be empty" routes:"/example/put"`
    TestIntReception     int     `max:"123" min:"32" msg:"TestIntReception The value range must be between 32 - 123" routes:"/example/post"`
    TestUintReception    uint    `max:"123" min:"32" msg:"TestUintReception The value range must be between 32 - 123"`
    TestFloatReception   float32 `max:"123" min:"32" msg:"TestFloatReception The value range must be between 32 - 123"`
    TestBoolReception    bool
    TestStringRegReception string `reg:"^[a-z]+$" msg:"TestStringRegReception Does not meet the regular"`
    TestBeeFileReception commons.BeeFile

    TestJsonReception []string
}

WebSocket example

CreateWebSocketRoute Creating websocket routes

func CreateWebSocketRoute() {
    wroute.AddWebSocketRoute("/ws/test", onConnection, onMessage, onClose)
    wroute.AddWebSocketRoute("/ws/test2", onConnection, onMessage, onClose)
}

// In order to save time, only three functions are used below. In practice, you can configure a set of functions for each wroute

func onConnection(session *params.WebSocketSession, msg string) {
    session.SendString("connection success")
}

func onMessage(session *params.WebSocketSession, msg string) {
    session.SendString("I got the message.")
}

func onClose(session *params.WebSocketSession, msg string) {
    println(msg + "-------------------------------")
}

Start Service

func main() {
    // Interceptors, routes, etc. Loading of data requires its own calls
    routes.CreateRoute()
    routes.CreateWebSocketRoute()

    // Listen the service and listen to port 8080
    beerus.ListenHTTP(8080)
}

Complete sample code

License

Beerus is MIT licensed

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.