updates
This commit is contained in:
parent
e91edf0194
commit
0de3674882
@ -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
|
|
14
069-http-server-static/http-server-static.go
Normal file
14
069-http-server-static/http-server-static.go
Normal 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
|
5
069-http-server-static/http-server-static.sh
Normal file
5
069-http-server-static/http-server-static.sh
Normal 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
|
@ -1,6 +1,9 @@
|
|||||||
|
// ## HTTP Server Status Code
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import ("net/http"; "fmt")
|
import "net/http"
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
func hello(res http.ResponseWriter, req *http.Request) {
|
func hello(res http.ResponseWriter, req *http.Request) {
|
||||||
res.Header().Set("Content-Type", "text/plain")
|
res.Header().Set("Content-Type", "text/plain")
|
||||||
@ -16,9 +19,3 @@ func main() {
|
|||||||
http.HandleFunc("/", hello)
|
http.HandleFunc("/", hello)
|
||||||
http.ListenAndServe(":5000", nil)
|
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
|
|
5
070-http-server-status-code/http-server-status-code.sh
Normal file
5
070-http-server-status-code/http-server-status-code.sh
Normal 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
|
@ -1,3 +1,5 @@
|
|||||||
|
// ## HTTP Server
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "net/http"
|
import "net/http"
|
@ -1,6 +1,11 @@
|
|||||||
|
// ## HTTPS Client
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import ("net/http"; "crypto/tls"; "io/ioutil"; "fmt")
|
import "net/http"
|
||||||
|
import "crypto/tls"
|
||||||
|
import "io/ioutil"
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
tr := &http.Transport{
|
tr := &http.Transport{
|
@ -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/
|
|
16
073-https-server/https-server.go
Normal file
16
073-https-server/https-server.go
Normal 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) }
|
||||||
|
}
|
11
073-https-server/https-server.sh
Normal file
11
073-https-server/https-server.sh
Normal 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/
|
@ -1,6 +1,9 @@
|
|||||||
|
// JSON
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import ("encoding/json"; "fmt")
|
import "encoding/json"
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// data to bytes/string
|
// data to bytes/string
|
@ -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
|
|
||||||
*/
|
|
30
075-number-parsing/number-parsing.go
Normal file
30
075-number-parsing/number-parsing.go
Normal 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)
|
||||||
|
}
|
6
075-number-parsing/number-parsing.sh
Normal file
6
075-number-parsing/number-parsing.sh
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
$ go run xx-number-parsing.go
|
||||||
|
1.234
|
||||||
|
123
|
||||||
|
6974
|
||||||
|
456
|
||||||
|
strconv.ParseInt: parsing "wat": invalid syntax
|
Loading…
x
Reference in New Issue
Block a user