Last active
December 27, 2015 01:59
-
-
Save wstucco/7248624 to your computer and use it in GitHub Desktop.
I tried Go and I liked it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net/http" | |
) | |
func main() { | |
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintf(w, "Hello World!") | |
}) | |
fmt.Println("Server running at http://localhost:8080/") | |
fmt.Println("hit CTRL+C to shut it down") | |
http.ListenAndServe(":8080", nil) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net/http" | |
) | |
// declare the new type Middleware. | |
// Note: the type can be literally **any** type | |
// type seq []int is a perfectly legal declaration | |
// it creates a new type (not an alias) seq that represents a slice of ints | |
type Middleware struct { | |
} | |
// implements the ServeHTTP method as requested by Handler's interface | |
// notice the syntax: | |
// after the keyword func we declare the type that the method will be attached to | |
// and then we pass a parameter m that represents our instance variable | |
// in a very similar way to Python's self | |
func (m *Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
fmt.Printf("Request to %s is being handled by our middleware\n", r.URL.Path) | |
w.Header().Set("X-Powered-By", "mikamai-web-server") | |
} | |
// initialize and return a new object literal of type Middleware | |
// in Go we also declare the return type, after the parameters of the function | |
// in this case a pointer, denoted by the *, to Middleware type | |
func NewMiddleware() *Middleware { | |
return &Middleware{} | |
} | |
func main() { | |
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintf(w, "Hello World!") | |
}) | |
fmt.Println("Server running at http://localhost:8080/") | |
fmt.Println("hit CTRL+C to shut it down") | |
http.ListenAndServe(":8080", NewMiddleware()) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net/http" | |
) | |
// we expand our middleware to contain the definition of the single allowed host | |
type Middleware struct { | |
allowedHost string | |
} | |
// Rewrite ServeHTTP to check for the host validity | |
func (m *Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
// strip the port from hostname | |
host := strings.Split(r.Host, ":")[0] // import "strings" in order to use Split | |
// the signature is sent anyway | |
w.Header().Set("X-Powered-By", "mikamai-web-server") | |
if host == m.allowedHost { | |
fmt.Printf("Request for host %s is being handled by our middleware\n", host) | |
// net/http has a default handler called DefaultServerMux | |
// we feeded it with an handler for "/" in the first example | |
// forward the call to the default handler and send "Hello World!" to the client | |
http.DefaultServeMux.ServeHTTP(w, r) | |
} else { | |
// request is denied, wrong hostname | |
fmt.Printf("Request for host %s is strictly forbidden by our middleware\n", host) | |
// order is important.If we send data before the header, the server assumes the return code is 200 OK | |
w.WriteHeader(403) | |
fmt.Fprintf(w, "<h1>Forbidden</h1>you don't have permission to access host %s", host) | |
} | |
} | |
// we modify the initializer function (our constructor) as well | |
func NewMiddleware(host string) *Middleware { | |
return &Middleware{ | |
allowedHost: host, // trailing comma is required by Go compiler | |
} | |
} | |
func main() { | |
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintf(w, "Hello World!") | |
}) | |
fmt.Println("Server running at http://localhost:8080/") | |
fmt.Println("hit CTRL+C to shut it down") | |
http.ListenAndServe(":8080", NewMiddleware("localhost")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment