README

Inertia-Echo - An Echo Server-Side Adapter for Inertia.js

GitHub license GitHub (pre-)release Donate

Inertia-Echo is an Echo (Go) server-side adapter for Inertia.js to build modern monolithic single-page apps. Based on inertia-laravel and zgabievi's PingCRM Demo.

A demo app can be found in the demo branch at https://github.com/elipZis/inertia-echo/tree/demo

Pre-requisites

Download and install Golang for your platform.

Notes

This module serves as middleware to the Echo server system and has to be registered accordingly. It is not intended to be used without Echo and a client-side Inertia.js.

For usage instructions about Echo please refer to the official documentation

Setup

Create a copy of the example environment variables

cp .env.example .env

Setup your own properties accordingly.

In the configured resources directory you need to create a views folder and a configured INERTIA_ROOT_VIEW file. You may use the example file provided in this repository which requires Webpack and Mix.

Usage

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

import (
	...

	"github.com/elipzis/inertia-echo"
	
    ...
)

e := echo.New()
e.Use(inertia.Middleware(e))

Import the module into your project.

The middleware hooks into the Echo error and template rendering with a dedicated Inertia instance by itself. Therefore, to render a client-side Inertia.js view you can register a route and render a component

// Handler
func hello(c echo.Context) error {
    // Status, Component Name, Data to pass on
    return c.Render(http.StatusOK, "Index", map[string]interface{}{})
}

// Route
e.GET("/hello", hello)

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/elipZis/inertia-echo/tree/demo

Configuration

You can leverage several ...WithConfig functions to configure this module to your needs.

For example, you may create your own Inertia-Echo instance via NewInertia(...) and pass the instance to the middleware via

e.Use(inertia.MiddlewareWithConfig(inertia.MiddlewareConfig{
    Inertia: MyInertia,
}))

By that you enable yourself to use functionality such as Share(...) in your own e.g. handlers.

License and Credits

This module is released under the MIT license by elipZis.

This program uses multiple other libraries. Credits and thanks to all the developers working on these great projects:

and many more.

Disclaimer

This source and the whole package comes without a warranty. It may or may not harm your computer. It is not a reference for best-practices or security concerns or any other application concept. Please use with care and not as absolute reference.
Any damage cannot be related back to the author.

Expand ▾ Collapse ▴

Documentation

Index

Constants

View Source
const HeaderLocation = HeaderPrefix + "-Location"
View Source
const HeaderPartialData = HeaderPrefix + "-Partial-Data"
View Source
const HeaderPrefix = "X-Inertia"

The Base "X-Inertia" header prefixes

View Source
const HeaderVersion = HeaderPrefix + "-Version"

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,
	})
}

Functions

func Middleware

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

Create a default Inertia Middleware for the given echo reference if the Inertia instance itself is not required in other instances

func MiddlewareWithConfig

func MiddlewareWithConfig(config MiddlewareConfig) echo.MiddlewareFunc

The Inertia Middleware to check every request for what it needs

Types

type Inertia

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

func NewInertia

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

Instance a new Inertia Handler with a default config

func NewInertiaWithConfig

func NewInertiaWithConfig(config InertiaConfig) (this *Inertia)

Instance a new Inertia Handler with a configuration

func (*Inertia) GetShared

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

Get a specific key-value from the shared information

func (*Inertia) GetVersion

func (this *Inertia) GetVersion() string

func (*Inertia) Render

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

Render renders a template document

func (*Inertia) SetMixVersion

func (this *Inertia) SetMixVersion(mixManifestPath ...string) bool

Create a version hash off of the mix-manifest.json file md5

func (*Inertia) SetVersion

func (this *Inertia) SetVersion(version string)

Set a version string

func (*Inertia) Share

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

Share a key/value pairs with every response

func (*Inertia) Shared

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

Returns the shared props (if any) and deletes them

func (*Inertia) Shares

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

Share multiple key/values with every response

func (*Inertia) Version

func (this *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
}

func NewDefaultInertiaConfig

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

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

type MiddlewareConfig

type MiddlewareConfig struct {
	Inertia *Inertia
	Skipper middleware.Skipper
}

type Response

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

func NewResponse

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

func (Response) Status

func (this Response) Status(code int) Response

Allow to set a custom status

func (Response) ToResponse

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

func (Response) With

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

func (Response) WithViewData

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

Directories

Path Synopsis
util