Share
Embed
Developing Web Applications with Go
Developing Web Applications with Go.
Sau Sheong Chang
August 19, 2014
Transcript
152 slides
-
Browser/Client Server HTTP Request • Request line GET /some/index.html HTTP/1.1
• Request headers • Empty line -
Browser/Client Server HTTP Request • Request line GET /some/index.html HTTP/1.1
• Request headers • Empty line • Message body (optional) -
Browser/Client Server HTTP Request HTTP Response • Request line GET
/some/index.html HTTP/1.1 • Request headers • Empty line • Message body (optional) -
Browser/Client Server HTTP Request HTTP Response • Request line GET
/some/index.html HTTP/1.1 • Request headers • Empty line • Message body (optional) • Status line HTTP/1.1 200 OK -
Browser/Client Server HTTP Request HTTP Response • Request line GET
/some/index.html HTTP/1.1 • Request headers • Empty line • Message body (optional) • Status line HTTP/1.1 200 OK • Response headers -
Browser/Client Server HTTP Request HTTP Response • Request line GET
/some/index.html HTTP/1.1 • Request headers • Empty line • Message body (optional) • Status line HTTP/1.1 200 OK • Response headers • Empty line -
Browser/Client Server HTTP Request HTTP Response • Request line GET
/some/index.html HTTP/1.1 • Request headers • Empty line • Message body (optional) • Status line HTTP/1.1 200 OK • Response headers • Empty line • Message body (optional) -
Server HTTP Request HTTP Response 1.Process request 2.Execute application
logic 3.Generate response -
Parts of a web app • Routes Processing the request
• Templates Generating response • Store Persisting data -
net/http • Server -‐ ServeMux(type), Handler(type), Handle(func), HandleFunc(func),
ListenAndServe(func), Serve(func) 10 -
net/http • Server -‐ ServeMux(type), Handler(type), Handle(func), HandleFunc(func),
ListenAndServe(func), Serve(func) • Client -‐ Get, Post, Head etc (func) 10 -
net/http • Server -‐ ServeMux(type), Handler(type), Handle(func), HandleFunc(func),
ListenAndServe(func), Serve(func) • Client -‐ Get, Post, Head etc (func) • Request, Response, ResponseWriter (type) 10 -
net/http • Server -‐ ServeMux(type), Handler(type), Handle(func), HandleFunc(func),
ListenAndServe(func), Serve(func) • Client -‐ Get, Post, Head etc (func) • Request, Response, ResponseWriter (type) • Cookie(type) 10 -
net/http • Server -‐ ServeMux(type), Handler(type), Handle(func), HandleFunc(func),
ListenAndServe(func), Serve(func) • Client -‐ Get, Post, Head etc (func) • Request, Response, ResponseWriter (type) • Cookie(type) • Redirect(func) 10 -
Routes package main! ! import (! "fmt"! "net/http"! )! !
func handler(w http.ResponseWriter, r *http.Request) {! fmt.Fprintf(w, "Hello World, %s!", r.URL.Path[1:])! }! ! func main() {! http.HandleFunc("/", handler)! http.ListenAndServe(":8080", nil)! }! -
Routes package main! ! import (! "fmt"! "net/http"! )! !
func main() {! mux := http.NewServeMux()! mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {! fmt.Fprintf(w, "Hello World, %s!", r.URL.Path[1:])! })! http.ListenAndServe(":8080", mux)! }! -
Routes package main! ! import (! "fmt"! "net/http"! )! !
type MyHandler struct {}! ! func (h *MyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {! fmt.Fprintf(w, "Hello World, %s!", r.URL.Path[1:])! }! ! func main() {! http.ListenAndServe(":8080", &MyHandler{})! }! -
Routes type MyHandler struct {}! ! func (h *MyHandler) ServeHTTP(w
http.ResponseWriter, r *http.Request) {! fmt.Fprintf(w, "Hello World, %s!", r.URL.Path[1:])! }! ! func main() {! server := &http.Server{! ! Addr: "0.0.0.0:8080",! ! Handler: &MyHandler{},! ! ReadTimeout: 10 * time.Second,! ! WriteTimeout: 600 * time.Second,! ! MaxHeaderBytes: 1 << 20,! }! server.ListenAndServe() ! } -
Routes package main! ! import (! "fmt"! "net/http"! "github.com/julienschmidt/httprouter"! )!
! func main() {! router := httprouter.New() ! router.GET("/:name", hello) ! http.ListenAndServe(":8080", router)! }! ! func hello(w http.ResponseWriter, r *http.Request, p httprouter.Params) {! fmt.Fprintf(w, "Hello World, %s!", p.ByName("name"))! }! -
Routes package main! ! import (! "fmt"! "net/http"! "github.com/julienschmidt/httprouter"! )!
! func main() {! router := httprouter.New() ! router.GET("/:name", hello) ! http.ListenAndServe(":8080", router)! }! ! func hello(w http.ResponseWriter, r *http.Request, p httprouter.Params) {! fmt.Fprintf(w, "Hello World, %s!", p.ByName("name"))! }! Not part of standard Go libraries -
Static assets? • Images • Stylesheets (CSS) •
Javascript • Fonts -
Serve Files func main() {! router := httprouter.New() ! router.ServeFiles("/images/*filepath",
! http.Dir("public/images"))! router.ServeFiles("/css/*filepath", ! http.Dir("public/css"))! router.ServeFiles(“/fonts/*filepath",! http.Dir("public/fonts"))! router.ServeFiles("/js/*filepath", ! http.Dir("public/js"))! http.ListenAndServe(":8080", router)! }! ! -
Templates 21 Hello World! This is { name }
name = “Sau Sheong” Hello World! This is Sau Sheong + -
Templates • text/template -‐ data driven templates • html/template -‐
data driven templates for HTML -
Templates • text/template -‐ data driven templates • html/template -‐
data driven templates for HTML • Actions (data evaluations or control structure) -‐ delimited by {{ and }} -
Templates • text/template -‐ data driven templates • html/template -‐
data driven templates for HTML • Actions (data evaluations or control structure) -‐ delimited by {{ and }} • Arguments -‐ values/variables in the template -
Templates • text/template -‐ data driven templates • html/template -‐
data driven templates for HTML • Actions (data evaluations or control structure) -‐ delimited by {{ and }} • Arguments -‐ values/variables in the template • Pass variables to template, accessible through the ‘dot’ {{ . }} -
Templates • text/template -‐ data driven templates • html/template -‐
data driven templates for HTML • Actions (data evaluations or control structure) -‐ delimited by {{ and }} • Arguments -‐ values/variables in the template • Pass variables to template, accessible through the ‘dot’ {{ . }} • Can define variables (start with $) -
Templates • Pipeline -‐ a chained sequence of commands •
Function -‐ global functions (predefined) or custom functions -
Templates • Pipeline -‐ a chained sequence of commands •
Function -‐ global functions (predefined) or custom functions • Include other templates with keyword ‘template’ -
Generating response 1. Set {{ define <template name> }} in
the template 2. Allocate the template (in the route) using New() -
Generating response 1. Set {{ define <template name> }} in
the template 2. Allocate the template (in the route) using New() 3. Create the template by parsing one or more files using Parse(), ParseFiles() or ParseGlob() -
Generating response 1. Set {{ define <template name> }} in
the template 2. Allocate the template (in the route) using New() 3. Create the template by parsing one or more files using Parse(), ParseFiles() or ParseGlob() 4. Execute the template, passing the output writer and the argument (or nil if none) -
t1.html 25 {{ define "t1" }}! ! <!DOCTYPE html>! <html>!
{{ template "header" }}! <body>! Hello World, {{ . }}!! </body>! </html>! ! {{ end }}! -
header.html 26 {{ define "header" }}! <head>! <meta http-equiv="Content-Type" content="text/html;
charset=utf-8">! <title>Template Example</title>! </head>! {{ end }}! -
templates.go 27 package main! ! import (! "os"! "html/template"! )!
! func main() {! t := template.New("t1")! t = template.Must(t.ParseGlob("*.html")) ! t.Execute(writer, "Sau Sheong")! }! -
templates.go 27 package main! ! import (! "os"! "html/template"! )!
! func main() {! t := template.New("t1")! t = template.Must(t.ParseGlob("*.html")) ! t.Execute(writer, "Sau Sheong")! }! Allocate -
templates.go 27 package main! ! import (! "os"! "html/template"! )!
! func main() {! t := template.New("t1")! t = template.Must(t.ParseGlob("*.html")) ! t.Execute(writer, "Sau Sheong")! }! Create -
templates.go 27 package main! ! import (! "os"! "html/template"! )!
! func main() {! t := template.New("t1")! t = template.Must(t.ParseGlob("*.html")) ! t.Execute(writer, "Sau Sheong")! }! Execute -
templates.go 27 package main! ! import (! "os"! "html/template"! )!
! func main() {! t := template.New("t1")! t = template.Must(t.ParseGlob("*.html")) ! t.Execute(writer, "Sau Sheong")! }! -
Response 28 <!DOCTYPE html>! <html>! ! <head>! <meta http-equiv="Content-Type" content="text/html;
charset=utf-8">! <title>Template Example</title>! </head>! ! <body>! Hello World, Sau Sheong!! </body>! </html> -
Response 28 <!DOCTYPE html>! <html>! ! <head>! <meta http-equiv="Content-Type" content="text/html;
charset=utf-8">! <title>Template Example</title>! </head>! ! <body>! Hello World, Sau Sheong!! </body>! </html> from header.html -
Response 28 <!DOCTYPE html>! <html>! ! <head>! <meta http-equiv="Content-Type" content="text/html;
charset=utf-8">! <title>Template Example</title>! </head>! ! <body>! Hello World, Sau Sheong!! </body>! </html> from header.html replaces {{ . }} -
templates.go 29 type Presentation struct {! Title string! Author string!
}! ! func main() {! t := template.New("t2")! t = template.Must(t.ParseGlob("*.html")) ! presso := Presentation{! Title: "Write Web Applications with Go",! Author: "Chang Sau Sheong",! }! t.Execute(writer, presso)! }! -
t2.html 30 {{ define "t2" }}! ! <!DOCTYPE html>! <html>!
{{ template "header" }}! <body>! <h1>{{ .Title }}</h1>! <h3>{{ .Author}}</h3>! </body>! </html>! ! {{ end }}! -
Response 31 <!DOCTYPE html>! <html>! ! <head>! <meta http-equiv="Content-Type" content="text/html;
charset=utf-8">! <title>Template Example</title>! </head>! ! <body>! <h1>Write Web Applications with Go</h1>! <h3>Chang Sau Sheong</h3>! </body>! </html>! -
SQL • Direct SQL statements • Mappers (ORMs) • gorp
• hood • beego orm -
SQL • Direct SQL statements • Mappers (ORMs) • gorp
• hood • beego orm • gorm -
35 package main! ! import(! "log"! "fmt" ! "database/sql"! _
"github.com/lib/pq"! )! ! func main() {! db, _ := sql.Open("postgres", "dbname=blog sslmode=disable”)! defer db.Close() ! ! rows, err := db.Query("SELECT name, email FROM users WHERE email = $1", “[email protected]”); if err != nil {! log.Fatal(err)! }! for rows.Next() {! var name, email string! if err := rows.Scan(&name, &email); err != nil {! log.Fatal(err)! }! fmt.Printf("%s - %s\n", name, email)! }! }! -
Define models 38 type User struct {! Id int64! Uuid
string `sql:"size:255;not null;unique"`! Email string `sql:"size:255"`! Password string `sql:"size:255"`! Name string `sql:"size:255"`! CreatedAt time.Time! }! ! type Session struct {! Id int64! Uuid string `sql:"size:255;not null;unique"`! UserId int64 ! CreatedAt time.Time! } // foreign key for User table -
Initialize database 39 var DB gorm.DB! ! // initialize gorm!
func init() {! var err error! DB, err = gorm.Open("postgres", "user=test password=test dbname=test sslmode=disable")! if err != nil {! panic(fmt.Sprintf("Got error when connect database, the error is '%v'", err))! }! migrate()! } -
Migrate tables 40 // Create tables! func migrate() {! DB.Exec("DROP
TABLE users;DROP TABLE sessions;")! DB.AutoMigrate(User{})! DB.AutoMigrate(Session{})! } -
Create 41 user := User{Name: "Sau Sheong", ! Email: "[email protected]",
! Password: “passw0rd”,! }! DB.Save(&user) ! ! -
Query 42 var user = User{}! // Get the first
record! err := DB.Where("email = ?", ! "[email protected]").First(&user).Error! if err != nil {! fmt.Println("Cannot retrieve this user:", err)! } ! ! // Get the multiple records! var users []User! err := DB.Where("email LIKE ?", ! "%@gmail.com").Find(&users).Error! if err != nil {! fmt.Println("Cannot retrieve users:", err)! } -
Update & Delete 43 var user = User{}! // Get
the first record! err := DB.Where("email = ?", ! "[email protected]").First(&user).Error! if err != nil {! fmt.Println("Cannot retrieve this user:", err)! } ! ! // Update the name! user.Name = "Batman"! DB.Save(&user)! ! // Delete the record! DB.Delete(&user)! -
Callbacks 44 // Before creating a user, add in the
uuid! func (u *User) BeforeCreate() (err error) {! ! ! u.Password = encrypt(u.Password)! ! u.Uuid = createUUID()! ! return! } -
Associations 45 user := User{Name: "Sau Sheong", ! Email: "[email protected]",
! Password: "test",! }! DB.Save(&user) ! ! sess := Session{UserId: user.Id}! DB.Save(&sess)! ! var usr User! err := DB.Model(&sess).Related(&usr).Error! if err != nil {! fmt.Println("Cannot retrieve this user:", err)! } ! ! var session Session! err := DB.Model(&user).Related(&session).Error! if err != nil {! fmt.Println("Cannot retrieve this session:", err)! } -
Associations 45 user := User{Name: "Sau Sheong", ! Email: "[email protected]",
! Password: "test",! }! DB.Save(&user) ! ! sess := Session{UserId: user.Id}! DB.Save(&sess)! ! var usr User! err := DB.Model(&sess).Related(&usr).Error! if err != nil {! fmt.Println("Cannot retrieve this user:", err)! } ! ! var session Session! err := DB.Model(&user).Related(&session).Error! if err != nil {! fmt.Println("Cannot retrieve this session:", err)! } Retrieve user given the session -
Associations 45 user := User{Name: "Sau Sheong", ! Email: "[email protected]",
! Password: "test",! }! DB.Save(&user) ! ! sess := Session{UserId: user.Id}! DB.Save(&sess)! ! var usr User! err := DB.Model(&sess).Related(&usr).Error! if err != nil {! fmt.Println("Cannot retrieve this user:", err)! } ! ! var session Session! err := DB.Model(&user).Related(&session).Error! if err != nil {! fmt.Println("Cannot retrieve this session:", err)! } Retrieve session given the user -
Associations 45 user := User{Name: "Sau Sheong", ! Email: "[email protected]",
! Password: "test",! }! DB.Save(&user) ! ! sess := Session{UserId: user.Id}! DB.Save(&sess)! ! var usr User! err := DB.Model(&sess).Related(&usr).Error! if err != nil {! fmt.Println("Cannot retrieve this user:", err)! } ! ! var session Session! err := DB.Model(&user).Related(&session).Error! if err != nil {! fmt.Println("Cannot retrieve this session:", err)! } -
Other features • Query chains • Batch updates and deletes
• Soft delete • Associations -
Other features • Query chains • Batch updates and deletes
• Soft delete • Associations • Has One -
Other features • Query chains • Batch updates and deletes
• Soft delete • Associations • Has One • Belongs To -
Other features • Query chains • Batch updates and deletes
• Soft delete • Associations • Has One • Belongs To • Has Many -
Other features • Query chains • Batch updates and deletes
• Soft delete • Associations • Has One • Belongs To • Has Many • Many to Many -
Other features • FirstOrInit, FirstOrCreate • Order, Limit, Offset, Count
• Group, Having, Joins -
Other features • FirstOrInit, FirstOrCreate • Order, Limit, Offset, Count
• Group, Having, Joins • Transactions -
Other features • FirstOrInit, FirstOrCreate • Order, Limit, Offset, Count
• Group, Having, Joins • Transactions • Logger -
Frameworks • Gorilla web toolkit • Martini •
Gin Gonic • Beego • Revel -
> go build > go test Compile Go code Run
tests on Go code -
Unit Testing 55 package main! ! import( ! "testing"! )!
! // Test package function that adds a new user! func TestAddUser(t *testing.T) {! addUser("Sau Sheong", "[email protected]", "password")! if val, ok := users["[email protected]"]; !ok {! t.Errorf("Cannot add user")! } else {! if val.Name != "Sau Sheong" {! t.Errorf("User name is wrong")! }! }! } -
HTTP Testing 56 package main! ! ...! ! func TestIndex(t
*testing.T) { ! router := httprouter.New()! router.GET("/", index)! writer := httptest.NewRecorder()! request, _ := http.NewRequest("GET", "/", nil) ! router.ServeHTTP(writer, request)! ! if writer.Code != 302 {! t.Errorf("Response code is %v", writer.Code)! } ! } -
HTTP Testing 57 func TestSignUp(t *testing.T) {! router := httprouter.New()!
router.POST("/signup", createUser)! writer := httptest.NewRecorder()! body := strings.NewReader("name=Sau Sheong&[email protected]&password=password")! request, _ := http.NewRequest("POST", "/signup", body)! request.Header.Add("Content-Type", "application/x-www- form-urlencoded")! router.ServeHTTP(writer, request)! ! if writer.Code != 302 {! t.Errorf("Response code is %v", writer.Code)! }! if writer.Header().Get("Location") != "/login" {! t.Errorf("Location is %v", writer.Header().Get("Location"))! }! } -
HTTP Testing 58 func TestAuthenticate(t *testing.T) {! addUser("Sau Sheong", "[email protected]",
"password")! router := httprouter.New()! router.POST("/login", authenticate) ! writer := httptest.NewRecorder()! body := strings.NewReader("[email protected]&password=pa ssword")! request, _ := http.NewRequest("POST", "/login", body)! request.Header.Add("Content-Type", "application/x-www- form-urlencoded")! router.ServeHTTP(writer, request)! ...! if !strings.HasPrefix(writer.Header().Get("Set- Cookie"), "pixelate_cookie") {! t.Errorf("Cookie not set")! }! } -
Go App Engine • Download the Go App Engine SDK
• Install Mercurial 60 -
Go App Engine • Download the Go App Engine SDK
• Install Mercurial • Add a file app.yaml to the same directory 60 -
Go App Engine • Download the Go App Engine SDK
• Install Mercurial • Add a file app.yaml to the same directory 60 application: pixelate! version: 1! runtime: go! api_version: go1! ! handlers:! - url: /.*! script: _go_app -
Go App Engine • Code changes required: • Change all
package names from main to the application name (in this case it is sausheong-pixelate) 61 -
Go App Engine • Code changes required: • Change all
package names from main to the application name (in this case it is sausheong-pixelate) • Change the main() function to init() 61 -
Go App Engine • Code changes required: • Change all
package names from main to the application name (in this case it is sausheong-pixelate) • Change the main() function to init() • Change from http.ListenAndServe() to http.Handle(mux) 61 -
Run locally • Run at command line: • Open browser
to: 62 go_appengine/dev_appserver.py sausheong-pixelate -
Run locally • Run at command line: • Open browser
to: 62 go_appengine/dev_appserver.py sausheong-pixelate http://localhost:8080 -
Deploy to Google App Engine • Create app on Google
App Engine 63 -
Deploy to Google App Engine • Create app on Google
App Engine • Run at command line: 63 go_appengine/goapp deploy sausheong-pixelate -
Deploy to Google App Engine • Create app on Google
App Engine • Run at command line: 63 go_appengine/goapp deploy sausheong-pixelate -
Deploy to Google App Engine • Create app on Google
App Engine • Run at command line: • Open browser to: 63 go_appengine/goapp deploy sausheong-pixelate -
Deploy to Google App Engine • Create app on Google
App Engine • Run at command line: • Open browser to: 63 go_appengine/goapp deploy sausheong-pixelate http://sausheong-pixelate.appspot.com -
My projects 64 • Pixelate Convert a JPEG file to
a pixelated version • TodayReader A faster, simpler Today (newspaper) reader • GoAuthServ Simple authentication service • Polyglot Multi-‐language web framework