ember

package module
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2021 License: MIT Imports: 10 Imported by: 1

README

ember

Test GoDoc Release

Provides tools to serve Ember.js apps from Go HTTP handlers.

Documentation

Overview

Package ember provides tools to serve Ember.js apps from Go HTTP handlers.

Example
package main

import (
	"fmt"
	"net/http"
	"time"
)

const indexHTML = `<!DOCTYPE html>
<html>
  	<head>
		<meta charset="utf-8"/>
		<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
		<title>App</title>
		<meta name="description" content=""/>
		<meta name="viewport" content="width=device-width, initial-scale=1"/>
		<meta name="app/config/environment" content="%7B%22modulePrefix%22%3A%22app%22%2C%22environment%22%3A%22production%22%2C%22rootURL%22%3A%22%2F%22%2C%22locationType%22%3A%22auto%22%2C%22EmberENV%22%3A%7B%22FEATURES%22%3A%7B%7D%2C%22EXTEND_PROTOTYPES%22%3A%7B%22Date%22%3Afalse%7D%2C%22_JQUERY_INTEGRATION%22%3Afalse%2C%22_TEMPLATE_ONLY_GLIMMER_COMPONENTS%22%3Atrue%7D%2C%22APP%22%3A%7B%22name%22%3A%22app%22%2C%22version%22%3A%220.0.0%2Ba7250a80%22%7D%2C%22exportApplicationGlobal%22%3Afalse%7D"/>
		<link integrity="" rel="stylesheet" href="/assets/vendor-d41d8cd98f00b204e9800998ecf8427e.css"/>
		<link integrity="" rel="stylesheet" href="/assets/app-45c749a3bbece8e3ce4ffd9e6b8addf7.css"/>
  	</head>
  	<body>
		<script src="/assets/vendor-0602240bb8c898070836851c4cc335bd.js" integrity="sha256-x5KZQsQtD11ZTdqNAQIXsfX2GhhsgLLMP2D6P/QUXtc= sha512-JeMuQGObr+XCFa0pndQDId4cKiqROg4Ai0iR27Zgv9FE32p340XLGz6OpQm8PrmcRGShcxPNkh61sc19Sm87Lw=="></script>
		<script src="/assets/app-6a49fc3c244bed354719f50d3ca3dd38.js" integrity="sha256-Tf7uETTbqK91hJxzmSrymkqPCl8zrt7KEnQ46H7MlSo= sha512-/G/3aD3HMrxRYLK4mUFz7Cbo3miN0lKYHrknOFSzwqop4LOcVMSc02FpvKJFWUm91Ga0DvgC3wN4I4RboTBfLQ=="></script>
  	</body>
</html>
`

const scriptJS = `alert("Hello World!");`

var files = map[string]string{
	"index.html": indexHTML,
	"script.js":  scriptJS,
}

func main() {
	// create app
	app := MustCreate("app", files)

	// set static config
	app.Set("apiBaseURI", "http://api.example.com")

	// run listener
	go func() {
		panic(http.ListenAndServe("0.0.0.0:4242", app.Handler(func(app *App, r *http.Request) {
			app.Set("path", r.URL.Path)
		})))
	}()
	time.Sleep(10 * time.Millisecond)

	// get index
	index, typ := fetch("http://0.0.0.0:4242/hello")
	fmt.Println("==>", typ)
	fmt.Println(unIndent(index))

	// get asset
	asset, typ := fetch("http://0.0.0.0:4242/script.js")
	fmt.Println("==>", typ)
	fmt.Println(unIndent(asset))

}
Output:

