@marcel91200 / caddy Mirror · read-only / commits / 31ab737b
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

Refactor code related to getting current version

And set version in CertMagic for User-Agent purposes

Matthew Holt committed Jun 28, 2019 at 19:28 UTC 31ab737bf26deb9315ffedf99a8bb7d43f620700
5 files changed +61 -48
caddy.go
+49 -25
@@ -5,6 +5,7 @@ import (
5 "encoding/json"
6 "fmt"
7 "log"
8 + "runtime/debug"
9 "strings"
10 "sync"
11 "time"
@@ -12,6 +13,28 @@ import (
13 "github.com/mholt/certmagic"
14 )
15
16 +// Config represents a Caddy configuration.
17 +type Config struct {
18 + Admin *AdminConfig `json:"admin,omitempty"`
19 +
20 + StorageRaw json.RawMessage `json:"storage,omitempty"`
21 + storage certmagic.Storage
22 +
23 + AppsRaw map[string]json.RawMessage `json:"apps,omitempty"`
24 +
25 + // apps stores the decoded Apps values,
26 + // keyed by module name.
27 + apps map[string]App
28 +
29 + cancelFunc context.CancelFunc
30 +}
31 +
32 +// App is a thing that Caddy runs.
33 +type App interface {
34 + Start() error
35 + Stop() error
36 +}
37 +
38 // Run runs Caddy with the given config.
39 func Run(newCfg *Config) error {
40 currentCfgMu.Lock()
@@ -69,7 +92,7 @@ func Run(newCfg *Config) error {
92 return err
93 }
94
72 - // Load, Provision, Validate
95 + // Load, Provision, Validate each app and their submodules
96 err = func() error {
97 for modName, rawMsg := range newCfg.AppsRaw {
98 val, err := ctx.LoadModule(modName, rawMsg)
@@ -112,7 +135,7 @@ func Run(newCfg *Config) error {
135 oldCfg := currentCfg
136 currentCfg = newCfg
137
115 - // Stop, Cleanup
138 + // Stop, Cleanup each old app
139 if oldCfg != nil {
140 for name, a := range oldCfg.apps {
141 err := a.Stop()
@@ -121,35 +144,13 @@ func Run(newCfg *Config) error {
144 }
145 }
146
124 - // clean up old modules
147 + // clean up all old modules
148 oldCfg.cancelFunc()
149 }
150
151 return nil
152 }
153
131 -// App is a thing that Caddy runs.
132 -type App interface {
133 - Start() error
134 - Stop() error
135 -}
136 -
137 -// Config represents a Caddy configuration.
138 -type Config struct {
139 - Admin *AdminConfig `json:"admin,omitempty"`
140 -
141 - StorageRaw json.RawMessage `json:"storage,omitempty"`
142 - storage certmagic.Storage
143 -
144 - AppsRaw map[string]json.RawMessage `json:"apps,omitempty"`
145 -
146 - // apps stores the decoded Apps values,
147 - // keyed by module name.
148 - apps map[string]App
149 -
150 - cancelFunc context.CancelFunc
151 -}
152 -
154 // Duration is a JSON-string-unmarshable duration type.
155 type Duration time.Duration
156
@@ -163,6 +164,29 @@ func (d *Duration) UnmarshalJSON(b []byte) error {
164 return nil
165 }
166
167 +// GoModule returns the build info of this Caddy
168 +// build from debug.BuildInfo (requires Go modules).
169 +// If no version information is available, a non-nil
170 +// value will still be returned, but with an
171 +// unknown version.
172 +func GoModule() *debug.Module {
173 + bi, ok := debug.ReadBuildInfo()
174 + if ok {
175 + // The recommended way to build Caddy involves
176 + // creating a separate main module, which
177 + // TODO: track related Go issue: https://github.com/golang/go/issues/29228
178 + for _, mod := range bi.Deps {
179 + if mod.Path == goModule {
180 + return mod
181 + }
182 + }
183 + }
184 + return &debug.Module{Version: "unknown"}
185 +}
186 +
187 +// goModule is the name of this Go module.
188 +const goModule = "github.com/mholt/caddy"
189 +
190 // CtxKey is a value type for use with context.WithValue.
191 type CtxKey string
192
cmd/commands.go
+9 -2
@@ -10,8 +10,10 @@ import (
10 "os"
11 "os/exec"
12 "path/filepath"
13 + "strings"
14
15 "github.com/caddyserver/caddy"
16 + "github.com/mholt/certmagic"
17 "github.com/mitchellh/go-ps"
18 )
19
@@ -126,6 +128,11 @@ func cmdRun() (int, error) {
128 }
129 }
130
131 + // set a fitting User-Agent for ACME requests
132 + goModule := caddy.GoModule()
133 + cleanModVersion := strings.TrimPrefix(goModule.Version, "v")
134 + certmagic.UserAgent = "Caddy/" + cleanModVersion
135 +
136 // start the admin endpoint along with any initial config
137 err := caddy.StartAdmin(config)
138 if err != nil {
@@ -180,10 +187,10 @@ func cmdStop() (int, error) {
187 }
188
189 func cmdVersion() (int, error) {
183 - goModule := getGoBuildModule()
190 + goModule := caddy.GoModule()
191 if goModule.Sum != "" {
192 // a build with a known version will also have a checksum
186 - fmt.Printf("%s (%s)\n", goModule.Version, goModule.Sum)
193 + fmt.Printf("%s %s\n", goModule.Version, goModule.Sum)
194 } else {
195 fmt.Println(goModule.Version)
196 }
cmd/main.go
-21
@@ -9,7 +9,6 @@ import (
9 "log"
10 "net"
11 "os"
12 - "runtime/debug"
12 )
13
14 // Main executes the main function of the caddy command.
@@ -66,23 +65,3 @@ func handlePingbackConn(conn net.Conn, expect []byte) error {
65 }
66 return nil
67 }
69 -
70 -// getGoBuildModule returns the build info of Caddy
71 -// from debug.BuildInfo (requires Go modules). If
72 -// no version information is available, a non-nil
73 -// value will still be returned, but with an
74 -// unknown version.
75 -func getGoBuildModule() *debug.Module {
76 - bi, ok := debug.ReadBuildInfo()
77 - if ok {
78 - // The recommended way to build Caddy involves
79 - // creating a separate main module, which
80 - // TODO: track related Go issue: https://github.com/golang/go/issues/29228
81 - for _, mod := range bi.Deps {
82 - if mod.Path == "github.com/mholt/caddy" {
83 - return mod
84 - }
85 - }
86 - }
87 - return &debug.Module{Version: "unknown"}
88 -}
go.mod
+1
@@ -17,6 +17,7 @@ require (
17 github.com/klauspost/compress v1.7.1-0.20190613161414-0b31f265a57b
18 github.com/klauspost/cpuid v1.2.1
19 github.com/mholt/certmagic v0.6.2-0.20190624175158-6a42ef9fe8c2
20 + github.com/mitchellh/go-ps v0.0.0-20170309133038-4fdf99ab2936
21 github.com/rs/cors v1.6.0
22 github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
23 github.com/starlight-go/starlight v0.0.0-20181207205707-b06f321544f3
go.sum
+2
@@ -36,6 +36,8 @@ github.com/mholt/certmagic v0.6.2-0.20190624175158-6a42ef9fe8c2 h1:xKE9kZ5C8gelJ
36 github.com/mholt/certmagic v0.6.2-0.20190624175158-6a42ef9fe8c2/go.mod h1:g4cOPxcjV0oFq3qwpjSA30LReKD8AoIfwAY9VvG35NY=
37 github.com/miekg/dns v1.1.3 h1:1g0r1IvskvgL8rR+AcHzUA+oFmGcQlaIm4IqakufeMM=
38 github.com/miekg/dns v1.1.3/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
39 +github.com/mitchellh/go-ps v0.0.0-20170309133038-4fdf99ab2936 h1:kw1v0NlnN+GZcU8Ma8CLF2Zzgjfx95gs3/GN3vYAPpo=
40 +github.com/mitchellh/go-ps v0.0.0-20170309133038-4fdf99ab2936/go.mod h1:r1VsdOzOPt1ZSrGZWFoNhsAedKnEd6r9Np1+5blZCWk=
41 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
42 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
43 github.com/rs/cors v1.6.0 h1:G9tHG9lebljV9mfp9SNPDL36nCDxmo3zTlAf1YgvzmI=