1
0
Fork 0
mirror of https://git.sr.ht/~adnano/go-gemini synced 2024-06-01 17:56:04 +02:00

Add (*CertificateStore).Write function

This commit is contained in:
Adnan Maolood 2020-11-09 13:54:15 -05:00
parent 85f8e84bd5
commit 3dee6dcff3
2 changed files with 16 additions and 6 deletions

15
cert.go
View File

@ -28,7 +28,7 @@ type CertificateStore struct {
// Add adds a certificate for the given scope to the store.
// It tries to parse the certificate if it is not already parsed.
func (c *CertificateStore) Add(scope string, cert tls.Certificate) error {
func (c *CertificateStore) Add(scope string, cert tls.Certificate) {
if c.store == nil {
c.store = map[string]tls.Certificate{}
}
@ -39,15 +39,18 @@ func (c *CertificateStore) Add(scope string, cert tls.Certificate) error {
cert.Leaf = parsed
}
}
c.store[scope] = cert
}
// Write writes the provided certificate to the certificate directory.
func (c *CertificateStore) Write(scope string, cert tls.Certificate) error {
if c.dir {
// Write certificates
certPath := filepath.Join(c.path, scope+".crt")
keyPath := filepath.Join(c.path, scope+".key")
if err := WriteCertificate(cert, certPath, keyPath); err != nil {
return err
}
}
c.store[scope] = cert
return nil
}
@ -82,6 +85,12 @@ func (c *CertificateStore) Load(path string) error {
return nil
}
// SetOutput sets the directory that new certificates will be written to.
func (c *CertificateStore) SetOutput(path string) {
c.dir = true
c.path = path
}
// CertificateOptions configures the creation of a certificate.
type CertificateOptions struct {
// Subject Alternate Name values.

View File

@ -160,8 +160,9 @@ func (s *Server) getCertificateFor(hostname string) (*tls.Certificate, error) {
if s.CreateCertificate != nil {
cert, err := s.CreateCertificate(hostname)
if err == nil {
if err := s.Certificates.Add(hostname, cert); err != nil {
s.logf("gemini: Failed to add new certificate for %s: %s", hostname, err)
s.Certificates.Add(hostname, cert)
if err := s.Certificates.Write(hostname, cert); err != nil {
s.logf("gemini: Failed to write new certificate for %s: %s", hostname, err)
}
}
return &cert, err
@ -262,7 +263,7 @@ type ResponseWriter struct {
b *bufio.Writer
bodyAllowed bool
wroteHeader bool
mediatype string
mediatype string
}
func newResponseWriter(conn net.Conn) *ResponseWriter {