bekrouter

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2024 License: MIT Imports: 5 Imported by: 0

README

Bekrouter

This project is based on go 1.22 with enhanced routing, it illustrates how easy it is to use Go's new routing API. This repository can be used as a module for our project if you like. It takes no more than 3 lines to have a whole restful Route to be added to your server

Requirements

go 1.22

Example usage:

package main

import (
	"fmt"
	"github.com/lmbek/bekrouter"
	"github.com/lmbek/bekrouter/example/mvc/controllers"
	"github.com/lmbek/bekrouter/example/mvc/models"
	"github.com/lmbek/bekrouter/example/server"
	"github.com/lmbek/bekrouter/wrapper"
)

func main() {
	// use bekrouter by initializing a new instance
	var router = bekrouter.New()
	
	// (Restful) Use it like this - you might want to make your own json wrapper and addRestfulJsonRoute
	// add routes to bekrouter, give a route and a data model, then restful routes will be added for each of the endpoint (CRUD)
	router.AddRestfulJSONRoute("/api/activities/", models.Activity{})
	router.AddRestfulJSONRoute("/api/users/", models.User{})
	
	// (Rest) alternatively if you don't want restful, you could do things manually
	// we could also add all or some of the routes ourselves, but this would result in more code
	router.AddRoute("GET /api/events/", wrapper.Json(controllers.HandleGet(models.Activity{})))            // GET /api/<your route here>/
	router.AddRoute("GET /api/events/{id}/", wrapper.Json(controllers.HandleGetById(models.Activity{})))   // GET /api/<your route here>/{id}/
	router.AddRoute("POST /api/events/", wrapper.Json(controllers.HandlePost(models.Activity{})))          // POST /api/<your route here>/
	router.AddRoute("PUT /api/events/{id}/", wrapper.Json(controllers.HandlePut(models.Activity{})))       // PUT /api/<your route here>/{id}/
	router.AddRoute("DELETE /api/events/{id}/", wrapper.Json(controllers.HandleDelete(models.Activity{}))) // DELETE /api/<your route here>/{id}/
	
	// initialise new server, pass the router.Mux and start it
	myServer := server.New("127.0.0.1", "8070", router.Mux)
	err := myServer.Start()
	if err != nil {
	fmt.Println(err)
	}
}

Contributors:

Lars Morten Bek (https://github.com/lmbek)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Route

type Route struct {
	Pattern     string
	HandlerFunc http.HandlerFunc
}

Route represents a route for handling HTTP requests. It contains the pattern for matching a request URL and the handler function for processing the request.

type Router added in v0.3.0

type Router struct {
	Routes []Route
	Mux    *http.ServeMux
}

Router is a type that represents a Router for handling HTTP requests.

func New

func New(routes ...Route) *Router

New creates a new instance of the router. It initializes a router struct with an empty route list and a new http.ServeMux. Returns a pointer to the newly created router instance.

Example:

myRouter := New()

func (*Router) AddRestfulJSONRoute added in v0.3.0

func (router *Router) AddRestfulJSONRoute(route string, handler controllers.IHandler)

AddRestfulJSONRoute adds the restful routes to the router for JSON responses. It takes a route string and an IHandler interface as parameters. The route parameter represents the base route for the endpoints. The handler parameter is an implementation of the IHandler interface, which defines the methods GetALl, GetById, Post, Put, and Delete. The method constructs the URL patterns using the route parameter and adds them to the router using the AddRoute method. The endpoint URLs are constructed as follows:

GET /api/{route}/          for retrieval of all entities
GET /api/{route}/{id}/     for retrieval of a specific entity by ID
POST /api/{route}/         for creating a new entity
PUT /api/{route}/{id}/     for updating a specific entity by ID
DELETE /api/{route}/{id}/  for deleting a specific entity by ID

Parameter: - route (string): The base route for the endpoints. - handler (controllers.IHandler): An implementation of the IHandler interface.

Example:

myRouter.AddRestfulJSONRoute("/api/activities/", models.Activity{})

Note: The IHandler interface must be implemented by a struct and should define the methods GetAll, GetById, Post, Put, and Delete.

The wrapper.Json function wraps the IHandler methods with JSON response generation.
The router.AddRoute method is used to add the route patterns to the router.
The models.Activity struct is an example implementation of the IHandler interface.

func (*Router) AddRoute added in v0.3.0

func (router *Router) AddRoute(pattern string, handler http.HandlerFunc)

AddRoute appends a new Route to the list of routes in the router. It takes a pattern string and a handler function as parameters. The pattern represents the URL pattern to match, and the handler is the function to be executed when the pattern is matched. The method creates a new Route struct with the given pattern and handler, and appends it to the Routes list. It also adds the pattern and handler to the http.ServeMux of the router.

Example:

myRouter.AddRoute("GET /api/users/", HandlerFunc)
myRouter.AddRoute("GET /api/users/{id}/", HandlerFunc)
myRouter.AddRoute("POST /api/users/", HandlerFunc)
myRouter.AddRoute("PUT /api/users/{id}/", HandlerFunc)
myRouter.AddRoute("DELETE /api/users/{id}/", HandlerFunc)

Parameters: - pattern (string): The URL pattern to match. - handler (http.HandlerFunc): The function to be executed when the pattern is matched.

Directories

Path Synopsis
mvc

Jump to

Keyboard shortcuts

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