mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-16 21:56:40 -05:00
Refactor code related to getting current version
And set version in CertMagic for User-Agent purposes
This commit is contained in:
parent
a4bdf249db
commit
31ab737bf2
5 changed files with 61 additions and 48 deletions
74
caddy.go
74
caddy.go
|
@ -5,6 +5,7 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
@ -12,6 +13,28 @@ import (
|
|||
"github.com/mholt/certmagic"
|
||||
)
|
||||
|
||||
// Config represents a Caddy configuration.
|
||||
type Config struct {
|
||||
Admin *AdminConfig `json:"admin,omitempty"`
|
||||
|
||||
StorageRaw json.RawMessage `json:"storage,omitempty"`
|
||||
storage certmagic.Storage
|
||||
|
||||
AppsRaw map[string]json.RawMessage `json:"apps,omitempty"`
|
||||
|
||||
// apps stores the decoded Apps values,
|
||||
// keyed by module name.
|
||||
apps map[string]App
|
||||
|
||||
cancelFunc context.CancelFunc
|
||||
}
|
||||
|
||||
// App is a thing that Caddy runs.
|
||||
type App interface {
|
||||
Start() error
|
||||
Stop() error
|
||||
}
|
||||
|
||||
// Run runs Caddy with the given config.
|
||||
func Run(newCfg *Config) error {
|
||||
currentCfgMu.Lock()
|
||||
|
@ -69,7 +92,7 @@ func Run(newCfg *Config) error {
|
|||
return err
|
||||
}
|
||||
|
||||
// Load, Provision, Validate
|
||||
// Load, Provision, Validate each app and their submodules
|
||||
err = func() error {
|
||||
for modName, rawMsg := range newCfg.AppsRaw {
|
||||
val, err := ctx.LoadModule(modName, rawMsg)
|
||||
|
@ -112,7 +135,7 @@ func Run(newCfg *Config) error {
|
|||
oldCfg := currentCfg
|
||||
currentCfg = newCfg
|
||||
|
||||
// Stop, Cleanup
|
||||
// Stop, Cleanup each old app
|
||||
if oldCfg != nil {
|
||||
for name, a := range oldCfg.apps {
|
||||
err := a.Stop()
|
||||
|
@ -121,35 +144,13 @@ func Run(newCfg *Config) error {
|
|||
}
|
||||
}
|
||||
|
||||
// clean up old modules
|
||||
// clean up all old modules
|
||||
oldCfg.cancelFunc()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// App is a thing that Caddy runs.
|
||||
type App interface {
|
||||
Start() error
|
||||
Stop() error
|
||||
}
|
||||
|
||||
// Config represents a Caddy configuration.
|
||||
type Config struct {
|
||||
Admin *AdminConfig `json:"admin,omitempty"`
|
||||
|
||||
StorageRaw json.RawMessage `json:"storage,omitempty"`
|
||||
storage certmagic.Storage
|
||||
|
||||
AppsRaw map[string]json.RawMessage `json:"apps,omitempty"`
|
||||
|
||||
// apps stores the decoded Apps values,
|
||||
// keyed by module name.
|
||||
apps map[string]App
|
||||
|
||||
cancelFunc context.CancelFunc
|
||||
}
|
||||
|
||||
// Duration is a JSON-string-unmarshable duration type.
|
||||
type Duration time.Duration
|
||||
|
||||
|
@ -163,6 +164,29 @@ func (d *Duration) UnmarshalJSON(b []byte) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// GoModule returns the build info of this Caddy
|
||||
// build from debug.BuildInfo (requires Go modules).
|
||||
// If no version information is available, a non-nil
|
||||
// value will still be returned, but with an
|
||||
// unknown version.
|
||||
func GoModule() *debug.Module {
|
||||
bi, ok := debug.ReadBuildInfo()
|
||||
if ok {
|
||||
// The recommended way to build Caddy involves
|
||||
// creating a separate main module, which
|
||||
// TODO: track related Go issue: https://github.com/golang/go/issues/29228
|
||||
for _, mod := range bi.Deps {
|
||||
if mod.Path == goModule {
|
||||
return mod
|
||||
}
|
||||
}
|
||||
}
|
||||
return &debug.Module{Version: "unknown"}
|
||||
}
|
||||
|
||||
// goModule is the name of this Go module.
|
||||
const goModule = "github.com/mholt/caddy"
|
||||
|
||||
// CtxKey is a value type for use with context.WithValue.
|
||||
type CtxKey string
|
||||
|
||||
|
|
|
@ -10,8 +10,10 @@ import (
|
|||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/caddyserver/caddy"
|
||||
"github.com/mholt/certmagic"
|
||||
"github.com/mitchellh/go-ps"
|
||||
)
|
||||
|
||||
|
@ -126,6 +128,11 @@ func cmdRun() (int, error) {
|
|||
}
|
||||
}
|
||||
|
||||
// set a fitting User-Agent for ACME requests
|
||||
goModule := caddy.GoModule()
|
||||
cleanModVersion := strings.TrimPrefix(goModule.Version, "v")
|
||||
certmagic.UserAgent = "Caddy/" + cleanModVersion
|
||||
|
||||
// start the admin endpoint along with any initial config
|
||||
err := caddy.StartAdmin(config)
|
||||
if err != nil {
|
||||
|
@ -180,10 +187,10 @@ func cmdStop() (int, error) {
|
|||
}
|
||||
|
||||
func cmdVersion() (int, error) {
|
||||
goModule := getGoBuildModule()
|
||||
goModule := caddy.GoModule()
|
||||
if goModule.Sum != "" {
|
||||
// a build with a known version will also have a checksum
|
||||
fmt.Printf("%s (%s)\n", goModule.Version, goModule.Sum)
|
||||
fmt.Printf("%s %s\n", goModule.Version, goModule.Sum)
|
||||
} else {
|
||||
fmt.Println(goModule.Version)
|
||||
}
|
||||
|
|
21
cmd/main.go
21
cmd/main.go
|
@ -9,7 +9,6 @@ import (
|
|||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"runtime/debug"
|
||||
)
|
||||
|
||||
// Main executes the main function of the caddy command.
|
||||
|
@ -66,23 +65,3 @@ func handlePingbackConn(conn net.Conn, expect []byte) error {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// getGoBuildModule returns the build info of Caddy
|
||||
// from debug.BuildInfo (requires Go modules). If
|
||||
// no version information is available, a non-nil
|
||||
// value will still be returned, but with an
|
||||
// unknown version.
|
||||
func getGoBuildModule() *debug.Module {
|
||||
bi, ok := debug.ReadBuildInfo()
|
||||
if ok {
|
||||
// The recommended way to build Caddy involves
|
||||
// creating a separate main module, which
|
||||
// TODO: track related Go issue: https://github.com/golang/go/issues/29228
|
||||
for _, mod := range bi.Deps {
|
||||
if mod.Path == "github.com/mholt/caddy" {
|
||||
return mod
|
||||
}
|
||||
}
|
||||
}
|
||||
return &debug.Module{Version: "unknown"}
|
||||
}
|
||||
|
|
1
go.mod
1
go.mod
|
@ -17,6 +17,7 @@ require (
|
|||
github.com/klauspost/compress v1.7.1-0.20190613161414-0b31f265a57b
|
||||
github.com/klauspost/cpuid v1.2.1
|
||||
github.com/mholt/certmagic v0.6.2-0.20190624175158-6a42ef9fe8c2
|
||||
github.com/mitchellh/go-ps v0.0.0-20170309133038-4fdf99ab2936
|
||||
github.com/rs/cors v1.6.0
|
||||
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
|
||||
github.com/starlight-go/starlight v0.0.0-20181207205707-b06f321544f3
|
||||
|
|
2
go.sum
2
go.sum
|
@ -36,6 +36,8 @@ github.com/mholt/certmagic v0.6.2-0.20190624175158-6a42ef9fe8c2 h1:xKE9kZ5C8gelJ
|
|||
github.com/mholt/certmagic v0.6.2-0.20190624175158-6a42ef9fe8c2/go.mod h1:g4cOPxcjV0oFq3qwpjSA30LReKD8AoIfwAY9VvG35NY=
|
||||
github.com/miekg/dns v1.1.3 h1:1g0r1IvskvgL8rR+AcHzUA+oFmGcQlaIm4IqakufeMM=
|
||||
github.com/miekg/dns v1.1.3/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
|
||||
github.com/mitchellh/go-ps v0.0.0-20170309133038-4fdf99ab2936 h1:kw1v0NlnN+GZcU8Ma8CLF2Zzgjfx95gs3/GN3vYAPpo=
|
||||
github.com/mitchellh/go-ps v0.0.0-20170309133038-4fdf99ab2936/go.mod h1:r1VsdOzOPt1ZSrGZWFoNhsAedKnEd6r9Np1+5blZCWk=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rs/cors v1.6.0 h1:G9tHG9lebljV9mfp9SNPDL36nCDxmo3zTlAf1YgvzmI=
|
||||
|
|
Loading…
Reference in a new issue