Skip to content

Commit

Permalink
Initial commit. Handlr, Router, and utils created.
Browse files Browse the repository at this point in the history
  • Loading branch information
mariomenjr committed Apr 10, 2022
0 parents commit 4b04695
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .dev/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# .dev directory

To easily implement Handlr for development purposes.
26 changes: 26 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work

# Custom
# Dev. Easily test implement package for development purposes
.dev/*.*
!.dev/README.md
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Debug",
"type": "go",
"request": "launch",
"mode": "auto",
"program": ".dev"
}
]
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Mario Menjivar

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Handlr

Easily manage routes and handlers on top of *http.ServeMux.

## License
The source code of this project is under [MIT License](https://opensource.org/licenses/MIT).
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module mariomenjr/handlr

go 1.17
32 changes: 32 additions & 0 deletions handlr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package handlr

import (
"fmt"
"net/http"
)

// Allows end user to create an instance of Handlr
func New() *Handlr {
return &Handlr{http.NewServeMux(), Router{path: "/"}}
}

// It houses the main Router as well as the mux instances.
type Handlr struct {
mux *http.ServeMux
router Router
}

// Registers Routers and ListenAndServer over Handlr.mux
func (h *Handlr) Start(portNumber int) error {
h.router.regiterRoutesAndHandler(h.mux)
return h.ListenAndServe(portNumber)
}

// ListenAndServer over Handlr.mux
func (h *Handlr) ListenAndServe(portNumber int) error {
portString := fmt.Sprintf(":%d", portNumber)

fmt.Printf("> Server started on port %s", portString)

return http.ListenAndServe(portString, h.mux)
}
13 changes: 13 additions & 0 deletions handlr.router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package handlr

import "net/http"

// Aliases the Route method from Handlr.Router to Handlr.
func (h *Handlr) Route(path string, routeHandler func(r *Router)) {
h.router.Route(path, routeHandler)
}

// Aliases the Handler method from Handlr.Router to Handlr.
func (h *Handlr) Handler(path string, actionHandler func(w http.ResponseWriter, r *http.Request)) {
h.router.Handler(path, actionHandler)
}
54 changes: 54 additions & 0 deletions router.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package handlr

import (
"log"
"net/http"
)

// A router instance will house paths and handlers.
// It will also keep track of their hierarchy.
//
// Ideally, you'd like to distribute your handlers
// into separate files and plugin them in as Routes.
type Router struct {
path string
parent *Router
children []*Router
handler *func(w http.ResponseWriter, r *http.Request)
}

// Allows Route registration.
// You don't program behavior through this method.
func (r *Router) Route(path string, routeHandler func(r *Router)) {
router := &Router{path: path, parent: r}
routeHandler(router)

r.children = append(r.children, router)
}

// Allows Handler registration which gives you the ability
// to tie a behavior to a path.
// i.e. Get a record from database by hiting URL:
// http://example.org/get/record?id=1
func (r *Router) Handler(path string, actionHandler func(w http.ResponseWriter, r *http.Request)) {
router := &Router{path: path, parent: r, handler: &actionHandler}

r.children = append(r.children, router)
}

// Recursively register handlers for paths.
// An error will be thrown if the same path is registered twice, no ServeMux
// instance is provided, or mux.HandleFunc throws an error itself.
func (r *Router) regiterRoutesAndHandler(mux *http.ServeMux) {
if mux == nil {
log.Fatal("router: No *http.ServeMux instance provided for registering routes and handlers.")
}

for _, v := range r.children {
if v.handler != nil {
mux.HandleFunc(v.buildPath(), *v.handler)
}

v.regiterRoutesAndHandler(mux)
}
}
21 changes: 21 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package handlr

import (
"path"
"strings"
)

// Given a Router instance and attempts to
// produce its endpoint path based on parent
// and children routes.
func (r *Router) buildPath() string {
if r.parent == nil {
return r.path
}
return path.Join(r.parent.buildPath(), trimSlash(r.path))
}

// Trims slashes from a string.
func trimSlash(endpoint string) string {
return strings.Trim(endpoint, "/")
}

0 comments on commit 4b04695

Please sign in to comment.