Skip to content

Latest commit

 

History

History
60 lines (44 loc) · 1.48 KB

README.md

File metadata and controls

60 lines (44 loc) · 1.48 KB

Router

A simple router written in Go for APIs and servers of Eodomius projects

Installation

go get github.com/eodomus/router

Usage

Create a router

You can create a new router with the New function :

var router = router.New()

Handle a request

You can handle a request with the Get, Post, Patch, Put, and Delete functions :

  • First parameter is the path of the request

  • Second parameter is the function to call when the request is received

    • First parameter is the responce
    • Second parameter is the request
    • Third parameter is a Result structure
  router.Get("/test/{id}", func(w http.ResponseWriter, r *http.Request, result *Result){
    w.Write([]byte("Get : Test ID : " + result.Params["id"]))
  })
  router.Post("/test", func(w http.ResponseWriter, r *http.Request, result *Result){
    w.Write([]byte("Post : Test"))
  })
  router.Patch("/test", func(w http.ResponseWriter, r *http.Request, result *Result){
    w.Write([]byte("Patch : Test"))
  })
  router.Put("/test", func(w http.ResponseWriter, r *http.Request, result *Result){
    w.Write([]byte("Put : Test"))
  })
  router.Delete("/test", func(w http.ResponseWriter, r *http.Request, result *Result){
    w.Write([]byte("Delete : Test"))
  })

Typedef

Result

Result is a structure that contains parameters.

type Result struct {
  Params map[string]string // [paramName]paramValue
}