This commit is contained in:
Mark McGranaghan 2012-09-23 12:07:35 -07:00
parent e91edf0194
commit 0de3674882
14 changed files with 103 additions and 90 deletions

View File

@ -1,21 +0,0 @@
package main
import "net/http"
func main() {
handler := http.FileServer(http.Dir("./"))
http.ListenAndServe(":5000", handler)
}
// == running
// $ cd src
// $ go run xx-http-server-static.go
//
// $ curl http://127.0.0.1:5000/
// $ curl http://127.0.0.1:5000/xx-http-server-static.go
// $ curl http://127.0.0.1:5000/missing
// == todo
// index pages
// disable listing
// custom 404 handling

View File

@ -0,0 +1,14 @@
// ## HTTP Server Static
package main
import "net/http"
func main() {
handler := http.FileServer(http.Dir("./"))
http.ListenAndServe(":5000", handler)
}
// todo: index pages
// todo: disable listing
// todo: custom 404 handling

View File

@ -0,0 +1,5 @@
$ go run http-server-static.go
$ curl http://127.0.0.1:5000/
$ curl http://127.0.0.1:5000/http-server-static.go
$ curl http://127.0.0.1:5000/missing

View File

@ -1,6 +1,9 @@
// ## HTTP Server Status Code
package main
import ("net/http"; "fmt")
import "net/http"
import "fmt"
func hello(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Content-Type", "text/plain")
@ -16,9 +19,3 @@ func main() {
http.HandleFunc("/", hello)
http.ListenAndServe(":5000", nil)
}
// running
// $ go run xx-http-server-status-code.go
//
// $ curl -i http://127.0.0.1:5000/
// $ curl -i http://127.0.0.1:5000/protected

View File

@ -0,0 +1,5 @@
$ go run xx-http-server-status-code.go
$ curl -i http://127.0.0.1:5000/ | grep HTTP
$ curl -i http://127.0.0.1:5000/protected | grep HTTP

View File

@ -1,3 +1,5 @@
// ## HTTP Server
package main
import "net/http"

View File

@ -1,6 +1,11 @@
// ## HTTPS Client
package main
import ("net/http"; "crypto/tls"; "io/ioutil"; "fmt")
import "net/http"
import "crypto/tls"
import "io/ioutil"
import "fmt"
func main() {
tr := &http.Transport{

View File

@ -1,26 +0,0 @@
package main
import "net/http"
func handler(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Content-Type", "text/plain")
res.Write([]byte("Hello from HTTPS\n"))
}
func main() {
http.HandleFunc("/", handler)
err := http.ListenAndServeTLS(":5000", "/tmp/server.crt", "/tmp/server.key", nil)
if err != nil { panic(err) }
}
// $ cd /tmp
// $ rm -f server.*
// $ openssl genrsa -des3 -out server.orig.key 2048
// $ 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
// $ curl https://127.0.0.1:5000/
// $ curl --insecure https://127.0.0.1:5000/

View File

@ -0,0 +1,16 @@
// ## HTTPS Server
package main
import "net/http"
func handler(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Content-Type", "text/plain")
res.Write([]byte("Hello from HTTPS\n"))
}
func main() {
http.HandleFunc("/", handler)
err := http.ListenAndServeTLS(":5000", "/tmp/server.crt", "/tmp/server.key", nil)
if err != nil { panic(err) }
}

View File

@ -0,0 +1,11 @@
$ cd /tmp
$ rm -f server.*
$ openssl genrsa -des3 -out server.orig.key 2048
$ 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
$ curl https://127.0.0.1:5000/
$ curl --insecure https://127.0.0.1:5000/

View File

@ -1,6 +1,9 @@
// JSON
package main
import ("encoding/json"; "fmt")
import "encoding/json"
import "fmt"
func main() {
// data to bytes/string

View File

@ -1,34 +0,0 @@
// Number parsing
package main
import (
"fmt"
"strconv" // Package `strconv` provides the parsing.
)
func main() {
f, _ := strconv.ParseFloat("1.234", 64) // `64` tells how many bits of precision to parse.
fmt.Println(f)
i, _ := strconv.ParseInt("123", 0, 64) // `0` means infer the base from the string.
println(i) // `64` requires that the result fit in 64 bits.
d, _ := strconv.ParseInt("0x1b3e", 0, 64) // `ParseInt` will recognize hex-formatted numbers.
println(d)
k, _ := strconv.Atoi("456") // `Atoi` is a convenienice function for `int` parsing.
println(k)
_, e := strconv.Atoi("wat") // Parse functions return an error on bad input.
fmt.Println(e)
}
/*
$ go run xx-number-parsing.go
1.234
123
6974
456
strconv.ParseInt: parsing "wat": invalid syntax
*/

View File

@ -0,0 +1,30 @@
// ## Number Parsing
package main
// Package `strconv` provides the number parsing.
import "strconv"
import "fmt"
func main() {
// `64` tells how many bits of precision to parse.
f, _ := strconv.ParseFloat("1.234", 64)
fmt.Println(f)
// `0` means infer the base from the string.
// `64` requires that the result fit in 64 bits.
i, _ := strconv.ParseInt("123", 0, 64)
println(i)
// `ParseInt` will recognize hex-formatted numbers.
d, _ := strconv.ParseInt("0x1b3e", 0, 64)
println(d)
// `Atoi` is a convenienice function for `int` parsing.
k, _ := strconv.Atoi("456")
println(k)
// Parse functions return an error on bad input.
_, e := strconv.Atoi("wat")
fmt.Println(e)
}

View File

@ -0,0 +1,6 @@
$ go run xx-number-parsing.go
1.234
123
6974
456
strconv.ParseInt: parsing "wat": invalid syntax