Update examples
This commit is contained in:
parent
d479c6391c
commit
0baa66a4e7
@ -3,10 +3,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"crypto/sha512"
|
"crypto/sha512"
|
||||||
"crypto/tls"
|
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"crypto/x509/pkix"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
@ -25,24 +24,22 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var mux gemini.ServeMux
|
certificates := &certificate.Store{}
|
||||||
|
certificates.Register("localhost")
|
||||||
|
if err := certificates.Load("/var/lib/gemini/certs"); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
mux := &gemini.ServeMux{}
|
||||||
mux.HandleFunc("/", profile)
|
mux.HandleFunc("/", profile)
|
||||||
mux.HandleFunc("/username", changeUsername)
|
mux.HandleFunc("/username", changeUsername)
|
||||||
|
|
||||||
var server gemini.Server
|
server := &gemini.Server{
|
||||||
if err := server.Certificates.Load("/var/lib/gemini/certs"); err != nil {
|
Handler: mux,
|
||||||
log.Fatal(err)
|
ReadTimeout: 30 * time.Second,
|
||||||
|
WriteTimeout: 1 * time.Minute,
|
||||||
|
GetCertificate: certificates.GetCertificate,
|
||||||
}
|
}
|
||||||
server.GetCertificate = func(hostname string) (tls.Certificate, error) {
|
|
||||||
return certificate.Create(certificate.CreateOptions{
|
|
||||||
Subject: pkix.Name{
|
|
||||||
CommonName: hostname,
|
|
||||||
},
|
|
||||||
DNSNames: []string{hostname},
|
|
||||||
Duration: time.Hour,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
server.Handler = &mux
|
|
||||||
|
|
||||||
if err := server.ListenAndServe(); err != nil {
|
if err := server.ListenAndServe(); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
@ -54,7 +51,7 @@ func fingerprint(cert *x509.Certificate) string {
|
|||||||
return string(b[:])
|
return string(b[:])
|
||||||
}
|
}
|
||||||
|
|
||||||
func profile(w gemini.ResponseWriter, r *gemini.Request) {
|
func profile(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request) {
|
||||||
if len(r.TLS.PeerCertificates) == 0 {
|
if len(r.TLS.PeerCertificates) == 0 {
|
||||||
w.WriteHeader(gemini.StatusCertificateRequired, "Certificate required")
|
w.WriteHeader(gemini.StatusCertificateRequired, "Certificate required")
|
||||||
return
|
return
|
||||||
@ -69,7 +66,7 @@ func profile(w gemini.ResponseWriter, r *gemini.Request) {
|
|||||||
fmt.Fprintln(w, "=> /username Change username")
|
fmt.Fprintln(w, "=> /username Change username")
|
||||||
}
|
}
|
||||||
|
|
||||||
func changeUsername(w gemini.ResponseWriter, r *gemini.Request) {
|
func changeUsername(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request) {
|
||||||
if len(r.TLS.PeerCertificates) == 0 {
|
if len(r.TLS.PeerCertificates) == 0 {
|
||||||
w.WriteHeader(gemini.StatusCertificateRequired, "Certificate required")
|
w.WriteHeader(gemini.StatusCertificateRequired, "Certificate required")
|
||||||
return
|
return
|
||||||
|
@ -5,8 +5,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
|
||||||
"crypto/x509/pkix"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
@ -16,17 +14,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
certificates := &certificate.Store{
|
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,
|
|
||||||
})
|
|
||||||
},
|
|
||||||
}
|
|
||||||
certificates.Register("localhost")
|
certificates.Register("localhost")
|
||||||
if err := certificates.Load("/var/lib/gemini/certs"); err != nil {
|
if err := certificates.Load("/var/lib/gemini/certs"); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
@ -6,8 +6,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
|
||||||
"crypto/x509/pkix"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
@ -17,23 +15,21 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var server gemini.Server
|
certificates := &certificate.Store{}
|
||||||
if err := server.Certificates.Load("/var/lib/gemini/certs"); err != nil {
|
certificates.Register("localhost")
|
||||||
|
if err := certificates.Load("/var/lib/gemini/certs"); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
server.GetCertificate = func(hostname string) (tls.Certificate, error) {
|
|
||||||
return certificate.Create(certificate.CreateOptions{
|
|
||||||
Subject: pkix.Name{
|
|
||||||
CommonName: hostname,
|
|
||||||
},
|
|
||||||
DNSNames: []string{hostname},
|
|
||||||
Duration: 365 * 24 * time.Hour,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
var mux gemini.ServeMux
|
mux := &gemini.ServeMux{}
|
||||||
mux.HandleFunc("/", stream)
|
mux.HandleFunc("/", stream)
|
||||||
server.Handler = &mux
|
|
||||||
|
server := &gemini.Server{
|
||||||
|
Handler: mux,
|
||||||
|
ReadTimeout: 30 * time.Second,
|
||||||
|
WriteTimeout: 1 * time.Minute,
|
||||||
|
GetCertificate: certificates.GetCertificate,
|
||||||
|
}
|
||||||
|
|
||||||
if err := server.ListenAndServe(); err != nil {
|
if err := server.ListenAndServe(); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
@ -41,7 +37,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// stream writes an infinite stream to w.
|
// stream writes an infinite stream to w.
|
||||||
func stream(w gemini.ResponseWriter, r *gemini.Request) {
|
func stream(ctx context.Context, w gemini.ResponseWriter, r *gemini.Request) {
|
||||||
flusher, ok := w.(gemini.Flusher)
|
flusher, ok := w.(gemini.Flusher)
|
||||||
if !ok {
|
if !ok {
|
||||||
w.WriteHeader(gemini.StatusTemporaryFailure, "Internal error")
|
w.WriteHeader(gemini.StatusTemporaryFailure, "Internal error")
|
||||||
@ -49,7 +45,7 @@ func stream(w gemini.ResponseWriter, r *gemini.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ch := make(chan string)
|
ch := make(chan string)
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
|
|
||||||
go func(ctx context.Context) {
|
go func(ctx context.Context) {
|
||||||
for {
|
for {
|
||||||
|
Loading…
Reference in New Issue
Block a user