go-start @ Google DevFest Vienna 2012

F26b047fe2705816b840cf529bd8fc54?s=47 Erik Unger
November 10, 2012

go-start @ Google DevFest Vienna 2012

F26b047fe2705816b840cf529bd8fc54?s=128

Erik Unger

November 10, 2012
Tweet

Transcript

  1. go-start A high level web-framework for Go Mittwoch, 14. November

    12
  2. 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
  3. 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
  4. 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
  5. All batteries included • HTML5 Boilerplate • jQuery • External

    dependencies fetched automatically: go get -u github.com/ungerik/go-start Mittwoch, 14. November 12
  6. Views Mittwoch, 14. November 12

  7. 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
  8. Higher level abstractions •view.List •view.Table •view.Form •view.ModelView •view.If •view.Menu •view.Video

    •view.Page Mittwoch, 14. November 12
  9. HTML Tags / Shortcuts •H1, H2, ... •P •Pre •A,

    A_blank •Img •Br •B, I, Q •Em, Strong •Ul, Ol Mittwoch, 14. November 12
  10. So how does a view look like? Mittwoch, 14. November

    12
  11. 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
  12. Yes, templates are supported Mittwoch, 14. November 12

  13. But I ended up using them only for the HTML5

    Boilerplate Page template Mittwoch, 14. November 12
  14. Dynamic Views Mittwoch, 14. November 12

  15. 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
  16. HTML Pages Mittwoch, 14. November 12

  17. 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
  18. URL structure Mittwoch, 14. November 12

  19. 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
  20. Models Mittwoch, 14. November 12

  21. 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
  22. FormModel Mittwoch, 14. November 12

  23. 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
  24. 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
  25. The End Questions? • erik@erikunger.com • Twitter: @ungerik • Skype:

    ungerik Mittwoch, 14. November 12