68 lines
1.2 KiB
Go
68 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/url"
|
|
"os"
|
|
"strings"
|
|
|
|
"tildegit.org/tjp/gus"
|
|
"tildegit.org/tjp/gus/gemini"
|
|
)
|
|
|
|
func main() {
|
|
if len(os.Args) < 2 {
|
|
fmt.Fprintf(os.Stderr, "usage: %s <gemini url>\n", os.Args[0])
|
|
}
|
|
|
|
certfile, keyfile := envConfig()
|
|
|
|
// build a client
|
|
var client gemini.Client
|
|
if certfile != "" && keyfile != "" {
|
|
tlsConf, err := gemini.FileTLS(certfile, keyfile)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
client = gemini.NewClient(tlsConf)
|
|
}
|
|
|
|
// parse the URL and build the request
|
|
request := &gus.Request{URL: buildURL()}
|
|
|
|
// fetch the response
|
|
response, err := client.RoundTrip(request)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer response.Close()
|
|
|
|
if response.Status != gemini.StatusSuccess {
|
|
log.Fatalf("%d %s\n", response.Status, response.Meta)
|
|
}
|
|
|
|
//io.Copy(os.Stdout, response)
|
|
buf, err := io.ReadAll(gemini.NewResponseReader(response))
|
|
fmt.Printf("response: %s\n", buf)
|
|
}
|
|
|
|
func envConfig() (string, string) {
|
|
return os.Getenv("SERVER_CERTIFICATE"), os.Getenv("SERVER_PRIVATEKEY")
|
|
}
|
|
|
|
func buildURL() *url.URL {
|
|
raw := os.Args[1]
|
|
if strings.HasPrefix(raw, "//") {
|
|
raw = "gemini:" + raw
|
|
}
|
|
|
|
u, err := url.Parse(raw)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
return u
|
|
}
|