go-gemini/examples/server.go

42 lines
884 B
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 (
"crypto/tls"
"crypto/x509/pkix"
2020-09-21 15:23:51 -06:00
"log"
"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() {
2020-10-28 12:59:45 -06:00
var server gemini.Server
server.ReadTimeout = 30 * time.Second
server.WriteTimeout = 1 * time.Minute
2020-10-28 12:59:45 -06:00
if err := server.Certificates.Load("/var/lib/gemini/certs"); err != nil {
2020-10-12 14:34:52 -06:00
log.Fatal(err)
}
2020-10-28 12:59:45 -06:00
server.CreateCertificate = func(hostname string) (tls.Certificate, error) {
2021-01-14 19:23:13 -07:00
return certificate.Create(certificate.CreateOptions{
Subject: pkix.Name{
CommonName: hostname,
},
2020-10-28 12:59:45 -06:00
DNSNames: []string{hostname},
Duration: 365 * 24 * time.Hour,
2020-10-28 12:59:45 -06:00
})
}
2020-10-12 14:34:52 -06:00
2020-10-28 12:59:45 -06:00
var mux gemini.ServeMux
mux.Handle("/", gemini.FileServer(gemini.Dir("/var/www")))
2020-09-21 15:23:51 -06:00
2020-10-21 14:28:50 -06:00
server.Register("localhost", &mux)
2020-10-12 14:34:52 -06:00
if err := server.ListenAndServe(); err != nil {
2020-10-11 22:13:24 -06:00
log.Fatal(err)
}
2020-09-21 15:23:51 -06:00
}