Skip to content

Instantly share code, notes, and snippets.

@mrtrkmn
Created February 21, 2022 10:01
Show Gist options
  • Save mrtrkmn/360adfe162ae326820686ba32e052d05 to your computer and use it in GitHub Desktop.
Save mrtrkmn/360adfe162ae326820686ba32e052d05 to your computer and use it in GitHub Desktop.
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