go-gemini/examples/server/server.go
2020-09-21 18:25:31 -04:00

42 lines
918 B
Go

// +build example
package main
import (
"crypto/tls"
"git.sr.ht/~adnano/go-gemini"
"log"
"net/url"
)
func main() {
// Load a TLS key pair.
// To generate a TLS key pair, run:
//
// openssl genrsa -out server.key 2048
// openssl ecparam -genkey -name secp384r1 -out server.key
// openssl req -new -x509 -sha256 -key server.key -out server.crt -days 3650
//
config := tls.Config{}
cert, err := tls.LoadX509KeyPair("examples/server/server.crt", "examples/server/server.key")
if err != nil {
log.Fatal(err)
}
config.Certificates = append(config.Certificates, cert)
mux := &gemini.Mux{}
mux.HandleFunc("/", func(url *url.URL) *gemini.Response {
return &gemini.Response{
Status: gemini.StatusSuccess,
Meta: "text/gemini",
Body: []byte("You requested " + url.String()),
}
})
server := gemini.Server{
TLSConfig: config,
Handler: mux,
}
server.ListenAndServe()
}