1
0
Fork 0
mirror of https://git.sr.ht/~adnano/go-gemini synced 2024-05-26 20:16:12 +02:00
go-gemini/doc.go

83 lines
2.0 KiB
Go
Raw Normal View History

2020-10-12 22:34:52 +02:00
/*
2020-10-24 21:15:32 +02:00
Package gemini implements the Gemini protocol.
2020-10-12 22:34:52 +02:00
2020-10-28 00:21:33 +01:00
Get makes a Gemini request:
2020-10-12 22:34:52 +02:00
2020-10-28 00:21:33 +01:00
resp, err := gemini.Get("gemini://example.com")
2020-10-12 22:34:52 +02:00
if err != nil {
// handle error
}
2020-10-12 22:49:35 +02:00
// ...
2020-10-12 22:34:52 +02:00
2020-10-28 00:21:33 +01:00
The client must close the response body when finished with it:
resp, err := gemini.Get("gemini://example.com")
if err != nil {
// handle error
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
// ...
For control over client behavior, create a Client:
2020-10-12 22:34:52 +02:00
2020-10-24 21:15:32 +02:00
var client gemini.Client
2020-10-28 00:21:33 +01:00
resp, err := client.Get("gemini://example.com")
2020-10-12 22:34:52 +02:00
if err != nil {
// handle error
}
// ...
2020-10-12 22:34:52 +02:00
2020-10-28 00:21:33 +01:00
Clients can load their own list of known hosts:
2020-10-12 22:34:52 +02:00
err := client.KnownHosts.Load("path/to/my/known_hosts")
2020-10-12 22:34:52 +02:00
if err != nil {
// handle error
}
Clients can control when to trust certificates with TrustCertificate:
2020-10-24 21:15:32 +02:00
client.TrustCertificate = func(hostname string, cert *x509.Certificate, knownHosts *gemini.KnownHosts) error {
2020-10-12 22:34:52 +02:00
return knownHosts.Lookup(hostname, cert)
}
2020-10-28 21:02:04 +01:00
Clients can create client certificates upon the request of a server:
2020-10-28 21:04:14 +01:00
client.CreateCertificate = func(hostname, path string) (tls.Certificate, error) {
2020-10-28 21:02:04 +01:00
return gemini.CreateCertificate(gemini.CertificateOptions{
Duration: time.Hour,
})
2020-10-12 22:34:52 +02:00
}
Server is a Gemini server.
2020-10-24 21:15:32 +02:00
var server gemini.Server
2020-10-12 22:34:52 +02:00
Servers must be configured with certificates:
2020-10-28 21:02:04 +01:00
err := server.Certificates.Load("/var/lib/gemini/certs")
if err != nil {
// handle error
}
2020-10-12 22:34:52 +02:00
Servers can accept requests for multiple hosts and schemes:
2020-10-24 21:15:32 +02:00
server.RegisterFunc("example.com", func(w *gemini.ResponseWriter, r *gemini.Request) {
2020-10-14 02:22:12 +02:00
fmt.Fprint(w, "Welcome to example.com")
2020-10-12 22:34:52 +02:00
})
2020-10-24 21:15:32 +02:00
server.RegisterFunc("example.org", func(w *gemini.ResponseWriter, r *gemini.Request) {
2020-10-14 02:22:12 +02:00
fmt.Fprint(w, "Welcome to example.org")
2020-10-12 22:34:52 +02:00
})
2020-10-24 21:15:32 +02:00
server.RegisterFunc("http://example.net", func(w *gemini.ResponseWriter, r *gemini.Request) {
2020-10-21 21:57:04 +02:00
fmt.Fprint(w, "Proxied content from http://example.net")
2020-10-12 22:34:52 +02:00
})
To start the server, call ListenAndServe:
err := server.ListenAndServe()
if err != nil {
// handle error
}
*/
2020-10-24 21:15:32 +02:00
package gemini