Upgrade to Pro — share decks privately, control downloads, hide ads and more …

go-start @ Google DevFest Vienna 2012

Erik Unger
November 10, 2012

go-start @ Google DevFest Vienna 2012

Erik Unger

November 10, 2012
Tweet

More Decks by Erik Unger

Other Decks in Programming

Transcript

  1. Goals • Create a high level web-framework for Go, like

    Django for Python or Rails for Ruby • Be Go-ish • Don’t make stuff more complicated than it has to be • Convention over configuration • Easy setup and deployment Mittwoch, 14. November 12
  2. Current status • In development for over a year •

    Work sponsored by STARTeurope • Used in production for http://startuplive.in • Go v1.0 Mittwoch, 14. November 12
  3. What’s in it? • MVC • Prefer Go syntax to

    template languages • HTML5 Boilerplate out of the box • MongoDB • All batteries included Mittwoch, 14. November 12
  4. All batteries included • HTML5 Boilerplate • jQuery • External

    dependencies fetched automatically: go get -u github.com/ungerik/go-start Mittwoch, 14. November 12
  5. Philosophy • Why learn another template language if you can

    use Go syntax? • DOM tree has a 1:1 Go object representation on the server • Every element has an ID to enable sync of DOM tree and server view representation Mittwoch, 14. November 12
  6. HTML Tags / Shortcuts •H1, H2, ... •P •Pre •A,

    A_blank •Img •Br •B, I, Q •Em, Strong •Ul, Ol Mittwoch, 14. November 12
  7. view := Views{ ! DIV("myclass", ! ! H1("Example HTML structure"),

    ! ! P("This is a paragraph"), ! ! P( ! ! ! HTML("Some unescaped HTML:<br/>"), ! ! ! Printf("The number of the beast: %d", 666), ! ! ! Escape("Will be escaped: 666 < 999"), ! ! ), ! ! A_blank("http://go-lang.org", "A very simple link"), ! ), ! Hr(), ! PRE("! <- pre formated text, followed by a list:"), ! UL("red", "green", "blue"), ! &Template{ ! ! Filename: "mytemplate.html", ! ! GetContext: func(ctx *Context) (interface{}, error) { ! ! ! return map[string]string{"Key": "Value"}, nil ! ! }, ! }, } Mittwoch, 14. November 12
  8. But I ended up using them only for the HTML5

    Boilerplate Page template Mittwoch, 14. November 12
  9. view := DynamicView( ! func(ctx *Context) (View, error) { !

    ! if ctx.Request.RequestURI == "/" { ! ! ! // return nil, so nothing will be rendered ! ! ! return nil, nil ! ! } ! ! return A("/", HTML("&larr; Back to the homepage")), nil ! }, ) Mittwoch, 14. November 12
  10. Homepage := &Page{ ! OnPreRender: func(page *Page, context *Context) (err

    error) { ! ! context.Data = &PerPageData{...} // Set global page data at request context ! }, ! Title: func(context *Context, writer io.Writer) (err error) { ! ! writer.Write([]byte(context.Data.(*PerPageData).DynamicTitle)) ! ! return nil ! }, ! CSS: HomepageCSS, ! AdditionalHead: RSS("go-start.org RSS Feed", &RssFeed) ! Scripts: Renderers{ ! ! Config.Page.DefaultWriteScripts, ! ! JQuery, // jQuery/UI is built-in ! ! JQueryUI, ! ! JQueryUIAutocompleteFromURL(".select-username", IndirectURL(&API_Usernames), 2), ! ! GoogleAnalytics(GoogleAnalyticsID), // Google Analytics is built-in ! }, ! Content: Views{}, } Mittwoch, 14. November 12
  11. var ( ! CSS View ! Homepage ViewWithURL ! ...

    ) func Paths() *ViewPath { ! return &ViewPath{View: Homepage, Sub: []ViewPath{ ! ! media.ViewPath("media"), ! ! {Name: "style.css", View: CSS}, ! ! ... ! }} } Mittwoch, 14. November 12
  12. Models • Models are Go structs (marshaling via reflection) •

    Meta information for validation and display is added via tags • Forms and DB share the same model and validation mechanism • MongoDB is the default database Mittwoch, 14. November 12
  13. Additional packages • Email (message creation missing in standard mail

    package + Google Mail defaults) • Gravatar • RSS parsing • Amiando event management (used by http://startuplive.in) Mittwoch, 14. November 12
  14. Where to get it • go get github.com/ungerik/go-start • Tutorial:

    https://github.com/ungerik/go-start/tree/master/ examples/FullTutorial • Documentation: http://go.pkgdoc.org/github.com/ungerik/go-start Mittwoch, 14. November 12