2019-06-30 17:07:58 -05:00
|
|
|
// Copyright 2015 Matthew Holt and The Caddy Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-05-07 10:56:13 -05:00
|
|
|
package caddyhttp
|
2019-05-04 14:21:20 -05:00
|
|
|
|
|
|
|
import (
|
2022-08-01 14:36:22 -05:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2019-05-04 14:21:20 -05:00
|
|
|
"fmt"
|
2022-08-01 14:36:22 -05:00
|
|
|
"io"
|
2019-05-04 14:21:20 -05:00
|
|
|
"net/http"
|
2022-10-29 15:35:44 -05:00
|
|
|
"net/textproto"
|
2022-08-01 14:36:22 -05:00
|
|
|
"os"
|
2019-05-20 22:21:33 -05:00
|
|
|
"strconv"
|
2022-08-01 14:36:22 -05:00
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
"time"
|
2019-05-04 14:21:20 -05:00
|
|
|
|
2019-07-02 13:37:06 -05:00
|
|
|
"github.com/caddyserver/caddy/v2"
|
2022-08-01 14:36:22 -05:00
|
|
|
"github.com/caddyserver/caddy/v2/caddyconfig"
|
2019-08-09 13:05:47 -05:00
|
|
|
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
2022-08-01 14:36:22 -05:00
|
|
|
caddycmd "github.com/caddyserver/caddy/v2/cmd"
|
2023-02-24 18:09:12 -05:00
|
|
|
"github.com/spf13/cobra"
|
2022-09-16 00:10:16 -05:00
|
|
|
"go.uber.org/zap"
|
2019-05-04 14:21:20 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2019-08-21 11:46:35 -05:00
|
|
|
caddy.RegisterModule(StaticResponse{})
|
2022-08-01 14:36:22 -05:00
|
|
|
caddycmd.RegisterCommand(caddycmd.Command{
|
|
|
|
Name: "respond",
|
|
|
|
Usage: `[--status <code>] [--body <content>] [--listen <addr>] [--access-log] [--debug] [--header "Field: value"] <body|status>`,
|
|
|
|
Short: "Simple, hard-coded HTTP responses for development and testing",
|
|
|
|
Long: `
|
|
|
|
Spins up a quick-and-clean HTTP server for development and testing purposes.
|
|
|
|
|
|
|
|
With no options specified, this command listens on a random available port
|
|
|
|
and answers HTTP requests with an empty 200 response. The listen address can
|
|
|
|
be customized with the --listen flag and will always be printed to stdout.
|
|
|
|
If the listen address includes a port range, multiple servers will be started.
|
|
|
|
|
|
|
|
If a final, unnamed argument is given, it will be treated as a status code
|
|
|
|
(same as the --status flag) if it is a 3-digit number. Otherwise, it is used
|
|
|
|
as the response body (same as the --body flag). The --status and --body flags
|
|
|
|
will always override this argument (for example, to write a body that
|
|
|
|
literally says "404" but with a status code of 200, do '--status 200 404').
|
|
|
|
|
|
|
|
A body may be given in 3 ways: a flag, a final (and unnamed) argument to
|
|
|
|
the command, or piped to stdin (if flag and argument are unset). Limited
|
|
|
|
template evaluation is supported on the body, with the following variables:
|
|
|
|
|
|
|
|
{{.N}} The server number (useful if using a port range)
|
|
|
|
{{.Port}} The listener port
|
|
|
|
{{.Address}} The listener address
|
|
|
|
|
|
|
|
(See the docs for the text/template package in the Go standard library for
|
|
|
|
information about using templates: https://pkg.go.dev/text/template)
|
|
|
|
|
|
|
|
Access/request logging and more verbose debug logging can also be enabled.
|
|
|
|
|
|
|
|
Response headers may be added using the --header flag for each header field.
|
|
|
|
`,
|
2023-02-24 18:09:12 -05:00
|
|
|
CobraFunc: func(cmd *cobra.Command) {
|
|
|
|
cmd.Flags().StringP("listen", "l", ":0", "The address to which to bind the listener")
|
|
|
|
cmd.Flags().IntP("status", "s", http.StatusOK, "The response status code")
|
|
|
|
cmd.Flags().StringP("body", "b", "", "The body of the HTTP response")
|
|
|
|
cmd.Flags().BoolP("access-log", "", false, "Enable the access log")
|
|
|
|
cmd.Flags().BoolP("debug", "v", false, "Enable more verbose debug-level logging")
|
|
|
|
cmd.Flags().StringSliceP("header", "H", []string{}, "Set a header on the response (format: \"Field: value\")")
|
|
|
|
cmd.RunE = caddycmd.WrapCommandFuncForCobra(cmdRespond)
|
|
|
|
},
|
2022-08-01 14:36:22 -05:00
|
|
|
})
|
2019-05-04 14:21:20 -05:00
|
|
|
}
|
|
|
|
|
2019-07-11 18:02:57 -05:00
|
|
|
// StaticResponse implements a simple responder for static responses.
|
|
|
|
type StaticResponse struct {
|
2019-12-23 14:45:35 -05:00
|
|
|
// The HTTP status code to respond with. Can be an integer or,
|
|
|
|
// if needing to use a placeholder, a string.
|
2022-09-05 14:50:44 -05:00
|
|
|
//
|
|
|
|
// If the status code is 103 (Early Hints), the response headers
|
|
|
|
// will be written to the client immediately, the body will be
|
|
|
|
// ignored, and the next handler will be invoked. This behavior
|
|
|
|
// is EXPERIMENTAL while RFC 8297 is a draft, and may be changed
|
|
|
|
// or removed.
|
2019-12-23 14:45:35 -05:00
|
|
|
StatusCode WeakString `json:"status_code,omitempty"`
|
|
|
|
|
2022-08-31 10:43:46 -05:00
|
|
|
// Header fields to set on the response; overwrites any existing
|
|
|
|
// header fields of the same names after normalization.
|
2019-12-23 14:45:35 -05:00
|
|
|
Headers http.Header `json:"headers,omitempty"`
|
|
|
|
|
2022-08-31 10:43:46 -05:00
|
|
|
// The response body. If non-empty, the Content-Type header may
|
|
|
|
// be added automatically if it is not explicitly configured nor
|
|
|
|
// already set on the response; the default value is
|
|
|
|
// "text/plain; charset=utf-8" unless the body is a valid JSON object
|
|
|
|
// or array, in which case the value will be "application/json".
|
|
|
|
// Other than those common special cases the Content-Type header
|
|
|
|
// should be set explicitly if it is desired because MIME sniffing
|
|
|
|
// is disabled for safety.
|
2019-12-23 14:45:35 -05:00
|
|
|
Body string `json:"body,omitempty"`
|
|
|
|
|
|
|
|
// If true, the server will close the client's connection
|
|
|
|
// after writing the response.
|
|
|
|
Close bool `json:"close,omitempty"`
|
2021-01-28 14:54:55 -05:00
|
|
|
|
|
|
|
// Immediately and forcefully closes the connection without
|
|
|
|
// writing a response. Interrupts any other HTTP streams on
|
|
|
|
// the same connection.
|
|
|
|
Abort bool `json:"abort,omitempty"`
|
2019-05-04 14:21:20 -05:00
|
|
|
}
|
|
|
|
|
2019-08-21 11:46:35 -05:00
|
|
|
// CaddyModule returns the Caddy module information.
|
|
|
|
func (StaticResponse) CaddyModule() caddy.ModuleInfo {
|
|
|
|
return caddy.ModuleInfo{
|
2019-12-10 15:36:46 -05:00
|
|
|
ID: "http.handlers.static_response",
|
|
|
|
New: func() caddy.Module { return new(StaticResponse) },
|
2019-08-21 11:46:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-09 13:05:47 -05:00
|
|
|
// UnmarshalCaddyfile sets up the handler from Caddyfile tokens. Syntax:
|
|
|
|
//
|
2022-08-31 10:43:46 -05:00
|
|
|
// respond [<matcher>] <status>|<body> [<status>] {
|
|
|
|
// body <text>
|
|
|
|
// close
|
|
|
|
// }
|
2019-08-09 13:05:47 -05:00
|
|
|
//
|
2020-01-17 12:57:57 -05:00
|
|
|
// If there is just one argument (other than the matcher), it is considered
|
|
|
|
// to be a status code if it's a valid positive integer of 3 digits.
|
2019-08-09 13:05:47 -05:00
|
|
|
func (s *StaticResponse) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
|
|
|
|
for d.Next() {
|
2020-01-17 12:57:57 -05:00
|
|
|
args := d.RemainingArgs()
|
|
|
|
switch len(args) {
|
|
|
|
case 1:
|
|
|
|
if len(args[0]) == 3 {
|
|
|
|
if num, err := strconv.Atoi(args[0]); err == nil && num > 0 {
|
|
|
|
s.StatusCode = WeakString(args[0])
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.Body = args[0]
|
|
|
|
case 2:
|
|
|
|
s.Body = args[0]
|
|
|
|
s.StatusCode = WeakString(args[1])
|
|
|
|
default:
|
|
|
|
return d.ArgErr()
|
2019-08-09 13:05:47 -05:00
|
|
|
}
|
2020-01-17 12:57:57 -05:00
|
|
|
|
2019-09-10 20:21:52 -05:00
|
|
|
for d.NextBlock(0) {
|
2019-08-09 13:05:47 -05:00
|
|
|
switch d.Val() {
|
|
|
|
case "body":
|
|
|
|
if s.Body != "" {
|
|
|
|
return d.Err("body already specified")
|
|
|
|
}
|
2020-01-17 12:57:57 -05:00
|
|
|
if !d.AllArgs(&s.Body) {
|
2019-08-09 13:05:47 -05:00
|
|
|
return d.ArgErr()
|
|
|
|
}
|
|
|
|
case "close":
|
|
|
|
if s.Close {
|
|
|
|
return d.Err("close already specified")
|
|
|
|
}
|
|
|
|
s.Close = true
|
2021-02-02 18:19:58 -05:00
|
|
|
default:
|
|
|
|
return d.Errf("unrecognized subdirective '%s'", d.Val())
|
2019-08-09 13:05:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-05 14:50:44 -05:00
|
|
|
func (s StaticResponse) ServeHTTP(w http.ResponseWriter, r *http.Request, next Handler) error {
|
2021-01-28 14:54:55 -05:00
|
|
|
// close the connection immediately
|
|
|
|
if s.Abort {
|
|
|
|
panic(http.ErrAbortHandler)
|
|
|
|
}
|
2019-05-04 14:21:20 -05:00
|
|
|
|
2019-05-16 12:46:17 -05:00
|
|
|
// close the connection after responding
|
2021-01-28 14:54:55 -05:00
|
|
|
if s.Close {
|
|
|
|
r.Close = true
|
|
|
|
w.Header().Set("Connection", "close")
|
|
|
|
}
|
|
|
|
|
|
|
|
repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
|
2019-05-04 14:21:20 -05:00
|
|
|
|
2019-05-20 23:00:54 -05:00
|
|
|
// set all headers
|
2019-05-04 14:21:20 -05:00
|
|
|
for field, vals := range s.Headers {
|
2022-10-29 15:35:44 -05:00
|
|
|
field = textproto.CanonicalMIMEHeaderKey(repl.ReplaceAll(field, ""))
|
2019-06-21 15:36:26 -05:00
|
|
|
newVals := make([]string, len(vals))
|
2019-05-04 14:21:20 -05:00
|
|
|
for i := range vals {
|
2019-06-21 15:36:26 -05:00
|
|
|
newVals[i] = repl.ReplaceAll(vals[i], "")
|
2019-05-04 14:21:20 -05:00
|
|
|
}
|
2019-06-21 15:36:26 -05:00
|
|
|
w.Header()[field] = newVals
|
2019-05-04 14:21:20 -05:00
|
|
|
}
|
|
|
|
|
2022-08-31 10:43:46 -05:00
|
|
|
// implicitly set Content-Type header if we can do so safely
|
|
|
|
// (this allows templates handler to eval templates successfully
|
|
|
|
// or for clients to render JSON properly which is very common)
|
|
|
|
body := repl.ReplaceKnown(s.Body, "")
|
|
|
|
if body != "" && w.Header().Get("Content-Type") == "" {
|
|
|
|
content := strings.TrimSpace(s.Body)
|
|
|
|
if len(content) > 2 &&
|
|
|
|
(content[0] == '{' && content[len(content)-1] == '}' ||
|
|
|
|
(content[0] == '[' && content[len(content)-1] == ']')) &&
|
|
|
|
json.Valid([]byte(content)) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
} else {
|
|
|
|
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// do not allow Go to sniff the content-type, for safety
|
2019-06-07 20:59:17 -05:00
|
|
|
if w.Header().Get("Content-Type") == "" {
|
|
|
|
w.Header()["Content-Type"] = nil
|
|
|
|
}
|
|
|
|
|
2020-06-04 11:32:01 -05:00
|
|
|
// get the status code; if this handler exists in an error route,
|
|
|
|
// use the recommended status code as the default; otherwise 200
|
2019-07-09 13:58:39 -05:00
|
|
|
statusCode := http.StatusOK
|
2020-06-04 11:32:01 -05:00
|
|
|
if reqErr, ok := r.Context().Value(ErrorCtxKey).(error); ok {
|
|
|
|
if handlerErr, ok := reqErr.(HandlerError); ok {
|
|
|
|
if handlerErr.StatusCode > 0 {
|
|
|
|
statusCode = handlerErr.StatusCode
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-11 18:02:57 -05:00
|
|
|
if codeStr := s.StatusCode.String(); codeStr != "" {
|
|
|
|
intVal, err := strconv.Atoi(repl.ReplaceAll(codeStr, ""))
|
|
|
|
if err != nil {
|
|
|
|
return Error(http.StatusInternalServerError, err)
|
2019-05-20 22:21:33 -05:00
|
|
|
}
|
2019-07-11 18:02:57 -05:00
|
|
|
statusCode = intVal
|
2019-05-20 22:21:33 -05:00
|
|
|
}
|
2019-05-20 23:00:54 -05:00
|
|
|
|
|
|
|
// write headers
|
2019-05-04 14:21:20 -05:00
|
|
|
w.WriteHeader(statusCode)
|
|
|
|
|
2019-05-20 23:00:54 -05:00
|
|
|
// write response body
|
2022-09-05 14:50:44 -05:00
|
|
|
if statusCode != http.StatusEarlyHints && body != "" {
|
2022-08-31 10:43:46 -05:00
|
|
|
fmt.Fprint(w, body)
|
2019-05-04 14:21:20 -05:00
|
|
|
}
|
|
|
|
|
2022-09-05 14:50:44 -05:00
|
|
|
// continue handling after Early Hints as they are not the final response
|
|
|
|
if statusCode == http.StatusEarlyHints {
|
|
|
|
return next.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
2019-05-04 14:21:20 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-01 14:36:22 -05:00
|
|
|
func cmdRespond(fl caddycmd.Flags) (int, error) {
|
|
|
|
caddy.TrapSignals()
|
|
|
|
|
|
|
|
// get flag values
|
|
|
|
listen := fl.String("listen")
|
|
|
|
statusCodeFl := fl.Int("status")
|
|
|
|
bodyFl := fl.String("body")
|
|
|
|
accessLog := fl.Bool("access-log")
|
|
|
|
debug := fl.Bool("debug")
|
|
|
|
arg := fl.Arg(0)
|
|
|
|
|
|
|
|
if fl.NArg() > 1 {
|
|
|
|
return caddy.ExitCodeFailedStartup, fmt.Errorf("too many unflagged arguments")
|
|
|
|
}
|
|
|
|
|
|
|
|
// prefer status and body from explicit flags
|
|
|
|
statusCode, body := statusCodeFl, bodyFl
|
|
|
|
|
|
|
|
// figure out if status code was explicitly specified; this lets
|
|
|
|
// us set a non-zero value as the default but is a little hacky
|
|
|
|
var statusCodeFlagSpecified bool
|
|
|
|
for _, fl := range os.Args {
|
|
|
|
if fl == "--status" {
|
|
|
|
statusCodeFlagSpecified = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// try to determine what kind of parameter the unnamed argument is
|
|
|
|
if arg != "" {
|
|
|
|
// specifying body and status flags makes the argument redundant/unused
|
|
|
|
if bodyFl != "" && statusCodeFlagSpecified {
|
|
|
|
return caddy.ExitCodeFailedStartup, fmt.Errorf("unflagged argument \"%s\" is overridden by flags", arg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// if a valid 3-digit number, treat as status code; otherwise body
|
|
|
|
if argInt, err := strconv.Atoi(arg); err == nil && !statusCodeFlagSpecified {
|
|
|
|
if argInt >= 100 && argInt <= 999 {
|
|
|
|
statusCode = argInt
|
|
|
|
}
|
|
|
|
} else if body == "" {
|
|
|
|
body = arg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we still need a body, see if stdin is being piped
|
|
|
|
if body == "" {
|
|
|
|
stdinInfo, err := os.Stdin.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return caddy.ExitCodeFailedStartup, err
|
|
|
|
}
|
|
|
|
if stdinInfo.Mode()&os.ModeNamedPipe != 0 {
|
|
|
|
bodyBytes, err := io.ReadAll(os.Stdin)
|
|
|
|
if err != nil {
|
|
|
|
return caddy.ExitCodeFailedStartup, err
|
|
|
|
}
|
|
|
|
body = string(bodyBytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// build headers map
|
2023-02-24 18:09:12 -05:00
|
|
|
headers, err := fl.GetStringSlice("header")
|
|
|
|
if err != nil {
|
|
|
|
return caddy.ExitCodeFailedStartup, fmt.Errorf("invalid header flag: %v", err)
|
|
|
|
}
|
2022-08-01 14:36:22 -05:00
|
|
|
hdr := make(http.Header)
|
2023-02-24 18:09:12 -05:00
|
|
|
for i, h := range headers {
|
2022-08-02 15:39:09 -05:00
|
|
|
key, val, found := strings.Cut(h, ":")
|
2022-08-01 14:36:22 -05:00
|
|
|
key, val = strings.TrimSpace(key), strings.TrimSpace(val)
|
|
|
|
if !found || key == "" || val == "" {
|
|
|
|
return caddy.ExitCodeFailedStartup, fmt.Errorf("header %d: invalid format \"%s\" (expecting \"Field: value\")", i, h)
|
|
|
|
}
|
|
|
|
hdr.Set(key, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
// expand listen address, if more than one port
|
|
|
|
listenAddr, err := caddy.ParseNetworkAddress(listen)
|
|
|
|
if err != nil {
|
|
|
|
return caddy.ExitCodeFailedStartup, err
|
|
|
|
}
|
|
|
|
listenAddrs := make([]string, 0, listenAddr.PortRangeSize())
|
|
|
|
for offset := uint(0); offset < listenAddr.PortRangeSize(); offset++ {
|
|
|
|
listenAddrs = append(listenAddrs, listenAddr.JoinHostPort(offset))
|
|
|
|
}
|
|
|
|
|
|
|
|
// build each HTTP server
|
|
|
|
httpApp := App{Servers: make(map[string]*Server)}
|
|
|
|
|
|
|
|
for i, addr := range listenAddrs {
|
|
|
|
var handlers []json.RawMessage
|
|
|
|
|
|
|
|
// response body supports a basic template; evaluate it
|
|
|
|
tplCtx := struct {
|
|
|
|
N int // server number
|
|
|
|
Port uint // only the port
|
|
|
|
Address string // listener address
|
|
|
|
}{
|
|
|
|
N: i,
|
|
|
|
Port: listenAddr.StartPort + uint(i),
|
|
|
|
Address: addr,
|
|
|
|
}
|
|
|
|
tpl, err := template.New("body").Parse(body)
|
|
|
|
if err != nil {
|
|
|
|
return caddy.ExitCodeFailedStartup, err
|
|
|
|
}
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
err = tpl.Execute(buf, tplCtx)
|
|
|
|
if err != nil {
|
|
|
|
return caddy.ExitCodeFailedStartup, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// create route with handler
|
|
|
|
handler := StaticResponse{
|
|
|
|
StatusCode: WeakString(fmt.Sprintf("%d", statusCode)),
|
|
|
|
Headers: hdr,
|
|
|
|
Body: buf.String(),
|
|
|
|
}
|
|
|
|
handlers = append(handlers, caddyconfig.JSONModuleObject(handler, "handler", "static_response", nil))
|
|
|
|
route := Route{HandlersRaw: handlers}
|
|
|
|
|
|
|
|
server := &Server{
|
|
|
|
Listen: []string{addr},
|
|
|
|
ReadHeaderTimeout: caddy.Duration(10 * time.Second),
|
|
|
|
IdleTimeout: caddy.Duration(30 * time.Second),
|
|
|
|
MaxHeaderBytes: 1024 * 10,
|
|
|
|
Routes: RouteList{route},
|
|
|
|
AutoHTTPS: &AutoHTTPSConfig{DisableRedir: true},
|
|
|
|
}
|
|
|
|
if accessLog {
|
|
|
|
server.Logs = new(ServerLogConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
// save server
|
|
|
|
httpApp.Servers[fmt.Sprintf("static%d", i)] = server
|
|
|
|
}
|
|
|
|
|
|
|
|
// finish building the config
|
|
|
|
var false bool
|
|
|
|
cfg := &caddy.Config{
|
|
|
|
Admin: &caddy.AdminConfig{
|
|
|
|
Disabled: true,
|
|
|
|
Config: &caddy.ConfigSettings{
|
|
|
|
Persist: &false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
AppsRaw: caddy.ModuleMap{
|
|
|
|
"http": caddyconfig.JSON(httpApp, nil),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if debug {
|
|
|
|
cfg.Logging = &caddy.Logging{
|
|
|
|
Logs: map[string]*caddy.CustomLog{
|
2023-03-27 16:41:24 -05:00
|
|
|
"default": {BaseLog: caddy.BaseLog{Level: zap.DebugLevel.CapitalString()}},
|
2022-08-01 14:36:22 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// run it!
|
|
|
|
err = caddy.Run(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return caddy.ExitCodeFailedStartup, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// to print listener addresses, get the active HTTP app
|
|
|
|
loadedHTTPApp, err := caddy.ActiveContext().App("http")
|
|
|
|
if err != nil {
|
|
|
|
return caddy.ExitCodeFailedStartup, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// print each listener address
|
|
|
|
for _, srv := range loadedHTTPApp.(*App).Servers {
|
|
|
|
for _, ln := range srv.listeners {
|
|
|
|
fmt.Printf("Server address: %s\n", ln.Addr())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
select {}
|
|
|
|
}
|
|
|
|
|
2019-09-16 12:04:18 -05:00
|
|
|
// Interface guards
|
|
|
|
var (
|
|
|
|
_ MiddlewareHandler = (*StaticResponse)(nil)
|
|
|
|
_ caddyfile.Unmarshaler = (*StaticResponse)(nil)
|
|
|
|
)
|