| 1 |
package caddycmdscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/cmd`/ |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/" |
| 5 |
|
| 6 |
"github.com/spf13/cobrascip-go gomod github.com/spf13/cobra v1.10.2 `github.com/spf13/cobra`/" |
| 7 |
|
| 8 |
"github.com/caddyserver/caddy/v2scip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/" |
| 9 |
) |
| 10 |
|
| 11 |
var defaultFactoryscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/cmd`/defaultFactory. = newRootCommandFactoryscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/cmd`/newRootCommandFactory().(func() *cobrascip-go gomod github.com/spf13/cobra v1.10.2 `github.com/spf13/cobra`/.Commandscip-go gomod github.com/spf13/cobra v1.10.2 `github.com/spf13/cobra`/Command# { |
| 12 |
bin := caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.CustomBinaryNamescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/CustomBinaryName. |
| 13 |
if bin == "" { |
| 14 |
bin = "caddy" |
| 15 |
} |
| 16 |
|
| •17 |
short := "An extensible server platform" |
| 18 |
long := caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.CustomLongDescriptionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/CustomLongDescription. |
| 19 |
if long == "" { |
| 20 |
long = `Caddy is an extensible server platform written in Go. |
| 21 |
|
| 22 |
At its core, Caddy merely manages configuration. Modules are plugged |
| 23 |
in statically at compile-time to provide useful functionality. Caddy's |
| 24 |
standard distribution includes common modules to serve HTTP, TLS, |
| 25 |
and PKI applications, including the automation of certificates. |
| 26 |
|
| 27 |
To run Caddy, use: |
| 28 |
|
| 29 |
- 'caddy run' to run Caddy in the foreground (recommended). |
| 30 |
- 'caddy start' to start Caddy in the background; only do this |
| 31 |
if you will be keeping the terminal window open until you run |
| 32 |
'caddy stop' to close the server. |
| 33 |
|
| 34 |
When Caddy is started, it opens a locally-bound administrative socket |
| 35 |
to which configuration can be POSTed via a restful HTTP API (see |
| 36 |
https://caddyserver.com/docs/api). |
| 37 |
|
| 38 |
Caddy's native configuration format is JSON. However, config adapters |
| 39 |
can be used to convert other config formats to JSON when Caddy receives |
| 40 |
its configuration. The Caddyfile is a built-in config adapter that is |
| 41 |
popular for hand-written configurations due to its straightforward |
| 42 |
syntax (see https://caddyserver.com/docs/caddyfile). Many third-party |
| 43 |
adapters are available (see https://caddyserver.com/docs/config-adapters). |
| 44 |
Use 'caddy adapt' to see how a config translates to JSON. |
| 45 |
|
| 46 |
For convenience, the CLI can act as an HTTP client to give Caddy its |
| 47 |
initial configuration for you. If a file named Caddyfile is in the |
| 48 |
current working directory, it will do this automatically. Otherwise, |
| 49 |
you can use the --config flag to specify the path to a config file. |
| 50 |
|
| 51 |
Some special-purpose subcommands build and load a configuration file |
| 52 |
for you directly from command line input; for example: |
| 53 |
|
| 54 |
- caddy file-server |
| 55 |
- caddy reverse-proxy |
| 56 |
- caddy respond |
| 57 |
|
| 58 |
These commands disable the administration endpoint because their |
| 59 |
configuration is specified solely on the command line. |
| 60 |
|
| 61 |
In general, the most common way to run Caddy is simply: |
| 62 |
|
| 63 |
$ caddy run |
| 64 |
|
| 65 |
Or, with a configuration file: |
| 66 |
|
| 67 |
$ caddy run --config caddy.json |
| 68 |
|
| 69 |
If running interactively in a terminal, running Caddy in the |
| 70 |
background may be more convenient: |
| 71 |
|
| 72 |
$ caddy start |
| 73 |
... |
| 74 |
$ caddy stop |
| 75 |
|
| 76 |
This allows you to run other commands while Caddy stays running. |
| 77 |
Be sure to stop Caddy before you close the terminal! |
| 78 |
|
| 79 |
Depending on the system, Caddy may need permission to bind to low |
| 80 |
ports. One way to do this on Linux is to use setcap: |
| 81 |
|
| 82 |
$ sudo setcap cap_net_bind_service=+ep $(which caddy) |
| 83 |
|
| 84 |
Remember to run that command again after replacing the binary. |
| 85 |
|
| 86 |
See the Caddy website for tutorials, configuration structure, |
| 87 |
syntax, and module documentation: https://caddyserver.com/docs/ |
| 88 |
|
| 89 |
Custom Caddy builds are available on the Caddy download page at: |
| 90 |
https://caddyserver.com/download |
| 91 |
|
| 92 |
The xcaddy command can be used to build Caddy from source with or |
| 93 |
without additional plugins: https://github.com/caddyserver/xcaddy |
| 94 |
|
| 95 |
Where possible, Caddy should be installed using officially-supported |
| 96 |
package installers: https://caddyserver.com/docs/install |
| 97 |
|
| 98 |
Instructions for running Caddy in production are also available: |
| 99 |
https://caddyserver.com/docs/running |
| 100 |
` |
| 101 |
} |
| 102 |
|
| 103 |
return &cobrascip-go gomod github.com/spf13/cobra v1.10.2 `github.com/spf13/cobra`/.Commandscip-go gomod github.com/spf13/cobra v1.10.2 `github.com/spf13/cobra`/Command#{ |
| 104 |
Usescip-go gomod github.com/spf13/cobra v1.10.2 `github.com/spf13/cobra`/Command#Use.: bin, |
| •105 |
Shortscip-go gomod github.com/spf13/cobra v1.10.2 `github.com/spf13/cobra`/Command#Short.: short, |
| 106 |
Longscip-go gomod github.com/spf13/cobra v1.10.2 `github.com/spf13/cobra`/Command#Long.: long, |
| 107 |
Examplescip-go gomod github.com/spf13/cobra v1.10.2 `github.com/spf13/cobra`/Command#Example.: ` $ caddy run |
| 108 |
$ caddy run --config caddy.json |
| 109 |
$ caddy reload --config caddy.json |
| 110 |
$ caddy stop`, |
| 111 |
|
| 112 |
// kind of annoying to have all the help text printed out if |
| 113 |
// caddy has an error provisioning its modules, for instance... |
| 114 |
SilenceUsagescip-go gomod github.com/spf13/cobra v1.10.2 `github.com/spf13/cobra`/Command#SilenceUsage.: true, |
| 115 |
Versionscip-go gomod github.com/spf13/cobra v1.10.2 `github.com/spf13/cobra`/Command#Version.: onlyVersionTextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/cmd`/onlyVersionText().(), |
| 116 |
} |
| 117 |
}) |
| 118 |
|
| 119 |
const fullDocsFooterscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/cmd`/fullDocsFooter. = `Full documentation is available at: |
| 120 |
https://caddyserver.com/docs/command-line` |
| 121 |
|
| 122 |
func initscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/cmd`/init().() { |
| 123 |
defaultFactoryscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/cmd`/defaultFactory..Usescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/cmd`/rootCommandFactory#Use().(func(rootCmd *cobrascip-go gomod github.com/spf13/cobra v1.10.2 `github.com/spf13/cobra`/.Commandscip-go gomod github.com/spf13/cobra v1.10.2 `github.com/spf13/cobra`/Command#) { |
| 124 |
rootCmd.SetVersionTemplatescip-go gomod github.com/spf13/cobra v1.10.2 `github.com/spf13/cobra`/Command#SetVersionTemplate().("{{.Version}}\n") |
| 125 |
rootCmd.SetHelpTemplatescip-go gomod github.com/spf13/cobra v1.10.2 `github.com/spf13/cobra`/Command#SetHelpTemplate().(rootCmd.HelpTemplatescip-go gomod github.com/spf13/cobra v1.10.2 `github.com/spf13/cobra`/Command#HelpTemplate().() + "\n" + fullDocsFooterscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/cmd`/fullDocsFooter. + "\n") |
| 126 |
}) |
| 127 |
} |
| 128 |
|
| 129 |
func onlyVersionTextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/cmd`/onlyVersionText().() string { |
| 130 |
_, f := caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Versionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Version().() |
| 131 |
return f |
| 132 |
} |
| 133 |
|
| 134 |
func caddyCmdToCobrascip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/cmd`/caddyCmdToCobra().(caddyCmd Commandscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/cmd`/Command#) *cobrascip-go gomod github.com/spf13/cobra v1.10.2 `github.com/spf13/cobra`/.Commandscip-go gomod github.com/spf13/cobra v1.10.2 `github.com/spf13/cobra`/Command# { |
| 135 |
cmd := &cobrascip-go gomod github.com/spf13/cobra v1.10.2 `github.com/spf13/cobra`/.Commandscip-go gomod github.com/spf13/cobra v1.10.2 `github.com/spf13/cobra`/Command#{ |
| 136 |
Usescip-go gomod github.com/spf13/cobra v1.10.2 `github.com/spf13/cobra`/Command#Use.: caddyCmd.Namescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/cmd`/Command#Name. + " " + caddyCmd.Usagescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/cmd`/Command#Usage., |
| 137 |
Shortscip-go gomod github.com/spf13/cobra v1.10.2 `github.com/spf13/cobra`/Command#Short.: caddyCmd.Shortscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/cmd`/Command#Short., |
| 138 |
Longscip-go gomod github.com/spf13/cobra v1.10.2 `github.com/spf13/cobra`/Command#Long.: caddyCmd.Longscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/cmd`/Command#Long., |
| 139 |
} |
| 140 |
if caddyCmd.CobraFuncscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/cmd`/Command#CobraFunc. != nil { |
| 141 |
caddyCmd.CobraFuncscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/cmd`/Command#CobraFunc.(cmd) |
| 142 |
} else { |
| 143 |
cmd.RunEscip-go gomod github.com/spf13/cobra v1.10.2 `github.com/spf13/cobra`/Command#RunE. = WrapCommandFuncForCobrascip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/cmd`/WrapCommandFuncForCobra().(caddyCmd.Funcscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/cmd`/Command#Func.) |
| 144 |
cmd.Flagsscip-go gomod github.com/spf13/cobra v1.10.2 `github.com/spf13/cobra`/Command#Flags().().AddGoFlagSetscip-go gomod github.com/spf13/pflag v1.0.10 `github.com/spf13/pflag`/FlagSet#AddGoFlagSet().(caddyCmd.Flagsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/cmd`/Command#Flags.) |
| 145 |
} |
| 146 |
return cmd |
| 147 |
} |
| 148 |
|
| 149 |
// WrapCommandFuncForCobra wraps a Caddy CommandFunc for use |
| 150 |
// in a cobra command's RunE field. |
| 151 |
func WrapCommandFuncForCobrascip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/cmd`/WrapCommandFuncForCobra().(f CommandFuncscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/cmd`/CommandFunc#) func(cmd *cobrascip-go gomod github.com/spf13/cobra v1.10.2 `github.com/spf13/cobra`/.Commandscip-go gomod github.com/spf13/cobra v1.10.2 `github.com/spf13/cobra`/Command#, _ []string) error { |
| 152 |
return func(cmd *cobrascip-go gomod github.com/spf13/cobra v1.10.2 `github.com/spf13/cobra`/.Commandscip-go gomod github.com/spf13/cobra v1.10.2 `github.com/spf13/cobra`/Command#, _ []string) error { |
| 153 |
status, err := f(Flagsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/cmd`/Flags#{cmd.Flagsscip-go gomod github.com/spf13/cobra v1.10.2 `github.com/spf13/cobra`/Command#Flags().()}) |
| 154 |
if status > 1 { |
| 155 |
cmd.SilenceErrorsscip-go gomod github.com/spf13/cobra v1.10.2 `github.com/spf13/cobra`/Command#SilenceErrors. = true |
| 156 |
return &exitErrorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/cmd`/exitError#{ExitCodescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/cmd`/exitError#ExitCode.: status, Errscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/cmd`/exitError#Err.: err} |
| 157 |
} |
| 158 |
return err |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
// exitError carries the exit code from CommandFunc to Main() |
| 163 |
type exitErrorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/cmd`/exitError# struct { |
| 164 |
ExitCodescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/cmd`/exitError#ExitCode. int |
| 165 |
Errscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/cmd`/exitError#Err. error |
| 166 |
} |
| 167 |
|
| 168 |
func (e *exitErrorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/cmd`/exitError#) Errorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/cmd`/exitError#Error().() string { |
| 169 |
if e.Errscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/cmd`/exitError#Err. == nil { |
| 170 |
return fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Sprintf().("exiting with status %d", e.ExitCodescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/cmd`/exitError#ExitCode.) |
| 171 |
} |
| 172 |
return e.Errscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/cmd`/exitError#Err..Error() |
| 173 |
} |