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

54 lines
1.2 KiB
Go
Raw Normal View History

2020-10-12 22:34:52 +02:00
// +build ignore
2020-09-21 23:23:51 +02:00
package main
import (
"crypto/tls"
2020-09-21 23:23:51 +02:00
"log"
"time"
2020-09-29 00:19:59 +02:00
"git.sr.ht/~adnano/gmi"
2020-09-21 23:23:51 +02:00
)
func main() {
2020-10-12 22:34:52 +02:00
var server gmi.Server
if err := server.CertificateStore.Load("/var/lib/gemini/certs"); err != nil {
log.Fatal(err)
}
server.GetCertificate = func(hostname string, store *gmi.CertificateStore) *tls.Certificate {
cert, err := store.Lookup(hostname)
if err != nil {
switch err {
case gmi.ErrInvalidCertificate:
log.Print("Old certificate expired, creating new one")
// Generate a new certificate if the old one is expired.
crt, key, err := gmi.NewRawCertificate(hostname, time.Minute)
if err != nil {
// Failed to generate new certificate, abort
return nil
}
// Store and return the new certificate
err = gmi.WriteX509KeyPair("/var/lib/gemini/certs/"+hostname, crt, key)
if err != nil {
return nil
}
newCert, err := tls.X509KeyPair(crt, key)
if err != nil {
return nil
}
store.Add(hostname, newCert)
return &newCert
}
}
return cert
}
2020-10-12 22:34:52 +02:00
var mux gmi.ServeMux
2020-09-28 02:20:59 +02:00
mux.Handle("/", gmi.FileServer(gmi.Dir("/var/www")))
2020-09-21 23:23:51 +02:00
2020-10-12 22:34:52 +02:00
server.Handle("localhost", &mux)
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
}