go-gemini/examples/server.go

49 lines
993 B
Go
Raw Normal View History

2020-10-12 20:34:52 +00:00
// +build ignore
2020-09-21 21:23:51 +00:00
2020-12-18 05:47:30 +00:00
// This example illustrates a Gemini server.
2020-09-21 21:23:51 +00:00
package main
import (
"crypto/tls"
"crypto/x509/pkix"
2020-09-21 21:23:51 +00:00
"log"
2021-02-17 14:25:44 +00:00
"os"
"time"
2020-10-28 18:59:45 +00:00
"git.sr.ht/~adnano/go-gemini"
2021-01-15 02:23:13 +00:00
"git.sr.ht/~adnano/go-gemini/certificate"
2020-09-21 21:23:51 +00:00
)
func main() {
2021-02-19 23:45:19 +00: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 20:34:52 +00:00
}
2021-02-19 23:45:19 +00:00
certificates.Register("localhost")
if err := certificates.Load("/var/lib/gemini/certs"); err != nil {
log.Fatal(err)
}
2020-10-12 20:34:52 +00:00
2021-02-19 23:45:19 +00: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 21:23:51 +00:00
2020-10-12 20:34:52 +00:00
if err := server.ListenAndServe(); err != nil {
2020-10-12 04:13:24 +00:00
log.Fatal(err)
}
2020-09-21 21:23:51 +00:00
}