2020-12-18 17:31:37 +00:00
|
|
|
// +build ignore
|
|
|
|
|
|
|
|
// This example illustrates a streaming Gemini server.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-01-10 06:13:07 +00:00
|
|
|
"context"
|
2020-12-18 17:31:37 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.sr.ht/~adnano/go-gemini"
|
2021-01-15 02:23:13 +00:00
|
|
|
"git.sr.ht/~adnano/go-gemini/certificate"
|
2020-12-18 17:31:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2021-02-20 23:30:49 +00:00
|
|
|
certificates := &certificate.Store{}
|
|
|
|
certificates.Register("localhost")
|
|
|
|
if err := certificates.Load("/var/lib/gemini/certs"); err != nil {
|
2020-12-18 17:31:37 +00:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-03-15 19:44:35 +00:00
|
|
|
mux := &gemini.Mux{}
|
2021-02-18 01:35:27 +00:00
|
|
|
mux.HandleFunc("/", stream)
|
2021-02-20 23:30:49 +00:00
|
|
|
|
|
|
|
server := &gemini.Server{
|
|
|
|
Handler: mux,
|
|
|
|
ReadTimeout: 30 * time.Second,
|
|
|
|
WriteTimeout: 1 * time.Minute,
|
2021-02-23 13:43:41 +00:00
|
|
|
GetCertificate: certificates.Get,
|
2021-02-20 23:30:49 +00:00
|
|
|
}
|
2021-02-18 01:35:27 +00:00
|
|
|
|
2021-02-23 02:16:13 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
if err := server.ListenAndServe(ctx); err != nil {
|
|
|
|
log.Fatal(err)
|
2020-12-18 17:31:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-10 06:13:07 +00:00
|
|
|
// stream writes an infinite stream to w.
|
2021-02-20 23:30:49 +00:00
|
|
|
func stream(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request) {
|
2020-12-18 17:31:37 +00:00
|
|
|
for {
|
2021-02-23 20:31:41 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
2021-01-10 06:13:07 +00:00
|
|
|
}
|
2021-02-23 20:31:41 +00:00
|
|
|
fmt.Fprintln(w, time.Now().UTC())
|
2021-02-23 01:07:15 +00:00
|
|
|
if err := w.Flush(); err != nil {
|
2021-01-10 06:13:07 +00:00
|
|
|
return
|
|
|
|
}
|
2021-02-23 20:31:41 +00:00
|
|
|
time.Sleep(time.Second)
|
2020-12-18 17:31:37 +00:00
|
|
|
}
|
|
|
|
}
|