├── .gitignore
├── README.md
├── tutorial_1
├── README.md
├── main.go
└── web
│ └── test.html
├── tutorial_2
├── README.md
├── main.go
└── web
│ ├── chat.html
│ └── test.html
└── tutorial_3
├── README.md
├── main.go
└── web
├── books.html
├── books_design.html
└── test.html
/.gitignore:
--------------------------------------------------------------------------------
1 | *.exe
2 | .idea
3 |
4 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | webapp tutorial
2 | ======================
3 |
4 | This tutorial series will help you get a web application up and running using the following technologies:
5 |
6 | * Go for developing the backend
7 | * Angularjs for developing the frontend
8 | * Bootstrap and Font-Awesome for page layout and icons
9 |
10 | Click on each of the folders above to view the code and READMEs for each tutorial.
11 |
--------------------------------------------------------------------------------
/tutorial_1/README.md:
--------------------------------------------------------------------------------
1 | Tutorial 1
2 | ==========
3 |
4 | In this first tutorial we get up and running with a basic web server that
5 | serves any files in the local directory.
6 |
7 | How to run
8 | -------------
9 |
10 | * Install Go
11 | * http://golang.org/doc/install
12 | * Download this project from github and unzip
13 | * https://github.com/jakecoffman/golang-webapp-tutorial/archive/master.zip
14 | * Open a command window and `cd` to the tutorial_1 directory
15 | * Type `go run main.go` to start the server
16 | * Open a web browser and go to http://localhost/
17 |
18 | If you get a message that says "It works!" then it's working.
19 |
--------------------------------------------------------------------------------
/tutorial_1/main.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "flag"
5 | "fmt"
6 | "log"
7 | "net/http"
8 | )
9 |
10 | func main() {
11 | // command line flags
12 | port := flag.Int("port", 80, "port to serve on")
13 | dir := flag.String("directory", "web/", "directory of web files")
14 | flag.Parse()
15 |
16 | // handle all requests by serving a file of the same name
17 | fs := http.Dir(*dir)
18 | fileHandler := http.FileServer(fs)
19 | http.Handle("/", fileHandler)
20 |
21 | log.Printf("Running on port %d\n", *port)
22 |
23 | addr := fmt.Sprintf("127.0.0.1:%d", *port)
24 | // this call blocks -- the progam runs here forever
25 | err := http.ListenAndServe(addr, nil)
26 | fmt.Println(err.Error())
27 | }
28 |
--------------------------------------------------------------------------------
/tutorial_1/web/test.html:
--------------------------------------------------------------------------------
1 |
2 |
3 | Tutorial
4 |
5 |
6 |
7 |
8 |
9 |
Bootstrap's working
10 |
11 | So is font-awesome.
12 |
13 |
14 |
15 |
16 |
Is Angular working?
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/tutorial_2/README.md:
--------------------------------------------------------------------------------
1 | Tutorial 1
2 | ==========
3 |
4 | In this first tutorial we get up and running with a basic web server that
5 | serves any files in the local directory.
6 |
7 | How to run
8 | -------------
9 |
10 | * Install Go
11 | * http://golang.org/doc/install
12 | * Download this project from github and unzip
13 | * https://github.com/jakecoffman/golang-webapp-tutorial/archive/master.zip
14 | * Open a command window and `cd` to the tutorial_1 directory
15 | * Type `go run main.go` to start the server
16 | * Open a web browser and go to http://localhost/
17 |
18 | If you get a message that says "It works!" then it's working.
19 |
--------------------------------------------------------------------------------
/tutorial_2/main.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "flag"
5 | "fmt"
6 | "github.com/gorilla/websocket"
7 | "log"
8 | "net/http"
9 | )
10 |
11 | var connections map[*websocket.Conn]bool
12 |
13 | func sendAll(msg []byte) {
14 | for conn := range connections {
15 | if err := conn.WriteMessage(websocket.TextMessage, msg); err != nil {
16 | delete(connections, conn)
17 | conn.Close()
18 | }
19 | }
20 | }
21 |
22 | func wsHandler(w http.ResponseWriter, r *http.Request) {
23 | // Taken from gorilla's website
24 | conn, err := websocket.Upgrade(w, r, nil, 1024, 1024)
25 | if _, ok := err.(websocket.HandshakeError); ok {
26 | http.Error(w, "Not a websocket handshake", 400)
27 | return
28 | } else if err != nil {
29 | log.Println(err)
30 | return
31 | }
32 | log.Println("Succesfully upgraded connection")
33 | connections[conn] = true
34 |
35 | for {
36 | // Blocks until a message is read
37 | _, msg, err := conn.ReadMessage()
38 | if err != nil {
39 | delete(connections, conn)
40 | conn.Close()
41 | return
42 | }
43 | log.Println(string(msg))
44 | sendAll(msg)
45 | }
46 | }
47 |
48 | func main() {
49 | // command line flags
50 | port := flag.Int("port", 80, "port to serve on")
51 | dir := flag.String("directory", "web/", "directory of web files")
52 | flag.Parse()
53 |
54 | connections = make(map[*websocket.Conn]bool)
55 |
56 | // handle all requests by serving a file of the same name
57 | fs := http.Dir(*dir)
58 | fileHandler := http.FileServer(fs)
59 | http.Handle("/", fileHandler)
60 | http.HandleFunc("/ws", wsHandler)
61 |
62 | log.Printf("Running on port %d\n", *port)
63 |
64 | addr := fmt.Sprintf("127.0.0.1:%d", *port)
65 | // this call blocks -- the progam runs here forever
66 | err := http.ListenAndServe(addr, nil)
67 | fmt.Println(err.Error())
68 | }
69 |
--------------------------------------------------------------------------------
/tutorial_2/web/chat.html:
--------------------------------------------------------------------------------
1 |
2 |
3 | Tutorial
4 |
5 |
6 |
12 |
13 |
14 |