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

Building Modern Web Apps in Go Part 1

David Nix
September 24, 2015

Building Modern Web Apps in Go Part 1

This was a presentation I gave at the Denver Go Meetup in September 2015.

My goal is to teach others and myself how to build a robust, modern web application in Go. My hope is that startups will turn to Go instead of Rails or Django when building web apps.

Disclaimer: This is an exploration. I am not an expert on the subject. I’ve built microservices in Go but have yet to build a monolith similar to Rails or Django.

David Nix

September 24, 2015
Tweet

More Decks by David Nix

Other Decks in Programming

Transcript

  1. Building Modern Web Apps with Go Part 1 of a

    lot A Journey not a Destination
  2. About Me — Lead Developer with CirrusMD — Skilled iOS

    developer — Experienced backend developer — (Ruby on Rails with Postgres) — Side project Go developer — Pretend Android developer — Hold-a-gun-to-my-head Javascript developer
  3. More about me... — Serial Startup Engineer — Type safety

    and leaning on the compiler — I ♥ interfaces — Inheritance is the devil — Patterns and principles not frameworks
  4. Personal Experiment Heroku free-tier + hobby PostgreSQL database Return an

    array of 1000 1-dimensional JSON objects from database Result: 50x throughput vs. Rails
  5. Parse.com Push backend 50k connections per node to 1.5 million

    connections per node1 Integration test suite 25 minutes to 2 minutes1 Full deploy 30 minutes to 3 minutes1 1 http://blog.parse.com/learn/how-we-moved-our-api-from-ruby-to-go-and-saved-our- sanity/
  6. Malwarebytes Scaled to 1 million requests per minute while uploading

    payloads to S3 without a worker instance2 (ie. no Sidekiq, Delayed Job, Redis, etc. Only Go concurrency primitives) 2 http://marcio.io/2015/07/handling-1-million-requests-per-minute-with-golang/
  7. Sendgrid 140 Python servers to 1 Go Server3 3 Confirmed

    during Sept 2015 Gopher Meetup hosted by Sendgrid
  8. The Free Lunch is Over4 i.e. Hardware won't make your

    code go faster 4 http://www.gotw.ca/publications/concurrency-ddj.htm
  9. This is not a modern web app package main import

    "net/http" func main() { http.HandleFunc("/", hello) http.ListenAndServe(":8080", nil) } func hello(w http.ResponseWriter, r *http.Request) { w.Write([]byte("hello, world!")) }
  10. Some components of a modern web app Routing, Middleware, Request

    Scoped Data, Authentication, Interfacing with a database, Asset Pipeline, Templates/Views, Static files, Graceful shutdown, Transactional Email
  11. What's covered in this talk Building a Web App in

    Go — Project structure — Routing — Middleware
  12. Not covered For future talks Request Scoped Data, Authentication, Domain

    Models, Asset Pipeline, Templates/Views, Static files, Graceful shutdown, Transactional Email
  13. class SomeController < SomeBaseController # Or maybe it's in the

    base class include Module1 include Module2 before_action :do_something_fun # ... end
  14. class SomeController < SomeBaseController include Module1 # Er, maybe it's

    in one of these modules? include Module2 before_action :do_something_fun # ... end
  15. Or in the base class's superclass? Or the superclass's superclass?

    Or in a module mixed into the superclass?
  16. Go's Solution type Middleware func(h http.Handler) http.Handler A function which

    returns a new handler that wraps the injected handler.
  17. func MyMiddleware(h http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request)

    { // Do stuff here h.ServeHTTP(w, r) // Then do stuff here aka around filter }) }
  18. func MyMiddleware(h http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request)

    { defer func() { // guarantee stuff gets done at the very end }() h.ServeHTTP(w, r) }) }
  19. Quest for a Go web framework — Revel and Beego

    - too complicated, too much like Rails — Stop it with controllers. Handlers are just functions. — Wrong question. Libraries not frameworks.
  20. Phoenix Framework Written by a Rails contributer in Elixer —

    Great concurrency — Functional, dynamic language — Runs on Erlang VM — Rails-like api, Elixer has Ruby-like syntax — Pipe operator is really cool