// Minimal, isolated HTTP fixture for a RouterOS VETH lab.
package main

import (
	"errors"
	"log"
	"net"
	"net/http"
	"time"
)

func observeBoundary(label, address string) {
	conn, err := net.DialTimeout("tcp4", address, 400*time.Millisecond)
	if err != nil {
		log.Printf("%s: blocked or unavailable", label)
		return
	}
	_ = conn.Close()
	log.Printf("%s: unexpected successful connection", label)
}

func health(w http.ResponseWriter, r *http.Request) {
	if r.URL.Path != "/health" {
		http.NotFound(w, r)
		return
	}
	if r.Method != http.MethodGet && r.Method != http.MethodHead {
		w.Header().Set("Allow", "GET, HEAD")
		http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
		return
	}
	w.Header().Set("Content-Type", "text/plain; charset=utf-8")
	w.Header().Set("Cache-Control", "no-store")
	_, _ = w.Write([]byte("ITTA container ready\n"))
}

func main() {
	observeBoundary("LAB_CLIENT_EGRESS", "198.51.100.2:8081")
	observeBoundary("ROUTER_INPUT_BOUNDARY", "192.0.2.1:8081")
	server := http.Server{
		Addr:              "0.0.0.0:8080",
		Handler:           http.HandlerFunc(health),
		ReadHeaderTimeout: 2 * time.Second,
		WriteTimeout:      2 * time.Second,
		IdleTimeout:       5 * time.Second,
		MaxHeaderBytes:    4096,
	}
	if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
		log.Fatal(err)
	}
}
