Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/4freewifi/goroute

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/4freewifi/goroute

  • v0.0.0-20151215104333-6db4a697df7b
  • Source
  • Go
  • Socket score

Version published
Created
Source

goroute

A very simple URL router for net/http package based on regular expression.

Why another

The idea of http.Handler is great because it is just an interface so the developer can keep all the necessary info in its own context. However, most of the web framework or routing package out there only support http.HandleFunc, which means the context must be kept elsewhere. For example, consider the following program:

package main

import (
	"fmt"
	"net/http"
)

type MySrv struct {
	hello string
}

func (m *MySrv) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello, %s!", m.hello)
}

func main() {
	srv := MySrv{"World"}
	http.Handle("/", &srv)
	http.ListenAndServe("localhost:8080", nil)
}

In case of web.go, if keeping the original context MySrv is required, this program will become something like:

package main

import (
	"fmt"
	"net/http"
	"github.com/hoisie/web"
)

type MySrv struct {
	hello string
}

func (m *MySrv) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Hello, %s!\n", m.hello)
}

func main() {
	srv := MySrv{"World"}
	hello := func(ctx *web.Context, val string) {
		for k,v := range ctx.Params {
			fmt.Fprintln(ctx, k, v)
		}
		srv.ServeHTTP(ctx, ctx.Request)
	}
	web.Get("/(.*)", hello)
	web.Run("localhost:8080")
}

Because of function closure, srv MySrv is implicitly included in hello, and modifications made to srv will implicitly affect hello. Explicit is better than implicit. Many other projects suffer from similiar issue. However, the requirement of a simple URL path router still exists, e.g. in case of RESTful like GET /users/<userid>. A very thin layer that handles just routing and doesn't get in the way of the original structure of net/http will be handy.

Example

Check goroute_test.go.

Comparing to the original http.Handler, a new parameter pattern is required when you associate an Handler to a path . It should be a regular expression with named capturing group, and every named parentheses will be stored in the extra parameter map[string]string introduced in goroute.Handler interface.

API

Visit http://godoc.org/github.com/4freewifi/goroute

License

Copyright 2015 John Lee john@4free.com.tw

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

FAQs

Package last updated on 15 Dec 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc