vibe

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2022 License: MIT Imports: 4 Imported by: 0

README

Vibe

Always feel good about your Golang servers.

About

Vibe is a project that is meant to be used alongside Fiber, a fast and easy web server library for Go. It has a very similar API to Express. Vibe extends Fiber's functionality by adding controller functionality, designed to be Rails-like.

API

See the docs

Documentation

Here's an example of the API:

package main

import (
  "log"

  "github.com/gofiber/fiber/v2"
  "github.com/ztcollazo/vibe"
)

type AppController struct {
  vibe.Controller[*AppController]
  Hello string
}

func (a *AppController) Setup(c *fiber.Ctx) {
  a.Hello = "Hello World!"
}

func (a *AppController) Index(c *fiber.Ctx) error {
  return c.SendString(a.Hello)
}

func main() {
  app := fiber.New()
  app.Route("/", vibe.CreateController(&AppController{}))
  log.Fatal(app.Listen(":3000"))
}

This is a basic example. You can view a (slightly) more in-depth example in the example folder As you can see, it uses an Index method to route to /. The supported builtin routes are:

  • Index: GET /
  • Show: GET /:id
  • New: GET /new
  • Create: POST /
  • Edit: GET /:id/edit
  • Update: POST /:id
  • Destroy: DELETE /:id

Defining custom routes is easy. You can create a Routes function on your controller, which returns a value of type map[string]vibe.Route. For example:

func (a *AppController) Hello(c *fiber.Ctx) error {
  return c.SendString("This will be at GET `/hello`")
}

func (a *AppController) Routes() map[string]vibe.Route {
  return map[string]vibe.Route{
    "Hello": {
      Path: "/hello",
      Method: "GET",
    }
  }
}

It's that easy. You can even leave out a property such as Path or Method (but not both, so that the value is not the Zero value), and Vibe will guess it for you.

Roadmap

  • Setup Middleware
  • Request Handlers
  • Default Paths
  • Custom context and rendering

...and more that may come up in the future.

License

Vibe is licensed under the MIT license. View the LICENSE.txt for more information.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Defaults = map[string]Route{
	"Index": {
		Method: "GET",
		Path:   "/",
	},
	"New": {
		Method: "GET",
		Path:   "/new",
	},
	"Create": {
		Method: "POST",
		Path:   "/",
	},
	"Edit": {
		Method: "GET",
		Path:   "/:id/edit",
	},
	"Update": {
		Method: "POST",
		Path:   "/:id",
	},
	"Destroy": {
		Method: "DELETE",
		Path:   "/:id",
	},
	"Show": {
		Method: "GET",
		Path:   "/:id",
	},
}

Functions

func CreateController

func CreateController[Ctr ictr[Ctr]](ctr Ctr) func(fiber.Router)

Types

type Controller

type Controller[Ctr CtrBase] struct {
	// contains filtered or unexported fields
}

func (Controller[Ctr]) Routes

func (c Controller[Ctr]) Routes() map[string]Route

func (Controller[Ctr]) Setup

func (c Controller[Ctr]) Setup(ctx *fiber.Ctx)

type CtrBase

type CtrBase interface {
	Setup(ctx *fiber.Ctx)
	Routes() map[string]Route
}

type Route

type Route struct {
	Method  string
	Path    string
	Handler fiber.Handler
}

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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