router

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2023 License: MIT Imports: 1 Imported by: 0

README

go-http-router

A HTTP router implementation using vanilla Go.

Usage

package main

import (
	"log"
	"net/http"

	"dev.raphlcx.com/go/router"
)

type handler struct{}

func newHandler() handler {
	return handler{}
}

func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "text/plain")
	w.Write([]byte("Success"))
	return
}

func main() {
	r := router.New()

	r.Get("/", newHandler())
	// r.Head("/", newHandler())
	// r.Post("/", newHandler())
	// r.Put("/", newHandler())
	// r.Patch("/", newHandler())
	// r.Delete("/", newHandler())
	// r.Connect("/", newHandler())
	// r.Options("/", newHandler())
	// r.Trace("/", newHandler())

	mux := r.Mux()

	// or chain the routes
	// mux := router.New().
	// 	Get("/", newHandler()).
	// 	Head("/", newHandler()).
	// 	Post("/", newHandler()).
	// 	Put("/", newHandler()).
	// 	Patch("/", newHandler()).
	// 	Delete("/", newHandler()).
	// 	Connect("/", newHandler()).
	// 	Options("/", newHandler()).
	// 	Trace("/", newHandler()).
	// 	Mux()

	log.Fatal(http.ListenAndServe("localhost:8888", mux))
}

Test it out:

$ curl http://localhost:8888/
Success

$ curl http://localhost:8888/ -X POST
Not Implemented

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New() *router

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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