MCPcopy
hub / github.com/caddyserver/caddy / buildHTTPServer

Function buildHTTPServer

modules/caddyhttp/staticresp.go:260–314  ·  view source on GitHub ↗
(
	i int,
	port uint,
	addr string,
	statusCode int,
	hdr http.Header,
	body string,
	accessLog bool,
)

Source from the content-addressed store, hash-verified

258}
259
260func buildHTTPServer(
261 i int,
262 port uint,
263 addr string,
264 statusCode int,
265 hdr http.Header,
266 body string,
267 accessLog bool,
268) (*Server, error) {
269 // nolint:prealloc
270 var handlers []json.RawMessage
271
272 // response body supports a basic template; evaluate it
273 tplCtx := struct {
274 N int // server number
275 Port uint // only the port
276 Address string // listener address
277 }{
278 N: i,
279 Port: port,
280 Address: addr,
281 }
282 tpl, err := template.New("body").Parse(body)
283 if err != nil {
284 return nil, err
285 }
286 buf := new(bytes.Buffer)
287 err = tpl.Execute(buf, tplCtx)
288 if err != nil {
289 return nil, err
290 }
291
292 // create route with handler
293 handler := StaticResponse{
294 StatusCode: WeakString(fmt.Sprintf("%d", statusCode)),
295 Headers: hdr,
296 Body: buf.String(),
297 }
298 handlers = append(handlers, caddyconfig.JSONModuleObject(handler, "handler", "static_response", nil))
299 route := Route{HandlersRaw: handlers}
300
301 server := &Server{
302 Listen: []string{addr},
303 ReadHeaderTimeout: caddy.Duration(10 * time.Second),
304 IdleTimeout: caddy.Duration(30 * time.Second),
305 MaxHeaderBytes: 1024 * 10,
306 Routes: RouteList{route},
307 AutoHTTPS: &AutoHTTPSConfig{DisableRedir: true},
308 }
309 if accessLog {
310 server.Logs = new(ServerLogConfig)
311 }
312
313 return server, nil
314}
315
316func cmdRespond(fl caddycmd.Flags) (int, error) {
317 caddy.TrapSignals()

Callers 1

cmdRespondFunction · 0.85

Calls 5

JSONModuleObjectFunction · 0.92
WeakStringTypeAlias · 0.85
ExecuteMethod · 0.80
DurationMethod · 0.80
StringMethod · 0.45

Tested by

no test coverage detected