Last active
August 29, 2015 14:01
-
-
Save albertogg/e9be036644735c7deb44 to your computer and use it in GitHub Desktop.
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 ( | |
"flag" | |
"fmt" | |
"log" | |
"net/http" | |
"github.com/bmizerany/pat" | |
) | |
var ( | |
port int | |
) | |
func init() { | |
flag.IntVar(&port, "port", 4000, "HTTP Server Port") | |
flag.Parse() | |
} | |
func rootHandler(w http.ResponseWriter, req *http.Request) { | |
fmt.Printf("%s %s\n", req.Method, req.URL) | |
fmt.Fprint(w, "Hello World!") | |
} | |
func nameHandler(w http.ResponseWriter, req *http.Request) { | |
fmt.Printf("%s %s\n", req.Method, req.URL) | |
params := req.URL.Query() | |
name := params.Get(":name") | |
w.Write([]byte("Hello " + name)) | |
} | |
func main() { | |
httpAddr := fmt.Sprintf(":%v", port) | |
log.Printf("Listening to %v", httpAddr) | |
router := pat.New() | |
router.Get("/", http.HandlerFunc(rootHandler)) | |
router.Get("/hello/:name", http.HandlerFunc(nameHandler)) | |
http.Handle("/", router) | |
log.Fatal(http.ListenAndServe(httpAddr, 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 ( | |
"flag" | |
"fmt" | |
"log" | |
"net/http" | |
) | |
var ( | |
port int | |
) | |
func rootHandler(w http.ResponseWriter, req *http.Request) { | |
fmt.Printf("%s %s %s\n", req.RemoteAddr, req.Method, req.URL) | |
fmt.Fprint(w, "Hello World!") | |
} | |
func init() { | |
flag.IntVar(&port, "port", 4000, "HTTP Server Port") | |
flag.Parse() | |
} | |
func main() { | |
httpAddr := fmt.Sprintf(":%v", port) | |
log.Printf("Listening to %v", httpAddr) | |
http.HandleFunc("/", rootHandler) | |
log.Fatal(http.ListenAndServe(httpAddr, nil)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to build and use another port: