Last active
December 17, 2024 12:10
-
-
Save jmervine/8321794 to your computer and use it in GitHub Desktop.
Golang - Hello PATH HTTP Server Example
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" | |
"log" | |
"net/http" | |
) | |
func Log(handler http.Handler) http.Handler { | |
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
log.Printf("%s %s %s", r.RemoteAddr, r.Method, r.URL) | |
handler.ServeHTTP(w, r) | |
}) | |
} | |
func handler(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintf(w, "Hello %s!", r.URL.Path) | |
} | |
func main() { | |
http.HandleFunc("/", handler) | |
http.ListenAndServe(":8000", Log(http.DefaultServeMux)) | |
} |
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" | |
"strings" | |
"log" | |
"net/http" | |
"io/ioutil" | |
"encoding/json" | |
) | |
func Log(handler http.Handler) http.Handler { | |
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
log.Printf("%s %s %s", r.RemoteAddr, r.Method, r.URL) | |
handler.ServeHTTP(w, r) | |
}) | |
} | |
func filename(name string) string { | |
if name == "" { | |
name = "query" | |
} | |
// A little security, but don't be fooled, this still isn't really safe. | |
name = strings.Replace(name, ".", "", -1) | |
name = strings.TrimPrefix(name, "/") | |
name += ".txt" | |
return name | |
} | |
func get(res http.ResponseWriter, req *http.Request) { | |
// Example of fetching specific Query Param. | |
name := filename(req.Form.Get("name")) | |
body, err := ioutil.ReadFile(name) | |
if err != nil { | |
fmt.Fprintf(res, string(body[:])) | |
} else { | |
fmt.Fprintf(res, "Error: ", err) | |
} | |
} | |
func post(res http.ResponseWriter, req *http.Request) { | |
// Example of fetching specific Query Param. | |
name := filename(req.Form.Get("name")) | |
// Example of creating JSON string. | |
body, err := json.Marshal(req.Form) | |
if err != nil { | |
fmt.Fprintf(res, "ERROR: ", err) | |
} else { | |
ioutil.WriteFile(name, body, 0600) | |
fmt.Fprintf(res, string(body[:])) | |
} | |
} | |
func bad(res http.ResponseWriter, req *http.Request) { | |
fmt.Fprintf(res, "GO AWAY", req.Method, req.URL.Path) | |
} | |
func handler(res http.ResponseWriter, req *http.Request) { | |
// Example of parsing GET or POST Query Params. | |
req.ParseForm() | |
// Example of handling POST request. | |
switch req.Method { | |
case "POST": | |
post(res, req) | |
// Example of handling GET request. | |
case "GET": | |
get(res, req) | |
default: | |
bad(res, req) | |
} | |
} | |
func main() { | |
fmt.Println("WARNING: This is an example, but not really safe.") | |
http.HandleFunc("/", handler) | |
http.ListenAndServe(":8000", Log(http.DefaultServeMux)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment