22032025-1

This commit is contained in:
Zeev Diukman 2025-03-22 09:00:48 +00:00
parent db7e6ea1b0
commit 82dece6abb

View file

@ -457,12 +457,12 @@ func AppRunner(shoudClear bool, runApp func()) {
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop() defer stop()
defer func() { // defer func() {
v := recover() // v := recover()
fmt.Println("Recovered:", v) // fmt.Println("Recovered:", v)
}() // }()
go runApp() runApp()
<-ctx.Done() <-ctx.Done()
//do stuff after ending //do stuff after ending
@ -472,8 +472,9 @@ func AppRunner(shoudClear bool, runApp func()) {
} }
func handlerOutput(w http.ResponseWriter, r *http.Request, str ...string) { func handlerOutput(w http.ResponseWriter, r *http.Request, str ...string) {
fmt.Fprintln(w, str[0]+": "+r.URL.Path+" "+str[1]) fmt.Fprintln(w, str[0]+": "+r.URL.Path+" "+str[1])
} }
func StartTestHTTPServer(port int, name string) { func StartTestHTTPServer(port int, name string, headers ...string) {
p := strconv.Itoa(port) p := strconv.Itoa(port)
go func() { go func() {
@ -482,13 +483,23 @@ func StartTestHTTPServer(port int, name string) {
// fmt.Println("Test server is running at http://" + GetIP() + ":" + p) // fmt.Println("Test server is running at http://" + GetIP() + ":" + p)
r := mux.NewRouter() r := mux.NewRouter()
r.Path("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { r.Path("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if name == "albert" {
AlbertHandler(w, r)
return
}
handlerOutput(w, r, name, "/") handlerOutput(w, r, name, "/")
}) })
r.Path("/test1").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { r.Path("/test1").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
handlerOutput(w, r, name, "/test1") handlerOutput(w, r, name, "/test1")
}) })
r.Path("/test2").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { r.PathPrefix("/api/users/").Path("/data").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
handlerOutput(w, r, name, "/test2") for _, h := range headers {
h := strings.Split(h, ":")
w.Header().Add(h[0], h[1])
}
w.Header().Add("test", "1234")
handlerOutput(w, r, name, "/api/users/data OK!")
}) })
err := http.ListenAndServe(":"+p, r) err := http.ListenAndServe(":"+p, r)
@ -553,3 +564,40 @@ func IsKeyExistsInMap[V comparable](mp map[string]V, key string) (V, bool, error
return val, ok, nil return val, ok, nil
} }
} }
func HttpClientWithSkipVerify() *http.Client {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
return &http.Client{Transport: tr}
}
func AlbertHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html") // Important: set the correct content type
html := `
<!DOCTYPE html>
<html>
<head>
<title>Albert Einstein Photo</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
img {
max-width: 80%;
max-height: 80%;
display: block;
}
</style>
</head>
<body>
<img src="https://upload.wikimedia.org/wikipedia/commons/1/14/Albert_Einstein_1947.jpg" alt="Albert Einstein">
</body>
</html>
`
w.Write([]byte(html)) // Write the HTML string to the response
}