Skip to content

Instantly share code, notes, and snippets.

@jmervine
Last active November 22, 2019 13:16
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jmervine/8321794 to your computer and use it in GitHub Desktop.
Save jmervine/8321794 to your computer and use it in GitHub Desktop.
Golang - Hello PATH HTTP Server Example
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))
}
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