Last active
November 29, 2017 18:55
-
-
Save benhoyt/a8ecc3f03b86d2d25543750cdcc77732 to your computer and use it in GitHub Desktop.
Simple HTTP server with regex-based router in Go
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
// Simple HTTP server with regex-based router | |
package main | |
import ( | |
"fmt" | |
"net/http" | |
"regexp" | |
) | |
type Route struct { | |
Regex *regexp.Regexp | |
Handler func(w http.ResponseWriter, r *http.Request) | |
} | |
func homeHandler(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintf(w, "HOME") | |
} | |
func hotelHandler(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintf(w, "HOTEL %s", r.URL.Path) | |
} | |
func main() { | |
routes := []Route{ | |
{regexp.MustCompile(`^/$`), homeHandler}, | |
{regexp.MustCompile(`^/hotels/(\d+)$`), hotelHandler}, | |
} | |
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | |
for _, route := range routes { | |
matches := route.Regex.FindStringSubmatch(r.URL.Path) | |
if len(matches) >= 1 { | |
route.Handler(w, r) | |
return | |
} | |
} | |
http.NotFound(w, r) | |
}) | |
fmt.Println("listening on port 8080") | |
http.ListenAndServe(":8080", nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment