Created
February 21, 2022 10:01
-
-
Save mrtrkmn/360adfe162ae326820686ba32e052d05 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"log" | |
"net/http" | |
"time" | |
"github.com/caddyserver/certmagic" | |
"github.com/libdns/cloudflare" | |
) | |
type timeHandler struct { | |
format string | |
} | |
func (th timeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
tm := time.Now().Format(th.format) | |
w.Write([]byte("The time is: " + tm)) | |
} | |
func main() { | |
mux := http.NewServeMux() | |
th := timeHandler{format: time.RFC1123} | |
certmagic.DefaultACME.DNS01Solver = &certmagic.DNS01Solver{ | |
DNSProvider: &cloudflare.Provider{ | |
APIToken: "<cloudflare-api-token>", | |
}, | |
} | |
certmagic.DefaultACME.Agreed = true | |
certmagic.DefaultACME.Email = "<email-to-get-notification>" | |
certmagic.DefaultACME.CA = certmagic.LetsEncryptProductionCA | |
certmagicConf := certmagic.Config{ | |
Storage: &certmagic.FileStorage{ | |
Path: "<domain-lets-encrypt-path>", | |
}, | |
} | |
getConfigForCert := func(certmagic.Certificate) (*certmagic.Config, error) { | |
return &certmagicConf, nil | |
} | |
cacheOpts := certmagic.CacheOptions{ | |
GetConfigForCert: getConfigForCert, | |
} | |
cache := certmagic.NewCache(cacheOpts) | |
mux.Handle("/time", th) | |
magic := certmagic.New(cache, certmagicConf) | |
//manage certificate renewal through certmagic | |
magic.ManageAsync(context.Background(),[]string{ | |
fmt.Sprintf("*.%s", "<domain>"), | |
}) | |
domains := []string{fmt.Sprintf("*.%s", "<domain>")} | |
if err := certmagic.HTTPS(domains, mux); err != nil { | |
log.Printf("Serving error: %s", err) | |
} | |
fmt.Println("Serving from ") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment