1
0
mirror of https://git.sr.ht/~adnano/go-gemini synced 2024-11-10 00:32:11 +01:00
go-gemini/examples/server.go

59 lines
1.1 KiB
Go
Raw Normal View History

2020-10-12 22:34:52 +02:00
// +build ignore
2020-09-21 23:23:51 +02:00
2020-12-18 06:47:30 +01:00
// This example illustrates a Gemini server.
2020-09-21 23:23:51 +02:00
package main
import (
2021-02-21 06:26:30 +01:00
"context"
2020-09-21 23:23:51 +02:00
"log"
2021-02-17 15:25:44 +01:00
"os"
"os/signal"
"time"
2020-10-28 19:59:45 +01:00
"git.sr.ht/~adnano/go-gemini"
2021-01-15 03:23:13 +01:00
"git.sr.ht/~adnano/go-gemini/certificate"
2020-09-21 23:23:51 +02:00
)
func main() {
2021-02-21 00:30:49 +01:00
certificates := &certificate.Store{}
2021-02-20 00:45:19 +01:00
certificates.Register("localhost")
if err := certificates.Load("/var/lib/gemini/certs"); err != nil {
log.Fatal(err)
}
2020-10-12 22:34:52 +02:00
2021-03-15 20:44:35 +01:00
mux := &gemini.Mux{}
2021-02-20 00:45:19 +01:00
mux.Handle("/", gemini.FileServer(os.DirFS("/var/www")))
server := &gemini.Server{
Handler: gemini.LoggingMiddleware(mux),
2021-02-20 00:45:19 +01:00
ReadTimeout: 30 * time.Second,
WriteTimeout: 1 * time.Minute,
2021-02-23 14:43:41 +01:00
GetCertificate: certificates.Get,
2021-02-20 00:45:19 +01:00
}
2020-09-21 23:23:51 +02: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-12 06:13:24 +02:00
log.Fatal(err)
case <-c:
// Shutdown the server
log.Println("Shutting down...")
2021-02-24 16:27:28 +01:00
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
err := server.Shutdown(ctx)
if err != nil {
log.Fatal(err)
}
2020-10-12 06:13:24 +02:00
}
2020-09-21 23:23:51 +02:00
}