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!");` const appCSS = `.image { background: url(/images/image.png); }` var files = map[string]string{ "index.html": indexHTML, "script.js": scriptJS, "app.css": appCSS, } 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, length := fetch("http://0.0.0.0:4242/hello") fmt.Println("==>", typ) fmt.Println("==>", length) fmt.Println(unIndent(index)) // get asset asset, typ, length := fetch("http://0.0.0.0:4242/script.js") fmt.Println("==>", typ) fmt.Println("==>", length) fmt.Println(unIndent(asset)) }
Output: ==> text/html; charset=utf-8 ==> 1504 <!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 ==> 22 alert("Hello World!");
Index ¶
- func Files(f fs.FS, dir string) (map[string]string, error)
- func MustFiles(f fs.FS, dir string) map[string]string
- type App
- func (a *App) AddFile(name string, contents string)
- func (a *App) AddInlineScript(js string)
- func (a *App) AddInlineStyle(css string)
- func (a *App) AppendBody(tag string)
- func (a *App) AppendHead(tag string)
- func (a *App) Clone() *App
- func (a *App) Config() map[string]interface{}
- func (a *App) File(path string) []byte
- func (a *App) Get(name string) interface{}
- func (a *App) Handler(configure func(*App, *http.Request)) http.Handler
- func (a *App) IsAsset(path string) bool
- func (a *App) IsPage(path string) bool
- func (a *App) Name() string
- func (a *App) Prefix(prefix string, dirs []string, fixCSS bool)
- func (a *App) PrependHead(tag string)
- func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (a *App) Set(name string, value interface{})
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type App ¶
type App struct {
// contains filtered or unexported fields
}
App is an in-memory representation of an Ember.js application.
func Create ¶
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
MustCreate will call Create and panic on errors.
func (*App) AddInlineScript ¶ added in v0.3.0
AddInlineScript will append the provides JS at the end of the body tag.
func (*App) AddInlineStyle ¶ added in v0.3.0
AddInlineStyle will append the provided CSS at the end of the head tag.
func (*App) AppendBody ¶ added in v0.3.0
AppendBody will append the provided tag to the body tag.
func (*App) AppendHead ¶ added in v0.3.0
AppendHead will append the provided tag to the head tag.
func (*App) Handler ¶ added in v0.3.0
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
IsAsset will return whether the provided path matches an asset.
func (*App) Prefix ¶ added in v0.3.2
Prefix will change the root URL and prefix all assets paths with the specified prefix. The app must be served with http.StripPrefix() to work correctly. If fixCSS is set to true, the app will also prefix all CSS url() paths. If dirs is empty or nil, the default "assets" directory will be used.
func (*App) PrependHead ¶ added in v0.5.0
PrependHead will prepend the provided tag to the head tag.