Skip to content

Instantly share code, notes, and snippets.

@nenodias
Created March 20, 2023 14:06
Show Gist options
  • Save nenodias/629e9658a735b8554fea6baadb86c0b5 to your computer and use it in GitHub Desktop.
Save nenodias/629e9658a735b8554fea6baadb86c0b5 to your computer and use it in GitHub Desktop.
Server-Test
package main
import (
"fmt"
"io"
"net/http"
"os"
"github.com/google/uuid"
)
func main() {
port := "8081"
if len(os.Args) == 2 && os.Args[1] == "--help" {
fmt.Printf("-p <port> : for use another port\n")
os.Exit(0)
return
}
if len(os.Args) == 3 && os.Args[1] == "-p" {
port = os.Args[2]
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
transcation := uuid.NewString()
fmt.Println("----------------")
fmt.Printf("[%s] - Path: %s\n", transcation, r.URL.Path)
fmt.Printf("[%s] - Method: %s\n", transcation, r.Method)
fmt.Printf("[%s] - Headers: \n", transcation)
for k, v := range r.Header {
fmt.Printf("[%s] - \t%s: %s\n", transcation, k, v)
}
fmt.Printf("[%s] - Query: %s\n", transcation, r.URL.RawQuery)
b, err := io.ReadAll(r.Body)
if err != nil {
fmt.Println(err.Error())
}
fmt.Printf("[%s] - Body: \n%s\n", transcation, string(b))
fmt.Println("----------------")
})
fmt.Printf("Listening on port: %s\n", port)
http.ListenAndServe(":"+port, nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment