controllers

package
v0.0.0-...-772259c Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2021 License: Apache-2.0 Imports: 8 Imported by: 0

README

GhastControllers

Ghast's Controllers are extremely easy to use and provide a few minor-abstractions to help you write simpler and cleaner code.

To use a GhastController, you can either run ghast make controller MyControllerNameHere or create your own controller and embed the GhastController struct into it.

Ghast's controllers are a little different than the standard http.handlers that the router supports (although technically a controller can use those too!). These controllers are intended to be used with a signature like so: func (c *TestController) Index(r *http.Request) (router.Response, error), however, binding these to a router requires a special step.

Namely, controllers that are built this way need to have their receiver functions passed to the router.RouteFunc to add support for the serveHTTP method thats necessary for our router (essentially making these conform to http.Handler from the standard library).

A simple example of a controller can be seen here:

package controllers

import (
	"net/http"

	"github.com/bradcypert/ghast/pkg/controllers"
	"github.com/bradcypert/ghast/pkg/router"
)

type TestController struct {
	controllers.GhastController
}

type Thing struct {
	Foo string
}

func (c *TestController) Index(r *http.Request) (router.Response, error) {
	myStruct := Thing{Foo: "bar"}
	return router.Response{
		Body: myStruct
	}, nil
}

Controllers also provide a lot of functionality for working with the HTTP request and response writer objects.

package controllers

import (
	"net/http"

	"github.com/bradcypert/ghast/pkg/controllers"
)

type TestController struct {
	controllers.GhastController
}

func (c *TestController) Index(r *http.Request) (router.Response, error) {
	// get user ID from path, /user/:user
	userId := c.PathParam("user").(string)
	
	// ...
}

I strongly recommend checking out the GoDoc for Ghast to get a better understanding of what is possible with Ghast's controllers (and all of Ghast, for that matter).

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type GhastController

type GhastController struct{}

GhastController should be embedded into consumer controllers and provides helper functions for working with the responseWriter, etc.

func (GhastController) Config

func (c GhastController) Config(key string) interface{}

Config gets a config value from the controller's container. Config keys map to YAML in a flattened dot structure prefixed by an @. For example: a:

b: "c"

"c" can be retrieved via @a.b We can't guarantee the type, so we return interface{}

func (GhastController) Container

func (c GhastController) Container() *ghastContainer.Container

Container returns the DI container associated with the given controller/request pairing.

func (GhastController) PathParam

func (c GhastController) PathParam(r *http.Request, key string) interface{}

PathParam Get a Path Parameter from a given request and key

func (GhastController) QueryParam

func (c GhastController) QueryParam(r *http.Request, key string) []string

PathParam Get a Path Parameter from a given request and key returns a list of strings as it supports multiple values for a given path param

func (GhastController) Unmarshal

func (c GhastController) Unmarshal(r *http.Request, s interface{}) error

Unmarshal unmarshalls a request with a body into the provided struct returns an error or nil value depending on if the unmarshall succeeded or not.

func (GhastController) View

func (c GhastController) View(name string, vars jet.VarMap, contextualData interface{}) (router.Response, error)

View executes a view from the app templates returns a response with the body set to the template Feel free to modify the response object further before returning in your controller

Jump to

Keyboard shortcuts

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