github.com/gmemcc/yaegi@v0.12.1-0.20221128122509-aa99124c5d16/_test/cli5.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"log"
     7  	"net/http"
     8  	"net/http/httptest"
     9  )
    10  
    11  type mw1 struct {
    12  	next http.Handler
    13  }
    14  
    15  func (m *mw1) ServeHTTP(rw http.ResponseWriter, rq *http.Request) {
    16  	m.next.ServeHTTP(rw, rq)
    17  }
    18  
    19  type mw0 struct{}
    20  
    21  func (m *mw0) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    22  	fmt.Fprint(w, "Welcome to my website!")
    23  }
    24  
    25  func main() {
    26  	m0 := &mw0{}
    27  	m1 := &mw1{next: m0}
    28  
    29  	mux := http.NewServeMux()
    30  	mux.HandleFunc("/", m1.ServeHTTP)
    31  
    32  	server := httptest.NewServer(mux)
    33  	defer server.Close()
    34  
    35  	client(server.URL)
    36  }
    37  
    38  func client(uri string) {
    39  	resp, err := http.Get(uri)
    40  	if err != nil {
    41  		log.Fatal(err)
    42  	}
    43  	body, err := io.ReadAll(resp.Body)
    44  	if err != nil {
    45  		log.Fatal(err)
    46  	}
    47  	fmt.Println(string(body))
    48  }
    49  
    50  // Output:
    51  // Welcome to my website!