go-gemini/examples/server.go

49 lines
993 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"
2021-02-17 07:25:44 -07:00
"os"
"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-19 16:45:19 -07:00
certificates := &certificate.Store{
CreateCertificate: func(hostname string) (tls.Certificate, error) {
return certificate.Create(certificate.CreateOptions{
Subject: pkix.Name{
CommonName: hostname,
},
DNSNames: []string{hostname},
Duration: 365 * 24 * time.Hour,
})
},
2020-10-12 14:34:52 -06:00
}
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,
GetCertificate: certificates.GetCertificate,
}
2020-09-21 15:23:51 -06:00
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
}