Last active
February 25, 2024 13:36
-
-
Save harryhanYuhao/9d9ec42ab97a526c799ddff965f636a0 to your computer and use it in GitHub Desktop.
Go minimal server
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
// to run: go mod init <modname>; go tidy; go run . | |
package main | |
import ( | |
"fmt" | |
"log" | |
"net/http" | |
"github.com/davecgh/go-spew/spew" | |
) | |
func syncData(w http.ResponseWriter, r *http.Request) { | |
// get id passed as `...?id=2111` | |
// id := r.URL.Query().Get("id") | |
// spew is for pretty printing | |
fmt.Fprintf(w, spew.Sdump(r)) | |
} | |
func main() { | |
mux := http.NewServeMux() | |
mux.HandleFunc("/", syncData) | |
// file server | |
//mux.Handle("/download/", http.StripPrefix("/download/", http.FileServer(http.Dir(fileDir)))) | |
log.Fatal(http.ListenAndServe(":8080", mux)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment