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