go-gemini/examples/server.go

58 lines
1.1 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: mux,
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
}