inertia

package module
v0.0.0-...-3b4c58b Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2023 License: MIT Imports: 13 Imported by: 0

README

Inertia.js Go (Echo) Adapter

The Inertia.js server-side adapter for Go. Visit inertiajs.com to learn more.


This module has fixed some bugs. Using Vite instead of Laravel-Mix

Original Source Code from here: https://github.com/elipZis/inertia-echo .
Stripped off everything except ./LICENSE.

Usage

Create a new Echo instance and register the Inertia middleware with it

func main() {
    e := echo.New()
    e.Use(inertia.Middleware(e))
    e.Static("/", "./public")

    e.GET("/", hello).Name = "hello"

    e.Logger.Fatal(e.Start("127.0.1.1:3000"))
}

func hello(c echo.Context) error {
    return c.Render(http.StatusOK, "Welcome", map[string]interface{}{})
}

The internal template renderer of Inertia-Echo checks whether a fresh full base-site has to be returned or only the reduced Inertia response.

For more examples refer to the demo branch at https://github.com/theartefak/demo-inertia-echo

License

This open-sourced software is licensed under the MIT license.

Documentation

Index

Constants

View Source
const (
	HeaderPrefix      = "X-Inertia"
	HeaderVersion     = HeaderPrefix + "-Version"
	HeaderLocation    = HeaderPrefix + "-Location"
	HeaderPartialData = HeaderPrefix + "-Partial-Data"
)

The Base "X-Inertia" header prefixes

Variables

View Source
var DefaultHTTPErrorHandler = func(err error, c echo.Context) {
	code := http.StatusInternalServerError
	if he, ok := err.(*echo.HTTPError); ok {
		code = he.Code
	}
	_ = c.Render(code, "Error", map[string]interface{}{
		"status": code,
	})
}

DefaultHTTPErrorHandler is the default error handler for Inertia.

Functions

func Middleware

func Middleware(echo *echo.Echo) echo.MiddlewareFunc

Middleware creates a default Inertia middleware for the given Echo instance.

func MiddlewareWithConfig

func MiddlewareWithConfig(config MiddlewareConfig) echo.MiddlewareFunc

MiddlewareWithConfig creates an Inertia middleware with a specific configuration.

Types

type Inertia

type Inertia struct {
	// contains filtered or unexported fields
}

Inertia is a struct representing the Inertia handler.

func NewInertia

func NewInertia(echo *echo.Echo) (i *Inertia)

Instance a new Inertia Handler with a default config

func NewInertiaWithConfig

func NewInertiaWithConfig(config InertiaConfig) (i *Inertia)

Instance a new Inertia Handler with a configuration

func (*Inertia) GetShared

func (i *Inertia) GetShared(c echo.Context, key string) (interface{}, bool)

Get a specific key-value from the shared information

func (*Inertia) GetVersion

func (i *Inertia) GetVersion() string

func (*Inertia) Render

func (i *Inertia) Render(w io.Writer, name string, data interface{}, c echo.Context) error

Render renders a template document

func (*Inertia) SetVersion

func (i *Inertia) SetVersion(version string)

Set a version string

func (*Inertia) Share

func (i *Inertia) Share(c echo.Context, key string, value interface{})

Share a key/value pairs with every response

func (*Inertia) Shared

func (i *Inertia) Shared(c echo.Context) map[string]interface{}

Returns the shared props (if any) and deletes them

func (*Inertia) Shares

func (i *Inertia) Shares(c echo.Context, values map[string]interface{})

Share multiple key/values with every response

func (*Inertia) Version

func (i *Inertia) Version(version func() string)

Set a version callback "func() string"

type InertiaConfig

type InertiaConfig struct {
	Echo             *echo.Echo
	PublicPath       string
	TemplatesPath    string
	RootView         string
	TemplateFuncMap  template.FuncMap
	HTTPErrorHandler echo.HTTPErrorHandler
	RequestIDConfig  middleware.RequestIDConfig
}

InertiaConfig holds the configuration for the Inertia handler.

func NewDefaultInertiaConfig

func NewDefaultInertiaConfig(e *echo.Echo) (i InertiaConfig)

Create a default Inertia Config to use without the hassle of setting up everything

type MiddlewareConfig

type MiddlewareConfig struct {
	Inertia *Inertia           // Reference to the Inertia instance
	Skipper middleware.Skipper // Skipper function to conditionally skip the middleware
}

MiddlewareConfig holds configuration options for the Inertia middleware.

type Response

type Response struct {
	// contains filtered or unexported fields
}

Response represents an Inertia response.

func NewResponse

func NewResponse(component string, props map[string]interface{}, rootView string, version string) Response

NewResponse creates a new Inertia response.

func (Response) Status

func (r Response) Status(code int) Response

Status sets a custom status for the Inertia response.

func (Response) ToResponse

func (r Response) ToResponse(c echo.Context) error

ToResponse sends the Inertia response to the client.

func (Response) With

func (r Response) With(key interface{}, value interface{}) Response

With adds a key-value pair to the response's props.

func (Response) WithViewData

func (r Response) WithViewData(key interface{}, value interface{}) Response

WithViewData adds a key-value pair to the response's view data.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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