==> text/html; charset=utf-8
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<title>App</title>
<meta name="description" content=""/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta name="app/config/environment" content="%7B%22APP%22:%7B%22name%22:%22app%22%2C%22version%22:%220.0.0+a7250a80%22%7D%2C%22EmberENV%22:%7B%22EXTEND_PROTOTYPES%22:%7B%22Date%22:false%7D%2C%22FEATURES%22:%7B%7D%2C%22_JQUERY_INTEGRATION%22:false%2C%22_TEMPLATE_ONLY_GLIMMER_COMPONENTS%22:true%7D%2C%22apiBaseURI%22:%22http:%2F%2Fapi.example.com%22%2C%22environment%22:%22production%22%2C%22exportApplicationGlobal%22:false%2C%22locationType%22:%22auto%22%2C%22modulePrefix%22:%22app%22%2C%22path%22:%22%2Fhello%22%2C%22rootURL%22:%22%2F%22%7D"/>
<link integrity="" rel="stylesheet" href="/assets/vendor-d41d8cd98f00b204e9800998ecf8427e.css"/>
<link integrity="" rel="stylesheet" href="/assets/app-45c749a3bbece8e3ce4ffd9e6b8addf7.css"/>
</head>
<body>
<script src="/assets/vendor-0602240bb8c898070836851c4cc335bd.js" integrity="sha256-x5KZQsQtD11ZTdqNAQIXsfX2GhhsgLLMP2D6P/QUXtc= sha512-JeMuQGObr+XCFa0pndQDId4cKiqROg4Ai0iR27Zgv9FE32p340XLGz6OpQm8PrmcRGShcxPNkh61sc19Sm87Lw=="></script>
<script src="/assets/app-6a49fc3c244bed354719f50d3ca3dd38.js" integrity="sha256-Tf7uETTbqK91hJxzmSrymkqPCl8zrt7KEnQ46H7MlSo= sha512-/G/3aD3HMrxRYLK4mUFz7Cbo3miN0lKYHrknOFSzwqop4LOcVMSc02FpvKJFWUm91Ga0DvgC3wN4I4RboTBfLQ=="></script>
</body>
</html>

==> application/javascript
alert("Hello World!");

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Files added in v0.4.1

func Files(f fs.FS, dir string) (map[string]string, error)

Files will return a file map from the provided file system directory.

func MustFiles added in v0.4.1

func MustFiles(f fs.FS, dir string) map[string]string

MustFiles will call Files and panic on errors.

Types

type App

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

App is an in-memory representation of an Ember.js application.

func Create

func Create(name string, files map[string]string) (*App, error)

Create will create and Ember.js application instance from the provided files. The provided map must at least include the "index.html" key with the contents of the index html file. All other files e.g. "assets/app.css" are served with their corresponding MIME types read from the file extension.

func MustCreate added in v0.1.1

func MustCreate(name string, files map[string]string) *App

MustCreate will call Create and panic on errors.

func (*App) AddFile added in v0.3.0

func (a *App) AddFile(name string, contents string)

AddFile will add the specified file to the app.

func (*App) AddInlineScript added in v0.3.0

func (a *App) AddInlineScript(js string)

AddInlineScript will append the provides JS at the end of the body tag.

func (*App) AddInlineStyle added in v0.3.0

func (a *App) AddInlineStyle(css string)

AddInlineStyle will append the provided CSS at the end of the head tag.

func (*App) AppendBody added in v0.3.0

func (a *App) AppendBody(tag string)

AppendBody will append the provided tag to the body tag.

func (*App) AppendHead added in v0.3.0

func (a *App) AppendHead(tag string)

AppendHead will append the provided tag to the head tag.

func (*App) Clone

func (a *App) Clone() *App

Clone will make a copy of the application.

func (*App) Get added in v0.3.2

func (a *App) Get(name string) interface{}

Get will get the specified setting from the application.

func (*App) Handler added in v0.3.0

func (a *App) Handler(configure func(*App, *http.Request)) http.Handler

Handler will construct and return a dynamic handler that invokes the provided callback for each page request to allow dynamic configuration. If no dynamic configuration is needed, the app should be served directly.

func (*App) IsAsset added in v0.3.0

func (a *App) IsAsset(path string) bool

IsAsset will return whether the provided path matches an asset.

func (*App) IsPage added in v0.3.0

func (a *App) IsPage(path string) bool

IsPage will return whether the provided path matches a page.

func (*App) Prefix added in v0.3.2

func (a *App) Prefix(prefix string)

Prefix will change the root URL and prefix all assets paths with the specified prefix. The app must be serve with http.StripPrefix() to work correctly.

func (*App) ServeHTTP

func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements the http.Handler interface.

func (*App) Set

func (a *App) Set(name string, value interface{})

Set will set the provided settings on the application.

Jump to

Keyboard shortcuts

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