@marcel91200 / caddy Mirror · read-only / commits / aac1ccf1
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 05:15 UTC

caddy: Add InstanceID() method

Caddy can now generate and persist its own instance ID, a UUID that is stored in the data directory. This makes it possible to differentiate it from other instances in a cluster.

Matthew Holt committed Mar 30, 2021 at 14:15 UTC aac1ccf12d0076ce7ae2b67ed9cbdf55dc99e14e
3 files changed +24 -1
caddy.go
+21
@@ -33,6 +33,7 @@ import (
33 "time"
34
35 "github.com/caddyserver/certmagic"
36 + "github.com/google/uuid"
37 "go.uber.org/zap"
38 )
39
@@ -662,6 +663,26 @@ func ParseDuration(s string) (time.Duration, error) {
663 return time.ParseDuration(s)
664 }
665
666 +// InstanceID returns the UUID for this instance, and generates one if it
667 +// does not already exist. The UUID is stored in the local data directory,
668 +// regardless of storage configuration, since each instance is intended to
669 +// have its own unique ID.
670 +func InstanceID() (uuid.UUID, error) {
671 + uuidFilePath := filepath.Join(AppDataDir(), "instance.uuid")
672 + uuidFileBytes, err := os.ReadFile(uuidFilePath)
673 + if os.IsNotExist(err) {
674 + uuid, err := uuid.NewRandom()
675 + if err != nil {
676 + return uuid, err
677 + }
678 + err = ioutil.WriteFile(uuidFilePath, []byte(uuid.String()), 0644)
679 + return uuid, err
680 + } else if err != nil {
681 + return [16]byte{}, err
682 + }
683 + return uuid.ParseBytes(uuidFileBytes)
684 +}
685 +
686 // GoModule returns the build info of this Caddy
687 // build from debug.BuildInfo (requires Go modules).
688 // If no version information is available, a non-nil
go.mod
+1
@@ -10,6 +10,7 @@ require (
10 github.com/dustin/go-humanize v1.0.1-0.20200219035652-afde56e7acac
11 github.com/go-chi/chi v4.1.2+incompatible
12 github.com/google/cel-go v0.6.0
13 + github.com/google/uuid v1.2.0
14 github.com/klauspost/compress v1.11.3
15 github.com/klauspost/cpuid/v2 v2.0.1
16 github.com/lucas-clemente/quic-go v0.19.3
go.sum
+2 -1
@@ -304,8 +304,9 @@ github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm4
304 github.com/google/trillian v1.2.2-0.20190612132142-05461f4df60a/go.mod h1:YPmUVn5NGwgnDUgqlVyFGMTgaWlnSvH7W5p+NdOG8UA=
305 github.com/google/trillian-examples v0.0.0-20190603134952-4e75ba15216c/go.mod h1:WgL3XZ3pA8/9cm7yxqWrZE6iZkESB2ItGxy5Fo6k2lk=
306 github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
307 -github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
307 github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
308 +github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
309 +github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
310 github.com/googleapis/gax-go v2.0.0+incompatible h1:j0GKcs05QVmm7yesiZq2+9cxHkNK9YM6zKx4D2qucQU=
311 github.com/googleapis/gax-go v2.0.0+incompatible/go.mod h1:SFVmujtThgffbyetf+mdk2eWhX2bMyUtNHzFKcPA9HY=
312 github.com/googleapis/gax-go/v2 v2.0.3/go.mod h1:LLvjysVCY1JZeum8Z6l8qUty8fiNwE08qbEPm1M08qg=