Coder Social home page Coder Social logo

bxog's Introduction

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

API documentation Go Report Card Mentioned in Awesome Go

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

Copyright

Copyright © 2016-2022 Eduard Sesigin. All rights reserved. Contacts: [email protected]

bxog's People

Contributors

claygod avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bxog's Issues

I think, I found an error in the example

Hello.

Thank you for this framework!

When I start your example, I saw an error in the output. If I used a recommended url http://localhost/country/USA/capital/Washington/valuta/dollar, I did't see a Country in the last string and I saw "Moscow" in the capital place.

I think, the code below is more right (and I replaced "valuta" to "currency"):

package main

import (
"io"
"net/http"

"github.com/claygod/bxog"

)

// Handlers
func IHandler(w http.ResponseWriter, req *http.Request, r *bxog.Router) {
io.WriteString(w, "Welcome to Bxog!")
}
func THandler(w http.ResponseWriter, req *http.Request, r *bxog.Router) {
params := r.Params(req, "/abc/:para")
io.WriteString(w, "Params:\n")
io.WriteString(w, " 'par' -> "+params["par"]+"\n")
}
func PHandler(w http.ResponseWriter, req *http.Request, r *bxog.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, " 'Currency' -> "+params["money"]+"\n")
// Creating a URL string
io.WriteString(w, "Creating a URL from route:\n")
io.WriteString(w, r.Create("country", params))

}

// Main
func main() {
m := bxog.New()
m.Add("/", IHandler)
m.Add("/abc/:par", THandler)
m.Add("/country/:name/capital/:city/currency/:money", PHandler).
Id("country"). // For ease indicate the short ID
Method("GET") // GET method do not need to write here, it is used by default (this is an example)
m.Start(":80")
}

Publish more

Почему только на хабре?
Послушай коммент с хабра: все сущности, которые не нужны пользователю, сделай приватными.
И публикуй в golang-nuts гугл группе, а также в golang сабреддите (reddit.com/r/golang)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.