github.com/fumiama/terasu@v0.0.0-20240507144117-547a591149c0/cmd/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"net/http"
     7  	"os"
     8  	"strings"
     9  
    10  	"github.com/fumiama/terasu/http2"
    11  )
    12  
    13  func main() {
    14  	if len(os.Args) != 2 {
    15  		fmt.Println("Usage:", os.Args[0], "url")
    16  		return
    17  	}
    18  	if !strings.HasPrefix(os.Args[1], "https://") {
    19  		fmt.Println("ERROR: invalid url")
    20  		return
    21  	}
    22  	resp, err := http2.Get(os.Args[1])
    23  	if err != nil {
    24  		fmt.Println("ERROR:", err)
    25  		return
    26  	}
    27  	defer resp.Body.Close()
    28  	if resp.StatusCode != http.StatusOK {
    29  		fmt.Println("ERROR:", "status code:", resp.StatusCode)
    30  		return
    31  	}
    32  	data, err := io.ReadAll(resp.Body)
    33  	if err != nil {
    34  		fmt.Println("ERROR:", err)
    35  		return
    36  	}
    37  	fmt.Print(string(data))
    38  }