index work

This commit is contained in:
Mark McGranaghan 2012-09-23 18:06:40 -07:00
parent 6c1742a4e5
commit 1f7065e387
32 changed files with 32 additions and 132 deletions

View File

@ -1,29 +0,0 @@
// ## TCP Client
package main
import "net"
import "fmt"
func main() {
c, err := net.Dial("tcp", "127.0.0.1:5000")
if err != nil {
panic(err)
}
msg := "hello world"
fmt.Println("Sending: ", msg)
_, err = c.Write([]byte(msg))
if err != nil {
panic(err)
}
buf := make([]byte, 1024)
_, err = c.Read(buf)
if err != nil {
panic(err)
} else {
fmt.Println("Received:", string(buf))
c.Close()
}
}

View File

@ -1,3 +0,0 @@
$ go run tcp-client.go
Sending: hello world
Received: hello world

View File

@ -1,4 +1,4 @@
// ## HTTP Server
// ## Hello Web
package main
@ -6,7 +6,7 @@ import "net/http"
func hello(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Content-Type", "text/plain")
res.Write([]byte("Hello From HTTP\n"))
res.Write([]byte("Hello web\n"))
}
func main() {

View File

@ -1,4 +1,4 @@
// ## HTTP Server Status Code
// ## Responses
package main

View File

@ -1,4 +1,4 @@
$ go get github.com/bmizerany/pat
$ go run xx-http-server-routing.go
$ go run request-routing.go
$ curl -i http://127.0.0.1:5000/hello/gopher

View File

@ -1,7 +0,0 @@
$ cd src
$ go build xx-http-server-graceful-shutdown.go
$ ./xx-http-server-graceful-shutdown
$ curl -i http://127.0.0.1:5000/
^C

View File

@ -1,4 +1,4 @@
// ## HTTP Server Logging
// ## Request Logging
package main
@ -37,4 +37,4 @@ func main() {
http.ListenAndServe(":5000", nil)
}
// todo: status code?
// todo: logging status code?

View File

@ -1,4 +1,4 @@
// ## HTTP Server Static
// ## Static Content
package main

View File

@ -1,4 +1,4 @@
$ go run http-server-static.go
$ go run static-content.go
$ curl http://127.0.0.1:5000/
$ curl http://127.0.0.1:5000/http-server-static.go

View File

@ -1,4 +1,4 @@
// ## HTTP Server Basic
// ## Basic Authentication
package main

View File

@ -1,4 +1,4 @@
// ## HTTP Server Canonical Host
// ## Canonical Hosts
package main

View File

@ -1,4 +1,4 @@
$ go run xx-http-server-canonical-host.go
$ go run canonical-hosts.go
$ curl -i -L http://127.0.0.1:5000/go
$ curl -i -L http://127.0.0.1:5000/go

View File

@ -1,22 +0,0 @@
// ## HTTP Server Static Dynamic
package main
import "net/http"
func hello(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Content-Type", "text/plain")
res.Write([]byte("Hello From HTTP\n"))
}
func main() {
helloHandler := http.HandlerFunc(hello)
staticHandler := http.StripPrefix("/static/", http.FileServer(http.Dir("./")))
http.Handle("/hello", helloHandler)
http.Handle("/static/", staticHandler)
http.ListenAndServe(":5000", nil)
}
// todo: try to get dynamic at root
// todo: try to get static at root
// todo: favicon

View File

@ -1,6 +0,0 @@
$ cd src
$ go run xx-http-server-static-dynamic.go
$ curl http://127.0.0.1:5000/hello
$ curl http://127.0.0.1:5000/static
$ curl http://127.0.0.1:5000/static/01-hello.go

View File

@ -1,4 +1,4 @@
// ## HTTP Server Middleware
// ## Middleware
package main

View File

@ -1,4 +1,4 @@
// ## HTTP Serve Graceful Shutdown
// ## Graceful Shutdown
package main

View File

@ -0,0 +1,6 @@
$ go build graceful-shutdown.go
$ ./graceful-shutdown
$ curl -i http://127.0.0.1:5000/
^C

View File

@ -1,30 +0,0 @@
// ## HTTP Server Static Select
package main
import "code.google.com/p/gorilla/mux"
import "net/http"
import "fmt"
func hello(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Content-Type", "text/plain")
fmt.Fprintln(res, "Hello")
}
func static(res http.ResponseWriter, req *http.Request) {
http.ServeFile(res, req, "./01-hello.go")
}
func notFound(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Content-Type", "text/plain")
res.WriteHeader(404)
fmt.Fprintln(res, "Not found: /" + mux.Vars(req)["path"])
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/", hello)
r.HandleFunc("/01-hello.go", static)
r.HandleFunc("/{path:.*}", notFound)
http.ListenAndServe(":5000", r)
}

View File

@ -1,7 +0,0 @@
$ go get code.google.com/p/gorilla/mux
$ go run xx-http-server-static-select.go
$ curl -i http://127.0.0.1:5000/
$ curl -i http://127.0.0.1:5000/favicon.ico
$ curl -i http://127.0.0.1:5000/wat
$ curl -i http://127.0.0.1:5000/wat/wat

View File

@ -1,4 +1,4 @@
// ## HTTPS Server
// ## HTTPS Servers
package main

View File

@ -5,7 +5,7 @@ $ openssl rsa -in server.orig.key -out server.key
$ openssl req -new -key server.key -out server.csr
$ openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
$ go run src/xx-https-server.go
$ go run https-servers.go
$ curl https://127.0.0.1:5000/
$ curl --insecure https://127.0.0.1:5000/

View File

@ -93,21 +93,19 @@ mongodb ~
sending-email
## web apps
http-server-basic
http-server-canonical-host
http-server-graceful-shutdown
http-server-log
http-server-middleware
http-server-routing
http-server-static-dynamic
http-server-static-select
http-server-static
http-server-status-code
http-server
https-server
headers-and-query-params ~
hello-web
requests ~
responses
request-routing
request-logging
static-content
basic-authentication
canonical-hosts
post-bodies ~
middleware
streaming-out-http-responses ~
graceful-shutdown
https-servers
## shipping go
using-gofmt ~