caddy.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. // Package caddy provides a PHP module for the Caddy web server.
  2. // FrankenPHP embeds the PHP interpreter directly in Caddy, giving it the ability to run your PHP scripts directly.
  3. // No PHP FPM required!
  4. package caddy
  5. import (
  6. "encoding/json"
  7. "errors"
  8. "fmt"
  9. "github.com/dunglas/frankenphp/internal/fastabs"
  10. "github.com/prometheus/client_golang/prometheus"
  11. "net/http"
  12. "path/filepath"
  13. "strconv"
  14. "strings"
  15. "github.com/caddyserver/caddy/v2"
  16. "github.com/caddyserver/caddy/v2/caddyconfig"
  17. "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
  18. "github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
  19. "github.com/caddyserver/caddy/v2/modules/caddyhttp"
  20. "github.com/caddyserver/caddy/v2/modules/caddyhttp/fileserver"
  21. "github.com/caddyserver/caddy/v2/modules/caddyhttp/rewrite"
  22. "github.com/dunglas/frankenphp"
  23. "go.uber.org/zap"
  24. )
  25. const defaultDocumentRoot = "public"
  26. func init() {
  27. caddy.RegisterModule(FrankenPHPApp{})
  28. caddy.RegisterModule(FrankenPHPModule{})
  29. httpcaddyfile.RegisterGlobalOption("frankenphp", parseGlobalOption)
  30. httpcaddyfile.RegisterHandlerDirective("php", parseCaddyfile)
  31. httpcaddyfile.RegisterDirectiveOrder("php", "before", "file_server")
  32. httpcaddyfile.RegisterDirective("php_server", parsePhpServer)
  33. httpcaddyfile.RegisterDirectiveOrder("php_server", "before", "file_server")
  34. }
  35. var metrics = frankenphp.NewPrometheusMetrics(prometheus.DefaultRegisterer)
  36. type workerConfig struct {
  37. // FileName sets the path to the worker script.
  38. FileName string `json:"file_name,omitempty"`
  39. // Num sets the number of workers to start.
  40. Num int `json:"num,omitempty"`
  41. // Env sets an extra environment variable to the given value. Can be specified more than once for multiple environment variables.
  42. Env map[string]string `json:"env,omitempty"`
  43. // Directories to watch for file changes
  44. Watch []string `json:"watch,omitempty"`
  45. }
  46. type FrankenPHPApp struct {
  47. // NumThreads sets the number of PHP threads to start. Default: 2x the number of available CPUs.
  48. NumThreads int `json:"num_threads,omitempty"`
  49. // Workers configures the worker scripts to start.
  50. Workers []workerConfig `json:"workers,omitempty"`
  51. }
  52. // CaddyModule returns the Caddy module information.
  53. func (f FrankenPHPApp) CaddyModule() caddy.ModuleInfo {
  54. return caddy.ModuleInfo{
  55. ID: "frankenphp",
  56. New: func() caddy.Module { return &f },
  57. }
  58. }
  59. func (f *FrankenPHPApp) Start() error {
  60. repl := caddy.NewReplacer()
  61. logger := caddy.Log()
  62. opts := []frankenphp.Option{frankenphp.WithNumThreads(f.NumThreads), frankenphp.WithLogger(logger), frankenphp.WithMetrics(metrics)}
  63. for _, w := range f.Workers {
  64. opts = append(opts, frankenphp.WithWorkers(repl.ReplaceKnown(w.FileName, ""), w.Num, w.Env, w.Watch))
  65. }
  66. frankenphp.Shutdown()
  67. if err := frankenphp.Init(opts...); err != nil {
  68. return err
  69. }
  70. return nil
  71. }
  72. func (f *FrankenPHPApp) Stop() error {
  73. caddy.Log().Info("FrankenPHP stopped 🐘")
  74. // attempt a graceful shutdown if caddy is exiting
  75. // note: Exiting() is currently marked as 'experimental'
  76. // https://github.com/caddyserver/caddy/blob/e76405d55058b0a3e5ba222b44b5ef00516116aa/caddy.go#L810
  77. if caddy.Exiting() {
  78. frankenphp.Shutdown()
  79. }
  80. // reset configuration so it doesn't bleed into later tests
  81. f.Workers = nil
  82. f.NumThreads = 0
  83. return nil
  84. }
  85. // UnmarshalCaddyfile implements caddyfile.Unmarshaler.
  86. func (f *FrankenPHPApp) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
  87. for d.Next() {
  88. for d.NextBlock(0) {
  89. switch d.Val() {
  90. case "num_threads":
  91. if !d.NextArg() {
  92. return d.ArgErr()
  93. }
  94. v, err := strconv.Atoi(d.Val())
  95. if err != nil {
  96. return err
  97. }
  98. f.NumThreads = v
  99. case "worker":
  100. wc := workerConfig{}
  101. if d.NextArg() {
  102. wc.FileName = d.Val()
  103. }
  104. if d.NextArg() {
  105. v, err := strconv.Atoi(d.Val())
  106. if err != nil {
  107. return err
  108. }
  109. wc.Num = v
  110. }
  111. for d.NextBlock(1) {
  112. v := d.Val()
  113. switch v {
  114. case "file":
  115. if !d.NextArg() {
  116. return d.ArgErr()
  117. }
  118. wc.FileName = d.Val()
  119. case "num":
  120. if !d.NextArg() {
  121. return d.ArgErr()
  122. }
  123. v, err := strconv.Atoi(d.Val())
  124. if err != nil {
  125. return err
  126. }
  127. wc.Num = v
  128. case "env":
  129. args := d.RemainingArgs()
  130. if len(args) != 2 {
  131. return d.ArgErr()
  132. }
  133. if wc.Env == nil {
  134. wc.Env = make(map[string]string)
  135. }
  136. wc.Env[args[0]] = args[1]
  137. case "watch":
  138. if !d.NextArg() {
  139. // the default if the watch directory is left empty:
  140. wc.Watch = append(wc.Watch, "./**/*.{php,yaml,yml,twig,env}")
  141. } else {
  142. wc.Watch = append(wc.Watch, d.Val())
  143. }
  144. }
  145. if wc.FileName == "" {
  146. return errors.New(`the "file" argument must be specified`)
  147. }
  148. if frankenphp.EmbeddedAppPath != "" && filepath.IsLocal(wc.FileName) {
  149. wc.FileName = filepath.Join(frankenphp.EmbeddedAppPath, wc.FileName)
  150. }
  151. }
  152. f.Workers = append(f.Workers, wc)
  153. }
  154. }
  155. }
  156. return nil
  157. }
  158. func parseGlobalOption(d *caddyfile.Dispenser, _ interface{}) (interface{}, error) {
  159. app := &FrankenPHPApp{}
  160. if err := app.UnmarshalCaddyfile(d); err != nil {
  161. return nil, err
  162. }
  163. // tell Caddyfile adapter that this is the JSON for an app
  164. return httpcaddyfile.App{
  165. Name: "frankenphp",
  166. Value: caddyconfig.JSON(app, nil),
  167. }, nil
  168. }
  169. type FrankenPHPModule struct {
  170. // Root sets the root folder to the site. Default: `root` directive, or the path of the public directory of the embed app it exists.
  171. Root string `json:"root,omitempty"`
  172. // SplitPath sets the substrings for splitting the URI into two parts. The first matching substring will be used to split the "path info" from the path. The first piece is suffixed with the matching substring and will be assumed as the actual resource (CGI script) name. The second piece will be set to PATH_INFO for the CGI script to use. Default: `.php`.
  173. SplitPath []string `json:"split_path,omitempty"`
  174. // ResolveRootSymlink enables resolving the `root` directory to its actual value by evaluating a symbolic link, if one exists.
  175. ResolveRootSymlink *bool `json:"resolve_root_symlink,omitempty"`
  176. // Env sets an extra environment variable to the given value. Can be specified more than once for multiple environment variables.
  177. Env map[string]string `json:"env,omitempty"`
  178. resolvedDocumentRoot string
  179. preparedEnv frankenphp.PreparedEnv
  180. preparedEnvNeedsReplacement bool
  181. logger *zap.Logger
  182. }
  183. // CaddyModule returns the Caddy module information.
  184. func (FrankenPHPModule) CaddyModule() caddy.ModuleInfo {
  185. return caddy.ModuleInfo{
  186. ID: "http.handlers.php",
  187. New: func() caddy.Module { return new(FrankenPHPModule) },
  188. }
  189. }
  190. // Provision sets up the module.
  191. func (f *FrankenPHPModule) Provision(ctx caddy.Context) error {
  192. f.logger = ctx.Logger(f)
  193. if f.Root == "" {
  194. if frankenphp.EmbeddedAppPath == "" {
  195. f.Root = "{http.vars.root}"
  196. } else {
  197. rrs := false
  198. f.Root = filepath.Join(frankenphp.EmbeddedAppPath, defaultDocumentRoot)
  199. f.ResolveRootSymlink = &rrs
  200. }
  201. } else {
  202. if frankenphp.EmbeddedAppPath != "" && filepath.IsLocal(f.Root) {
  203. f.Root = filepath.Join(frankenphp.EmbeddedAppPath, f.Root)
  204. }
  205. }
  206. if len(f.SplitPath) == 0 {
  207. f.SplitPath = []string{".php"}
  208. }
  209. if f.ResolveRootSymlink == nil {
  210. rrs := true
  211. f.ResolveRootSymlink = &rrs
  212. }
  213. if !needReplacement(f.Root) {
  214. root, err := fastabs.FastAbs(f.Root)
  215. if err != nil {
  216. return fmt.Errorf("unable to make the root path absolute: %w", err)
  217. }
  218. f.resolvedDocumentRoot = root
  219. if *f.ResolveRootSymlink {
  220. root, err := filepath.EvalSymlinks(root)
  221. if err != nil {
  222. return fmt.Errorf("unable to resolve root symlink: %w", err)
  223. }
  224. f.resolvedDocumentRoot = root
  225. }
  226. }
  227. if f.preparedEnv == nil {
  228. f.preparedEnv = frankenphp.PrepareEnv(f.Env)
  229. for _, e := range f.preparedEnv {
  230. if needReplacement(e) {
  231. f.preparedEnvNeedsReplacement = true
  232. break
  233. }
  234. }
  235. }
  236. return nil
  237. }
  238. // needReplacement checks if a string contains placeholders.
  239. func needReplacement(s string) bool {
  240. return strings.Contains(s, "{") || strings.Contains(s, "}")
  241. }
  242. // ServeHTTP implements caddyhttp.MiddlewareHandler.
  243. // TODO: Expose TLS versions as env vars, as Apache's mod_ssl: https://github.com/caddyserver/caddy/blob/master/modules/caddyhttp/reverseproxy/fastcgi/fastcgi.go#L298
  244. func (f FrankenPHPModule) ServeHTTP(w http.ResponseWriter, r *http.Request, _ caddyhttp.Handler) error {
  245. origReq := r.Context().Value(caddyhttp.OriginalRequestCtxKey).(http.Request)
  246. repl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)
  247. var documentRootOption frankenphp.RequestOption
  248. if f.resolvedDocumentRoot == "" {
  249. documentRootOption = frankenphp.WithRequestDocumentRoot(repl.ReplaceKnown(f.Root, ""), *f.ResolveRootSymlink)
  250. } else {
  251. documentRootOption = frankenphp.WithRequestResolvedDocumentRoot(f.resolvedDocumentRoot)
  252. }
  253. env := make(map[string]string, len(f.preparedEnv)+1)
  254. env["REQUEST_URI\x00"] = origReq.URL.RequestURI()
  255. for k, v := range f.preparedEnv {
  256. if f.preparedEnvNeedsReplacement {
  257. env[k] = repl.ReplaceKnown(v, "")
  258. } else {
  259. env[k] = v
  260. }
  261. }
  262. fr, err := frankenphp.NewRequestWithContext(
  263. r,
  264. documentRootOption,
  265. frankenphp.WithRequestSplitPath(f.SplitPath),
  266. frankenphp.WithRequestPreparedEnv(env),
  267. )
  268. if err != nil {
  269. return err
  270. }
  271. return frankenphp.ServeHTTP(w, fr)
  272. }
  273. // UnmarshalCaddyfile implements caddyfile.Unmarshaler.
  274. func (f *FrankenPHPModule) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
  275. for d.Next() {
  276. for d.NextBlock(0) {
  277. switch d.Val() {
  278. case "root":
  279. if !d.NextArg() {
  280. return d.ArgErr()
  281. }
  282. f.Root = d.Val()
  283. case "split":
  284. f.SplitPath = d.RemainingArgs()
  285. if len(f.SplitPath) == 0 {
  286. return d.ArgErr()
  287. }
  288. case "env":
  289. args := d.RemainingArgs()
  290. if len(args) != 2 {
  291. return d.ArgErr()
  292. }
  293. if f.Env == nil {
  294. f.Env = make(map[string]string)
  295. f.preparedEnv = make(frankenphp.PreparedEnv)
  296. }
  297. f.Env[args[0]] = args[1]
  298. f.preparedEnv[args[0]+"\x00"] = args[1]
  299. case "resolve_root_symlink":
  300. if !d.NextArg() {
  301. continue
  302. }
  303. v, err := strconv.ParseBool(d.Val())
  304. if err != nil {
  305. return err
  306. }
  307. if d.NextArg() {
  308. return d.ArgErr()
  309. }
  310. f.ResolveRootSymlink = &v
  311. }
  312. }
  313. }
  314. return nil
  315. }
  316. // parseCaddyfile unmarshals tokens from h into a new Middleware.
  317. func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
  318. m := FrankenPHPModule{}
  319. err := m.UnmarshalCaddyfile(h.Dispenser)
  320. return m, err
  321. }
  322. // parsePhpServer parses the php_server directive, which has a similar syntax
  323. // to the php_fastcgi directive. A line such as this:
  324. //
  325. // php_server
  326. //
  327. // is equivalent to a route consisting of:
  328. //
  329. // # Add trailing slash for directory requests
  330. // @canonicalPath {
  331. // file {path}/index.php
  332. // not path */
  333. // }
  334. // redir @canonicalPath {path}/ 308
  335. //
  336. // # If the requested file does not exist, try index files
  337. // @indexFiles file {
  338. // try_files {path} {path}/index.php index.php
  339. // split_path .php
  340. // }
  341. // rewrite @indexFiles {http.matchers.file.relative}
  342. //
  343. // # FrankenPHP!
  344. // @phpFiles path *.php
  345. // php @phpFiles
  346. // file_server
  347. //
  348. // parsePhpServer is freely inspired from the php_fastgci directive of the Caddy server (Apache License 2.0, Matthew Holt and The Caddy Authors)
  349. func parsePhpServer(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error) {
  350. if !h.Next() {
  351. return nil, h.ArgErr()
  352. }
  353. // set up FrankenPHP
  354. phpsrv := FrankenPHPModule{}
  355. // set up file server
  356. fsrv := fileserver.FileServer{}
  357. disableFsrv := false
  358. // set up the set of file extensions allowed to execute PHP code
  359. extensions := []string{".php"}
  360. // set the default index file for the try_files rewrites
  361. indexFile := "index.php"
  362. // set up for explicitly overriding try_files
  363. var tryFiles []string
  364. // if the user specified a matcher token, use that
  365. // matcher in a route that wraps both of our routes;
  366. // either way, strip the matcher token and pass
  367. // the remaining tokens to the unmarshaler so that
  368. // we can gain the rest of the directive syntax
  369. userMatcherSet, err := h.ExtractMatcherSet()
  370. if err != nil {
  371. return nil, err
  372. }
  373. // make a new dispenser from the remaining tokens so that we
  374. // can reset the dispenser back to this point for the
  375. // php unmarshaler to read from it as well
  376. dispenser := h.NewFromNextSegment()
  377. // read the subdirectives that we allow as overrides to
  378. // the php_server shortcut
  379. // NOTE: we delete the tokens as we go so that the php
  380. // unmarshal doesn't see these subdirectives which it cannot handle
  381. for dispenser.Next() {
  382. for dispenser.NextBlock(0) {
  383. // ignore any sub-subdirectives that might
  384. // have the same name somewhere within
  385. // the php passthrough tokens
  386. if dispenser.Nesting() != 1 {
  387. continue
  388. }
  389. // parse the php_server subdirectives
  390. switch dispenser.Val() {
  391. case "root":
  392. if !dispenser.NextArg() {
  393. return nil, dispenser.ArgErr()
  394. }
  395. phpsrv.Root = dispenser.Val()
  396. fsrv.Root = phpsrv.Root
  397. dispenser.DeleteN(2)
  398. case "split":
  399. extensions = dispenser.RemainingArgs()
  400. dispenser.DeleteN(len(extensions) + 1)
  401. if len(extensions) == 0 {
  402. return nil, dispenser.ArgErr()
  403. }
  404. case "index":
  405. args := dispenser.RemainingArgs()
  406. dispenser.DeleteN(len(args) + 1)
  407. if len(args) != 1 {
  408. return nil, dispenser.ArgErr()
  409. }
  410. indexFile = args[0]
  411. case "try_files":
  412. args := dispenser.RemainingArgs()
  413. dispenser.DeleteN(len(args) + 1)
  414. if len(args) < 1 {
  415. return nil, dispenser.ArgErr()
  416. }
  417. tryFiles = args
  418. case "file_server":
  419. args := dispenser.RemainingArgs()
  420. dispenser.DeleteN(len(args) + 1)
  421. if len(args) < 1 || args[0] != "off" {
  422. return nil, dispenser.ArgErr()
  423. }
  424. disableFsrv = true
  425. }
  426. }
  427. }
  428. // reset the dispenser after we're done so that the frankenphp
  429. // unmarshaler can read it from the start
  430. dispenser.Reset()
  431. if frankenphp.EmbeddedAppPath != "" {
  432. if phpsrv.Root == "" {
  433. phpsrv.Root = filepath.Join(frankenphp.EmbeddedAppPath, defaultDocumentRoot)
  434. fsrv.Root = phpsrv.Root
  435. rrs := false
  436. phpsrv.ResolveRootSymlink = &rrs
  437. } else if filepath.IsLocal(fsrv.Root) {
  438. phpsrv.Root = filepath.Join(frankenphp.EmbeddedAppPath, phpsrv.Root)
  439. fsrv.Root = phpsrv.Root
  440. }
  441. }
  442. // set up a route list that we'll append to
  443. routes := caddyhttp.RouteList{}
  444. // set the list of allowed path segments on which to split
  445. phpsrv.SplitPath = extensions
  446. // if the index is turned off, we skip the redirect and try_files
  447. if indexFile != "off" {
  448. dirRedir := false
  449. dirIndex := "{http.request.uri.path}/" + indexFile
  450. tryPolicy := "first_exist_fallback"
  451. // if tryFiles wasn't overridden, use a reasonable default
  452. if len(tryFiles) == 0 {
  453. if disableFsrv {
  454. tryFiles = []string{dirIndex, indexFile}
  455. } else {
  456. tryFiles = []string{"{http.request.uri.path}", dirIndex, indexFile}
  457. }
  458. dirRedir = true
  459. } else {
  460. if !strings.HasSuffix(tryFiles[len(tryFiles)-1], ".php") {
  461. // use first_exist strategy if the last file is not a PHP file
  462. tryPolicy = ""
  463. }
  464. for _, tf := range tryFiles {
  465. if tf == dirIndex {
  466. dirRedir = true
  467. break
  468. }
  469. }
  470. }
  471. // route to redirect to canonical path if index PHP file
  472. if dirRedir {
  473. redirMatcherSet := caddy.ModuleMap{
  474. "file": h.JSON(fileserver.MatchFile{
  475. TryFiles: []string{dirIndex},
  476. }),
  477. "not": h.JSON(caddyhttp.MatchNot{
  478. MatcherSetsRaw: []caddy.ModuleMap{
  479. {
  480. "path": h.JSON(caddyhttp.MatchPath{"*/"}),
  481. },
  482. },
  483. }),
  484. }
  485. redirHandler := caddyhttp.StaticResponse{
  486. StatusCode: caddyhttp.WeakString(strconv.Itoa(http.StatusPermanentRedirect)),
  487. Headers: http.Header{"Location": []string{"{http.request.orig_uri.path}/"}},
  488. }
  489. redirRoute := caddyhttp.Route{
  490. MatcherSetsRaw: []caddy.ModuleMap{redirMatcherSet},
  491. HandlersRaw: []json.RawMessage{caddyconfig.JSONModuleObject(redirHandler, "handler", "static_response", nil)},
  492. }
  493. routes = append(routes, redirRoute)
  494. }
  495. // route to rewrite to PHP index file
  496. rewriteMatcherSet := caddy.ModuleMap{
  497. "file": h.JSON(fileserver.MatchFile{
  498. TryFiles: tryFiles,
  499. TryPolicy: tryPolicy,
  500. SplitPath: extensions,
  501. }),
  502. }
  503. rewriteHandler := rewrite.Rewrite{
  504. URI: "{http.matchers.file.relative}",
  505. }
  506. rewriteRoute := caddyhttp.Route{
  507. MatcherSetsRaw: []caddy.ModuleMap{rewriteMatcherSet},
  508. HandlersRaw: []json.RawMessage{caddyconfig.JSONModuleObject(rewriteHandler, "handler", "rewrite", nil)},
  509. }
  510. routes = append(routes, rewriteRoute)
  511. }
  512. // route to actually pass requests to PHP files;
  513. // match only requests that are for PHP files
  514. var pathList []string
  515. for _, ext := range extensions {
  516. pathList = append(pathList, "*"+ext)
  517. }
  518. phpMatcherSet := caddy.ModuleMap{
  519. "path": h.JSON(pathList),
  520. }
  521. // the rest of the config is specified by the user
  522. // using the php directive syntax
  523. dispenser.Next() // consume the directive name
  524. err = phpsrv.UnmarshalCaddyfile(dispenser)
  525. if err != nil {
  526. return nil, err
  527. }
  528. // create the PHP route which is
  529. // conditional on matching PHP files
  530. phpRoute := caddyhttp.Route{
  531. MatcherSetsRaw: []caddy.ModuleMap{phpMatcherSet},
  532. HandlersRaw: []json.RawMessage{caddyconfig.JSONModuleObject(phpsrv, "handler", "php", nil)},
  533. }
  534. routes = append(routes, phpRoute)
  535. // create the file server route
  536. if !disableFsrv {
  537. fileRoute := caddyhttp.Route{
  538. MatcherSetsRaw: []caddy.ModuleMap{},
  539. HandlersRaw: []json.RawMessage{caddyconfig.JSONModuleObject(fsrv, "handler", "file_server", nil)},
  540. }
  541. routes = append(routes, fileRoute)
  542. }
  543. subroute := caddyhttp.Subroute{
  544. Routes: routes,
  545. }
  546. // the user's matcher is a prerequisite for ours, so
  547. // wrap ours in a subroute and return that
  548. if userMatcherSet != nil {
  549. return []httpcaddyfile.ConfigValue{
  550. {
  551. Class: "route",
  552. Value: caddyhttp.Route{
  553. MatcherSetsRaw: []caddy.ModuleMap{userMatcherSet},
  554. HandlersRaw: []json.RawMessage{caddyconfig.JSONModuleObject(subroute, "handler", "subroute", nil)},
  555. },
  556. },
  557. }, nil
  558. }
  559. // otherwise, return the literal subroute instead of
  560. // individual routes, to ensure they stay together and
  561. // are treated as a single unit, without necessarily
  562. // creating an actual subroute in the output
  563. return []httpcaddyfile.ConfigValue{
  564. {
  565. Class: "route",
  566. Value: subroute,
  567. },
  568. }, nil
  569. }
  570. // Interface guards
  571. var (
  572. _ caddy.App = (*FrankenPHPApp)(nil)
  573. _ caddy.Provisioner = (*FrankenPHPModule)(nil)
  574. _ caddyhttp.MiddlewareHandler = (*FrankenPHPModule)(nil)
  575. _ caddyfile.Unmarshaler = (*FrankenPHPModule)(nil)
  576. )