github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/framework/httpclient/httpclient.go (about) 1 package httpclient 2 3 import ( 4 "errors" 5 "fmt" 6 "net/http" 7 "time" 8 ) 9 10 func HttpClient(serverPort string, handlefunc func(w http.ResponseWriter, r *http.Request)) { 11 go func() { 12 mux := http.NewServeMux() 13 mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { 14 fmt.Printf("server: %s /\n", r.Method) 15 handlefunc(w, r) 16 }) 17 server := http.Server{ 18 Addr: fmt.Sprintf(":%d", serverPort), 19 Handler: mux, 20 } 21 if err := server.ListenAndServe(); err != nil { 22 if !errors.Is(err, http.ErrServerClosed) { 23 fmt.Printf("error running http server: %s\n", err) 24 } 25 } 26 }() 27 28 time.Sleep(100 * time.Millisecond) 29 }