Nigel  Uys

Nigel Uys

1654358400

Bxog: A Simple and Fast HTTP Router for Go (HTTP Request Multiplexer)

Bxog is a simple and fast HTTP router for Go (HTTP request multiplexer).

Usage

An example of using the multiplexer:

package main

import (
    "io"
    "net/http"

    bx "github.com/claygod/Bxog"
)

// Handlers
func IHandler(w http.ResponseWriter, req *http.Request, r *bx.Router) {
    io.WriteString(w, "Welcome to Bxog!")
}
func THandler(w http.ResponseWriter, req *http.Request, r *bx.Router) {
    params := r.Params(req, "/abc/:par")
    io.WriteString(w, "Params:\n")
    io.WriteString(w, " 'par' -> "+params["par"]+"\n")
}
func PHandler(w http.ResponseWriter, req *http.Request, r *bx.Router) {
    // Getting parameters from URL
    params := r.Params(req, "country")
    io.WriteString(w, "Country:\n")
    io.WriteString(w, " 'name' -> "+params["name"]+"\n")
    io.WriteString(w, " 'capital' -> "+params["city"]+"\n")
    io.WriteString(w, " 'valuta' -> "+params["money"]+"\n")
    // Creating an URL string
    io.WriteString(w, "Creating an URL from the current route (This is an example of creating an another URL):\n")
    io.WriteString(w, r.Create("country", map[string]string{"name": "Russia", "city": "Moscow", "money": "rouble"}))
}

// Main
func main() {
    m := bx.New()
    m.Add("/", IHandler)
    m.Add("/abc/:par", THandler)
    m.Add("/country/:name/capital/:city/valuta/:money", PHandler).
        Id("country"). // For a convinience you can indicate a short ID
        Method("GET")  // It is not necessary to indicate the GET method here as the GET method is used by default but this way is used to set an allowed method
    m.Test()
    m.Start(":9999")
}

Click URLs:

Settings

Necessary changes in the configuration of the multiplexer can be made in the configuration file config.go

Perfomance

Bxog is the fastest router, showing the speed of query processing. Its speed is comparable to the speed of the popular multiplexers: Bone, Httprouter, Gorilla, Zeus. The test is done on a computer with a i3-6320 3.7GHz processor and 8 GB RAM. In short (less time, the better):

  • Bxog 163 ns/op
  • HttpRouter 183 ns/op
  • Zeus 12302 ns/op
  • GorillaMux 14928 ns/op
  • GorillaPat 618 ns/op
  • Bone 47333 ns/op

Detailed benchmark here

API

Methods:

  • New - create a new multiplexer
  • Add - add a rule specifying the handler (the default method - GET, ID - as a string to this rule)
  • Start - start the server indicating the listening port
  • Params - extract parameters from URL
  • Create - generate URL of the available options
  • Shutdown - graceful stop the server
  • Stop - aggressive stop the server
  • Test - Start analogue (for testing only)

Example: m := bxog.New() m.Add("/", IHandler)

Named parameters

Arguments in the rules designated route colon. Example route: /abc/:param , where abc is a static section and :param - the dynamic section(argument).

Static files

The directory path to the file and its nickname as part of URL specified in the configuration file. This constants FILE_PREF and FILE_PATH

Author: Claygod
Source Code: https://github.com/claygod/Bxog 
License: View license

#go #golang #http 

What is GEEK

Buddha Community

Bxog: A Simple and Fast HTTP Router for Go (HTTP Request Multiplexer)
Nigel  Uys

Nigel Uys

1654358400

Bxog: A Simple and Fast HTTP Router for Go (HTTP Request Multiplexer)

Bxog is a simple and fast HTTP router for Go (HTTP request multiplexer).

Usage

An example of using the multiplexer:

package main

import (
    "io"
    "net/http"

    bx "github.com/claygod/Bxog"
)

// Handlers
func IHandler(w http.ResponseWriter, req *http.Request, r *bx.Router) {
    io.WriteString(w, "Welcome to Bxog!")
}
func THandler(w http.ResponseWriter, req *http.Request, r *bx.Router) {
    params := r.Params(req, "/abc/:par")
    io.WriteString(w, "Params:\n")
    io.WriteString(w, " 'par' -> "+params["par"]+"\n")
}
func PHandler(w http.ResponseWriter, req *http.Request, r *bx.Router) {
    // Getting parameters from URL
    params := r.Params(req, "country")
    io.WriteString(w, "Country:\n")
    io.WriteString(w, " 'name' -> "+params["name"]+"\n")
    io.WriteString(w, " 'capital' -> "+params["city"]+"\n")
    io.WriteString(w, " 'valuta' -> "+params["money"]+"\n")
    // Creating an URL string
    io.WriteString(w, "Creating an URL from the current route (This is an example of creating an another URL):\n")
    io.WriteString(w, r.Create("country", map[string]string{"name": "Russia", "city": "Moscow", "money": "rouble"}))
}

// Main
func main() {
    m := bx.New()
    m.Add("/", IHandler)
    m.Add("/abc/:par", THandler)
    m.Add("/country/:name/capital/:city/valuta/:money", PHandler).
        Id("country"). // For a convinience you can indicate a short ID
        Method("GET")  // It is not necessary to indicate the GET method here as the GET method is used by default but this way is used to set an allowed method
    m.Test()
    m.Start(":9999")
}

Click URLs:

Settings

Necessary changes in the configuration of the multiplexer can be made in the configuration file config.go

Perfomance

Bxog is the fastest router, showing the speed of query processing. Its speed is comparable to the speed of the popular multiplexers: Bone, Httprouter, Gorilla, Zeus. The test is done on a computer with a i3-6320 3.7GHz processor and 8 GB RAM. In short (less time, the better):

  • Bxog 163 ns/op
  • HttpRouter 183 ns/op
  • Zeus 12302 ns/op
  • GorillaMux 14928 ns/op
  • GorillaPat 618 ns/op
  • Bone 47333 ns/op

Detailed benchmark here

API

Methods:

  • New - create a new multiplexer
  • Add - add a rule specifying the handler (the default method - GET, ID - as a string to this rule)
  • Start - start the server indicating the listening port
  • Params - extract parameters from URL
  • Create - generate URL of the available options
  • Shutdown - graceful stop the server
  • Stop - aggressive stop the server
  • Test - Start analogue (for testing only)

Example: m := bxog.New() m.Add("/", IHandler)

Named parameters

Arguments in the rules designated route colon. Example route: /abc/:param , where abc is a static section and :param - the dynamic section(argument).

Static files

The directory path to the file and its nickname as part of URL specified in the configuration file. This constants FILE_PREF and FILE_PATH

Author: Claygod
Source Code: https://github.com/claygod/Bxog 
License: View license

#go #golang #http 

Fannie  Zemlak

Fannie Zemlak

1599854400

What's new in the go 1.15

Go announced Go 1.15 version on 11 Aug 2020. Highlighted updates and features include Substantial improvements to the Go linker, Improved allocation for small objects at high core counts, X.509 CommonName deprecation, GOPROXY supports skipping proxies that return errors, New embedded tzdata package, Several Core Library improvements and more.

As Go promise for maintaining backward compatibility. After upgrading to the latest Go 1.15 version, almost all existing Golang applications or programs continue to compile and run as older Golang version.

#go #golang #go 1.15 #go features #go improvement #go package #go new features

Go Route: Simple Yet Powerful HTTP Request Multiplexer

Few main features

  • Minimal core.
  • No external runtime dependencies. Custom middlewares which requires 3th party dependecies are places in separates repositories under goroute org.
  • HTTP Routing.
  • Middlewares support.
  • Global error handling.

Getting Started

Prerequisites

You need to have at least go 1.11 installed on you local machine.

Installing

Install go route package with go get

go get -u github.com/goroute/route

Start your first server. Create main.go file and add:

package main

import (
    "net/http"
    "log"
    "github.com/goroute/route"
)

type helloResponse struct {
    Title string `json:"title"`
}

func main() {
    mux := route.NewServeMux()
    
    mux.Use(func(c route.Context, next route.HandlerFunc) error {
        log.Println("Hello, Middleware!")
        return next(c)
    })
    
    mux.GET("/", func(c route.Context) error {
        return c.JSON(http.StatusOK, &helloResponse{Title:"Hello, JSON!"})
    })
    
    log.Fatal(http.ListenAndServe(":9000", mux))
}

Run it

go run main.go

More examples

See examples

Built With

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Acknowledgments

  • This project is largely inspired by echo. Parts of the code are adopted from echo. See NOTICE.

Author: Goroute
Source Code: https://github.com/goroute/route 
License: MIT license

#go #golang #http 

Go Server/API Micro Framework, HTTP Request Router, Multiplexer, Mux

πŸƒ gorouter

Go Server/API micro framework, HTTP request router, multiplexer, mux.

πŸš… Benchmark

➜  gorouter git:(master) βœ— go test -bench=. -cpu=4 -benchmem
test
goos: darwin
goarch: amd64
pkg: github.com/vardius/gorouter/v4
BenchmarkNetHTTP-4                  65005786            17.9 ns/op           0 B/op           0 allocs/op
BenchmarkFastHTTP-4                 69810878            16.5 ns/op           0 B/op           0 allocs/op
PASS
ok      github.com/vardius/gorouter/v4    3.808s

πŸ‘‰ Click here to see all benchmark results.

Features

  • Routing System
  • Middleware System
  • Authentication
  • Fast HTTP
  • Serving Files
  • Multidomain
  • HTTP2 Support
  • Low memory usage
  • Documentation

🚏 HOW TO USE

πŸ–₯️ API example setup

πŸ“– ABOUT

Contributors:

Want to contribute ? Feel free to send pull requests!

Have problems, bugs, feature ideas? We are using the github issue tracker to manage them.

πŸ“š Documentation

For documentation (including examples), visit rafallorenz.com/gorouter

For GoDoc reference, visit pkg.go.dev

Author: Vardius
Source Code: https://github.com/vardius/gorouter 
License: MIT license

#go #golang #framework #http 

Nigel  Uys

Nigel Uys

1651097100

Request: Go Request, Go Http Client

Request

HTTP Client for golang, Inspired by Javascript-axios Python-request. If you have experience about axios or requests, you will love it. No 3rd dependency.

Features

  • Make http requests from Golang
  • Transform request and response data

Installing

go mod:

go get github.com/monaco-io/request

Methods

  • OPTIONS
  • GET
  • HEAD
  • POST
  • PUT
  • DELETE
  • TRACE
  • CONNECT

Example

POST

package main

import (
    "github.com/monaco-io/request"
)

func main() {
    var body = struct {
         A string
         B int
        }{A: "A", B: 1}
    var result interface{}

    c := request.Client{
        URL:    "https://google.com",
        Method: "POST",
        Query: map[string]string{"hello": "world"},
        JSON:   body,
    }
    resp := c.Send().Scan(&result)
    if !resp.OK(){
        // handle error
        log.Println(resp.Error())
    }

    // str := resp.String()
    // bytes := resp.Bytes()

POST with local files

package main

import (
    "github.com/monaco-io/request"
)

func main() {
    c := request.Client{
        URL:    "https://google.com",
        Method: "POST",
        Query: map[string]string{"hello": "world"},
        MultipartForm: MultipartForm{
            Fields: map[string]string{"a": "1"},
            Files:  []string{"doc.txt"},
        },
    }
    resp := c.Send().Scan(&result)
    ...

POST step by step

package main

import (
    "github.com/monaco-io/request"
)

func main() {
    var response interface{}

    resp := request.
        New().
        POST("http://httpbin.org/post").
        AddHeader(map[string]string{"Google": "google"}).
        AddBasicAuth("google", "google").
        AddURLEncodedForm(map[string]string{"data": "google"}).
        Send().
        Scan(&response)
    ...

POST with context (1/2)

package main

import (
    "github.com/monaco-io/request"
    "context"
)

func main() {
    c := request.Client{
        Context: context.Background(),
        URL:       "https://google.com",
        Method:    "POST",
        BasicAuth: request.BasicAuth{
            Username: "google",
            Password: "google",
        },
    }
    resp := c.Send()
    ...

POST with context (2/2)

package main

import (
    "github.com/monaco-io/request"
    "context"
)

func main() {
    var response interface{}

    resp := request.
        NewWithContext(context.TODO()).
        POST("http://httpbin.org/post").
        AddHeader(map[string]string{"Google": "google"}).
        AddBasicAuth("google", "google").
        AddURLEncodedForm(map[string]string{"data": "google"}).
        Send().
        Scan(&response)
    ...

Authorization

package main

import (
    "github.com/monaco-io/request"
)

func main() {
    c := request.Client{
        URL:       "https://google.com",
        Method:    "POST",
        BasicAuth: request.BasicAuth{
            Username: "google",
            Password: "google",
        },
    }
    resp := c.Send()
}

Timeout

package main

import (
    "github.com/monaco-io/request"
)

func main() {
    c := request.Client{
        URL:       "https://google.com",
        Method:    "POST",
        Timeout:   time.Second*10,
    }
}

Cookies

package main

import (
    "github.com/monaco-io/request"
)

func main() {
    c := request.Client{
        URL:       "https://google.com",
        CookiesMap: map[string]string{
            "cookie_name": "cookie_value",
        }
    }
}

TLS

package main

import (

    "github.com/monaco-io/request"
)

func main() {
    c := request.Client{
        URL:       "https://google.com",
        TLSConfig: &tls.Config{InsecureSkipVerify: true},
    }
}

Author: Monaco-io
Source Code: https://github.com/monaco-io/request 
License: MIT License

#go #golang #http #client