Add http server initial setup

This commit is contained in:
Xanderitsme
2026-03-30 17:55:10 -05:00
commit daa2a9c4c7
4 changed files with 138 additions and 0 deletions

52
.air.toml Normal file
View File

@@ -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

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
tmp/**/*

3
go.mod Normal file
View File

@@ -0,0 +1,3 @@
module github.com/Xanderitsme/go-http-testing
go 1.24.2

82
main.go Normal file
View File

@@ -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()
}