GithubHelp home page GithubHelp logo

joseph-beck / routey Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 103 KB

a simple HTTP Go router

Home Page: https://pkg.go.dev/github.com/joseph-beck/routey

License: MIT License

Makefile 1.01% Go 98.46% HTML 0.53%
go http

routey's Introduction

GitHub go.mod Go version (subdirectory of monorepo) GitHub License GitHub release (with filter) GitHub Workflow Status (with event)

routey

An extremely simple Go HTTP Router made for fun! This was heavily inspired by gin.

Installation

$ go get -u github.com/joseph-beck/routey
import (
    route "github.com/joseph-beck/routey/pkg/router"
)

Examples

Getting Started

package main

import (
    "fmt"
    "net/http"

    routey "github.com/joseph-beck/routey/pkg/router"
)

func main() {
    r := routey.New()
    go r.Shutdown()

    r.Route(routey.Route{
        Path: "/hello",
        Params: "",
        Method: routey.Get,
        HandleFunc: func(c *routey.Context) {
            b := "Hello world!"
            c.Render(http.StatusOK, b)
        }
        DecoratorFunc: nil,
    })

    r.Run()
}

Please note that routes defined before others will have precedence, for example a route of /api/:id will have complete precedence over a route /api/ping even if the route /api/ping is called.

func main() {
    c := routey.Config{
        Port:  ":3000",
        Debug: true,
        CORS:  false,
    }

    r := routey.New(c)
}

Routey also has a few configuration options, and hopefully more to come in the future, that allow you to manipulate modes and the ports used.

Using parameters

func handler() routey.HandlerFunc {
    return func(c *routey.Context) {
        p, err := c.Param("param")
        if err != nil {
            c.Status(http.BadRequest)
            return
        }

        c.Render(http.StatusOK, p)
    }
}

r.Add(
    "/route",
    "/:param",
    routey.Get,
    handler(),
    nil,
)

Using queries

func handler() routey.HandlerFunc {
    return func(c *routey.Context) {
        q, err := c.Query("query")
        if err != nil {
            c.Status(http.BadRequest)
            return
        }

        c.Render(http.StatusOK, q)
    }
}

Creating a HandlerFunc

func handler() routey.HandlerFunc {
    return func(c *routey.Context) {
        b := "I am a handler!"
        c.Render(http.StatusOK, b)
    }
}

Declaring a handler function this way allows us to more easily use dependency injection.

Creating a DecoratorFunc

func decorator(f routey.HandlerFunc) routey.HandlerFunc {
    return func(c *routey.Context) {
        fmt.Println("I am a decorator!")
        f(c)
    }
}

Declaring a decorator function this way allows us to decorate decorator functions as well as more easily use dependency injection. They can be used for a variety of things, but commonly used in protecting our end points.

Adding Middleware

func middleware() routey.MiddlewareFunc {
    return (c *routey.Context) {
        fmt.Println("I am middleware!")
    }
}

func main() {
    r := routey.New()
    r.Use(middleware())
}

Although Routey has more of an emphasis on using Decorators, Middleware is a great option for something you would like to use on all of your routes. It uses the same context as the Handler will use so values can even be communicated between the Middleware and Handler.

Services

routey also supports the use of Services, which are structs with methods that are your endpoints, every Service must implement an Add() []Route that can be registered to the App with the Register() method.

type HelloService struct{}

func (s HelloService) Add() []Route {
    return []Route{
        {
            Path:          "/hello",
            Params:        "",
            Method:        Get,
            HandlerFunc:   s.Get(),
            DecoratorFunc: nil,
        },
    }
}

func (s *HelloService) Get() HandlerFunc {
    return func(c *Context) {
        c.Status(http.StatusOK)
    }
}

When creating instances of your Service you can give dependencies to the struct for example a database connection, etc.

Rendering HTML

func main() {
    ...
    r.LoadHTMLGlob("web/*")
    ...
}

// /index
func indexHandler() routey.HandlerFunc {
    return func(c *routey.Context) {
        c.HTML(
            http.StatusOK,
            "index.html",
            nil,
        )
    }
}

// /name/:name
func helloHandler() routey.HandlerFunc {
    return func(c *routey.Context) {
        name, err := c.Param("name")
        if err != nil {
            c.Status(http.StatusBadRequest)
            return
        }

        c.HTML(
            http.StatusOK,
            "hello.html",
            routey.M{
                "name": name,
            },
        )
    }
}

We can render HTML pages by loading a glob, or group of HTML files in a directory, or just specific files. The endpoints are added to the App just like any other endpoint. We can link HTML pages together by creating an endpoint for another HTML file and using a href to that, for example:

index.html

<!DOCTYPE html>
<html>
<body>

<h1>Routey</h1>
<a href="/hello">hello<a/>

</body>
</html>

hello.html

<!DOCTYPE html>
<html>
<body>

<p>Hello {{ .name }}<p>
<a href="/index">home<a/>

</body>
</html>

routey's People

Contributors

joseph-beck avatar dependabot[bot] avatar

Watchers

 avatar

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.