commit daa2a9c4c73b753be66467b814dc342bafa33d26 Author: Xanderitsme <136658282+Xanderitsme@users.noreply.github.com> Date: Mon Mar 30 17:55:10 2026 -0500 Add http server initial setup diff --git a/.air.toml b/.air.toml new file mode 100644 index 0000000..9e2cfaa --- /dev/null +++ b/.air.toml @@ -0,0 +1,52 @@ +root = "." +testdata_dir = "testdata" +tmp_dir = "tmp" + +[build] +args_bin = [] +bin = "./tmp/main" +cmd = "go build -o ./tmp/main ." +delay = 1000 +exclude_dir = ["assets", "tmp", "vendor", "testdata"] +exclude_file = [] +exclude_regex = ["_test.go"] +exclude_unchanged = false +follow_symlink = false +full_bin = "" +include_dir = [] +include_ext = ["go", "tpl", "tmpl", "html"] +include_file = [] +kill_delay = "0s" +log = "build-errors.log" +poll = false +poll_interval = 0 +post_cmd = [] +pre_cmd = [] +rerun = false +rerun_delay = 500 +send_interrupt = false +stop_on_error = false + +[color] +app = "" +build = "yellow" +main = "magenta" +runner = "green" +watcher = "cyan" + +[log] +main_only = false +silent = false +time = false + +[misc] +clean_on_exit = false + +[proxy] +app_port = 0 +enabled = false +proxy_port = 0 + +[screen] +clear_on_rebuild = false +keep_scroll = true diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e5d6558 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +tmp/**/* diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..87621f5 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/Xanderitsme/go-http-testing + +go 1.24.2 diff --git a/main.go b/main.go new file mode 100644 index 0000000..a05066b --- /dev/null +++ b/main.go @@ -0,0 +1,82 @@ +package main + +import ( + "context" + "errors" + "fmt" + "io" + "net" + "net/http" +) + +const keyServerAddr string = "adm-server" + +func main() { + mux := http.NewServeMux() + + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + + fmt.Printf("%s: got / request\n", ctx.Value((keyServerAddr))) + + path := r.URL.Path + + if path != "/" { + w.WriteHeader(404) + io.WriteString(w, "Not found") + return + } + + io.WriteString(w, "This is the index route") + }) + + mux.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + + fmt.Printf("%s: got /test request\n", ctx.Value((keyServerAddr))) + + io.WriteString(w, "Test") + }) + + ctx, cancelCtx := context.WithCancel(context.Background()) + + serverOne := &http.Server{ + Addr: ":3000", + Handler: mux, + BaseContext: func(l net.Listener) context.Context { + ctx = context.WithValue(ctx, keyServerAddr, l.Addr().String()) + return ctx + }, + } + + serverTwo := &http.Server{ + Addr: ":3001", + Handler: mux, + BaseContext: func(l net.Listener) context.Context { + ctx = context.WithValue(ctx, keyServerAddr, l.Addr().String()) + return ctx + }, + } + + go func() { + err := serverOne.ListenAndServe() + if errors.Is(err, http.ErrServerClosed) { + fmt.Printf("server one closed\n") + } else if err != nil { + fmt.Printf("error listening for server one: %s\n", err) + } + cancelCtx() + }() + + go func() { + err := serverTwo.ListenAndServe() + if errors.Is(err, http.ErrServerClosed) { + fmt.Printf("server two closed\n") + } else if err != nil { + fmt.Printf("error listening for server two: %s\n", err) + } + cancelCtx() + }() + + <-ctx.Done() +}