go-gemini/examples/server.go

108 lines
2.2 KiB
Go
Raw Normal View History

2020-10-12 14:34:52 -06:00
// +build ignore
2020-09-21 15:23:51 -06:00
2020-12-17 22:47:30 -07:00
// This example illustrates a Gemini server.
2020-09-21 15:23:51 -06:00
package main
import (
2021-02-20 22:26:30 -07:00
"context"
2020-09-21 15:23:51 -06:00
"log"
2021-02-17 07:25:44 -07:00
"os"
"os/signal"
"time"
2020-10-28 12:59:45 -06:00
"git.sr.ht/~adnano/go-gemini"
2021-01-14 19:23:13 -07:00
"git.sr.ht/~adnano/go-gemini/certificate"
2020-09-21 15:23:51 -06:00
)
func main() {
2021-02-20 16:30:49 -07:00
certificates := &certificate.Store{}
2021-02-19 16:45:19 -07:00
certificates.Register("localhost")
if err := certificates.Load("/var/lib/gemini/certs"); err != nil {
log.Fatal(err)
}
2020-10-12 14:34:52 -06:00
2021-02-19 16:45:19 -07:00
mux := &gemini.ServeMux{}
mux.Handle("/", gemini.FileServer(os.DirFS("/var/www")))
server := &gemini.Server{
Handler: logMiddleware(mux),
2021-02-19 16:45:19 -07:00
ReadTimeout: 30 * time.Second,
WriteTimeout: 1 * time.Minute,
2021-02-23 06:43:41 -07:00
GetCertificate: certificates.Get,
2021-02-19 16:45:19 -07:00
}
2020-09-21 15:23:51 -06:00
// Listen for interrupt signal
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
errch := make(chan error)
go func() {
ctx := context.Background()
errch <- server.ListenAndServe(ctx)
}()
select {
case err := <-errch:
2020-10-11 22:13:24 -06:00
log.Fatal(err)
case <-c:
// Shutdown the server
log.Println("Shutting down...")
ctx, _ := context.WithTimeout(context.Background(), 30*time.Second)
err := server.Shutdown(ctx)
if err != nil {
log.Fatal(err)
}
2020-10-11 22:13:24 -06:00
}
2020-09-21 15:23:51 -06:00
}
func logMiddleware(h gemini.Handler) gemini.Handler {
return gemini.HandlerFunc(func(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request) {
lw := &logResponseWriter{rw: w}
h.ServeGemini(ctx, lw, r)
host := r.TLS().ServerName
log.Printf("gemini: %s %q %d %d", host, r.URL, lw.status, lw.wrote)
})
}
type logResponseWriter struct {
rw gemini.ResponseWriter
status gemini.Status
meta string
mediatype string
wroteHeader bool
wrote int
}
func (w *logResponseWriter) SetMediaType(mediatype string) {
w.mediatype = mediatype
}
func (w *logResponseWriter) Write(b []byte) (int, error) {
if !w.wroteHeader {
w.WriteHeader(gemini.StatusSuccess, w.mediatype)
}
n, err := w.rw.Write(b)
w.wrote += n
return n, err
}
func (w *logResponseWriter) WriteHeader(status gemini.Status, meta string) {
if w.wroteHeader {
return
}
w.status = status
w.meta = meta
w.wroteHeader = true
w.rw.WriteHeader(status, meta)
w.wrote += len(meta) + 5
}
func (w *logResponseWriter) Flush() error {
return nil
}
func (w *logResponseWriter) Close() error {
return nil
}