ember

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2020 License: MIT Imports: 8 Imported by: 1

README

ember

Build Status Coverage Status GoDoc Release Go Report Card

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"
	"io/ioutil"
	"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>
`

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

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

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

	// run listener
	go func() {
		panic(http.ListenAndServe("0.0.0.0:4242", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			// clone app
			app := app.Clone()

			// set dynamic config
			app.MustSet("path", r.URL.Path)

			// serve app
			app.ServeHTTP(w, r)
		})))
	}()
	time.Sleep(10 * time.Millisecond)

	// get page
	res, err := http.Get("http://0.0.0.0:4242/hello")
	if err != nil {
		panic(err)
	}

	// ensure body is closed
	defer res.Body.Close()

	// read body
	data, err := ioutil.ReadAll(res.Body)
	if err != nil {
		panic(err)
	}

	fmt.Println(unIndent(string(data)))

}
Output:

<!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%3A%7B%22name%22%3A%22app%22%2C%22version%22%3A%220.0.0%2Ba7250a80%22%7D%2C%22EmberENV%22%3A%7B%22EXTEND_PROTOTYPES%22%3A%7B%22Date%22%3Afalse%7D%2C%22FEATURES%22%3A%7B%7D%2C%22_JQUERY_INTEGRATION%22%3Afalse%2C%22_TEMPLATE_ONLY_GLIMMER_COMPONENTS%22%3Atrue%7D%2C%22apiBaseURI%22%3A%22http%3A%2F%2Fapi.example.com%22%2C%22environment%22%3A%22production%22%2C%22exportApplicationGlobal%22%3Afalse%2C%22locationType%22%3A%22auto%22%2C%22modulePrefix%22%3A%22app%22%2C%22path%22%3A%22%2Fhello%22%2C%22rootURL%22%3A%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>

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

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) Clone

func (a *App) Clone() *App

Clone will make a copy of the application.

func (*App) MustSet added in v0.1.1

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

MustSet will call Set and panic on errors.

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{}) error

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