You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.3 KiB

  1. // Copyright 2017 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. // +build autocert
  5. // This file contains autocert and cloud.google.com/go/storage
  6. // dependencies we want to hide by default from the Go build system,
  7. // which currently doesn't know how to fetch non-golang.org/x/*
  8. // dependencies. The Dockerfile builds the production binary
  9. // with this code using --tags=autocert.
  10. package main
  11. import (
  12. "context"
  13. "crypto/tls"
  14. "log"
  15. "net/http"
  16. "cloud.google.com/go/storage"
  17. "golang.org/x/build/autocertcache"
  18. "golang.org/x/crypto/acme/autocert"
  19. )
  20. func init() {
  21. runHTTPS = runHTTPSAutocert
  22. }
  23. func runHTTPSAutocert(h http.Handler) error {
  24. var cache autocert.Cache
  25. if b := *autoCertCacheBucket; b != "" {
  26. sc, err := storage.NewClient(context.Background())
  27. if err != nil {
  28. log.Fatalf("storage.NewClient: %v", err)
  29. }
  30. cache = autocertcache.NewGoogleCloudStorageCache(sc, b)
  31. }
  32. m := autocert.Manager{
  33. Prompt: autocert.AcceptTOS,
  34. HostPolicy: autocert.HostWhitelist(*autoCertDomain),
  35. Cache: cache,
  36. }
  37. s := &http.Server{
  38. Addr: ":https",
  39. Handler: h,
  40. TLSConfig: &tls.Config{GetCertificate: m.GetCertificate},
  41. }
  42. return s.ListenAndServeTLS("", "")
  43. }