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

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