Golang: Web development using Gorilla Mux and Pongo2 templating engine


What is Golang Gorilla ?

Gorilla is a web toolkit for the Go programming language.

What is Gorilla mux?

The name mux stands for “HTTP request multiplexer”. Like the standard http.ServeMux, mux.Router matches incoming requests against a list of registered routes and calls a handler for the route that matches the URL or other conditions.

What is HTTP Multiplexer / Mux?

ListenAndServe starts an HTTP server with a given address and handler. The handler is usually nil, which means to use DefaultServeMux. Handle and HandleFunc add handlers to DefaultServeMux

What is Pongo2 ?

pongo2 is the successor of pongo, a Django-syntax like templating-language.

pongo is a well-tested template engine which implements a Django-template-like syntax.

Install Gorilla Mux and Pongo

go get github.com/gorilla/mux
go get -u github.com/flosch/pongo2

Create app.yml

  runtime: go
  api_version: go1 

  handlers:
  - url: /.*
    script: _go_app
    secure: always
    redirect_http_response_code: 301 

create main.go

 package main

  import (
      "net/http"

      pongo "github.com/flosch/pongo2"
      "github.com/gorilla/mux"
  )

  func init() {
      r := mux.NewRouter()

      r.HandleFunc("/", homeHandler)  

      http.Handle("/", r)
  }

  func homeHandler(w http.ResponseWriter, r *http.Request) {
      template := "dashboard.html"
      tmpl := pongo.Must(pongo.FromFile(template))

      err := tmpl.ExecuteWriter(pongo.Context{"title": "Index", "greating": "Hallo"}, w)
      if err != nil {
          http.Error(w, err.Error(), http.StatusInternalServerError)
      }
  }

create base.html

 <html>
      <head>
          <title>
              {% block title %}Website Title Default{% endblock %}
          </title>
      </head>
      <body>
          {% block body %}Body Default{% endblock %}
      </body>
  </html>

create dashboard.html

  {% extends "base.html" %}

  {% block title  %}Dashboard Real Title{% endblock %}

  {% block body %}Hallo world Gorilla!{% endblock %}

Running the code on the local server


dev_appserver.py --port=9999 ./

wakakakak1

bowser1

 

Note: I am using App Engine local environment development

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s