Documentation
¶
Overview ¶
Package frankenphp embeds PHP in Go projects and provides a SAPI for net/http.
This is the core of the FrankenPHP app server, and can be used in any Go program.
Index ¶
- Constants
- Variables
- func DrainWorkers()
- func ExecutePHPCode(phpCode string) int
- func ExecuteScriptCLI(script string, args []string) int
- func GoMap(arr unsafe.Pointer) map[string]any
- func GoPackedArray(arr unsafe.Pointer) []any
- func GoString(s unsafe.Pointer) string
- func Init(options ...Option) error
- func NewRequestWithContext(r *http.Request, opts ...RequestOption) (*http.Request, error)
- func PHPAssociativeArray(arr AssociativeArray) unsafe.Pointer
- func PHPMap(arr map[string]any) unsafe.Pointer
- func PHPPackedArray(slice []any) unsafe.Pointer
- func PHPString(s string, persistent bool) unsafe.Pointer
- func RegisterExtension(me unsafe.Pointer)
- func RestartWorkers()
- func ServeHTTP(responseWriter http.ResponseWriter, request *http.Request) error
- func Shutdown()
- type AssociativeArray
- type FrankenPHPDebugState
- type Metrics
- type Option
- func WithLogger(l *slog.Logger) Option
- func WithMaxThreads(maxThreads int) Option
- func WithMaxWaitTime(maxWaitTime time.Duration) Option
- func WithMetrics(m Metrics) Option
- func WithNumThreads(numThreads int) Option
- func WithPhpIni(overrides map[string]string) Option
- func WithWorkers(name string, fileName string, num int, options ...WorkerOption) Option
- type PHPConfig
- type PHPVersion
- type PreparedEnv
- type PrometheusMetrics
- func (m *PrometheusMetrics) DequeuedRequest()
- func (m *PrometheusMetrics) DequeuedWorkerRequest(name string)
- func (m *PrometheusMetrics) QueuedRequest()
- func (m *PrometheusMetrics) QueuedWorkerRequest(name string)
- func (m *PrometheusMetrics) ReadyWorker(name string)
- func (m *PrometheusMetrics) Shutdown()
- func (m *PrometheusMetrics) StartRequest()
- func (m *PrometheusMetrics) StartWorker(name string)
- func (m *PrometheusMetrics) StartWorkerRequest(name string)
- func (m *PrometheusMetrics) StopRequest()
- func (m *PrometheusMetrics) StopWorker(name string, reason StopReason)
- func (m *PrometheusMetrics) StopWorkerRequest(name string, duration time.Duration)
- func (m *PrometheusMetrics) TotalThreads(num int)
- func (m *PrometheusMetrics) TotalWorkers(string, int)
- type RequestOption
- func WithOriginalRequest(r *http.Request) RequestOption
- func WithRequestDocumentRoot(documentRoot string, resolveSymlink bool) RequestOption
- func WithRequestEnv(env map[string]string) RequestOption
- func WithRequestLogger(logger *slog.Logger) RequestOption
- func WithRequestPreparedEnv(env PreparedEnv) RequestOption
- func WithRequestResolvedDocumentRoot(documentRoot string) RequestOption
- func WithRequestSplitPath(splitPath []string) RequestOption
- func WithWorkerName(name string) RequestOption
- type StopReason
- type ThreadDebugState
- type WorkerOption
Examples ¶
Constants ¶
const ( StopReasonCrash = iota StopReasonRestart )
Variables ¶
var ( ErrInvalidRequest = errors.New("not a FrankenPHP request") ErrAlreadyStarted = errors.New("FrankenPHP is already started") ErrInvalidPHPVersion = errors.New("FrankenPHP is only compatible with PHP 8.2+") ErrMainThreadCreation = errors.New("error creating the main thread") ErrRequestContextCreation = errors.New("error during request context creation") ErrScriptExecution = errors.New("error during PHP script execution") ErrNotRunning = errors.New("FrankenPHP is not running. For proper configuration visit: https://frankenphp.dev/docs/config/#caddyfile-config") )
var EmbeddedAppPath string
EmbeddedAppPath contains the path of the embedded PHP application (empty if none)
var (
ErrMaxThreadsReached = errors.New("max amount of overall threads reached")
)
Functions ¶
func DrainWorkers ¶ added in v1.5.0
func DrainWorkers()
EXPERIMENTAL: DrainWorkers finishes all worker scripts before a graceful shutdown
func ExecutePHPCode ¶ added in v1.6.0
func ExecuteScriptCLI ¶
ExecuteScriptCLI executes the PHP script passed as parameter. It returns the exit status code of the script.
Example ¶
package main
import (
"log"
"os"
"github.com/dunglas/frankenphp"
)
func main() {
if len(os.Args) <= 1 {
log.Println("Usage: my-program script.php")
os.Exit(1)
}
os.Exit(frankenphp.ExecuteScriptCLI(os.Args[1], os.Args))
}
func GoPackedArray ¶ added in v1.9.1
EXPERIMENTAL: GoPackedArray converts a zend_array to a Go slice
func NewRequestWithContext ¶
NewRequestWithContext creates a new FrankenPHP request context.
func PHPAssociativeArray ¶ added in v1.9.1
func PHPAssociativeArray(arr AssociativeArray) unsafe.Pointer
EXPERIMENTAL: PHPAssociativeArray converts a Go AssociativeArray to a PHP zend_array.
func PHPMap ¶ added in v1.9.1
EXPERIMENTAL: PHPMap converts an unordered Go map to a PHP zend_array.
func PHPPackedArray ¶ added in v1.9.1
EXPERIMENTAL: PHPPackedArray converts a Go slice to a PHP zend_array.
func PHPString ¶ added in v1.8.0
EXPERIMENTAL: PHPString converts a Go string to a zend_string with copy. The string can be non-persistent (automatically freed after the request by the ZMM) or persistent. If you choose the second mode, it is your repsonsability to free the allocated memory.
func RegisterExtension ¶ added in v1.8.0
RegisterExtension registers a new PHP extension.
func RestartWorkers ¶ added in v1.5.0
func RestartWorkers()
RestartWorkers attempts to restart all workers gracefully
func ServeHTTP ¶
func ServeHTTP(responseWriter http.ResponseWriter, request *http.Request) error
ServeHTTP executes a PHP script according to the given context.
Example ¶
package main
import (
"log"
"net/http"
"github.com/dunglas/frankenphp"
)
func main() {
if err := frankenphp.Init(); err != nil {
panic(err)
}
defer frankenphp.Shutdown()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
req, err := frankenphp.NewRequestWithContext(r, frankenphp.WithRequestDocumentRoot("/path/to/document/root", false))
if err != nil {
panic(err)
}
if err := frankenphp.ServeHTTP(w, req); err != nil {
panic(err)
}
})
log.Fatal(http.ListenAndServe(":8080", nil))
}
Example (Workers) ¶
package main
import (
"log"
"net/http"
"github.com/dunglas/frankenphp"
)
func main() {
if err := frankenphp.Init(
frankenphp.WithWorkers("worker1", "worker1.php", 4,
frankenphp.WithWorkerEnv(map[string]string{"ENV1": "foo"}),
frankenphp.WithWorkerWatchMode([]string{}),
frankenphp.WithWorkerMaxFailures(0),
),
frankenphp.WithWorkers("worker2", "worker2.php", 2,
frankenphp.WithWorkerEnv(map[string]string{"ENV2": "bar"}),
frankenphp.WithWorkerWatchMode([]string{}),
frankenphp.WithWorkerMaxFailures(0),
),
); err != nil {
panic(err)
}
defer frankenphp.Shutdown()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
req, err := frankenphp.NewRequestWithContext(r, frankenphp.WithRequestDocumentRoot("/path/to/document/root", false))
if err != nil {
panic(err)
}
if err := frankenphp.ServeHTTP(w, req); err != nil {
panic(err)
}
})
log.Fatal(http.ListenAndServe(":8080", nil))
}
Types ¶
type AssociativeArray ¶ added in v1.9.1
AssociativeArray represents a PHP array with ordered key-value pairs
func GoAssociativeArray ¶ added in v1.9.1
func GoAssociativeArray(arr unsafe.Pointer) AssociativeArray
EXPERIMENTAL: GoAssociativeArray converts a zend_array to a Go AssociativeArray
type FrankenPHPDebugState ¶ added in v1.5.0
type FrankenPHPDebugState struct {
ThreadDebugStates []ThreadDebugState
ReservedThreadCount int
}
EXPERIMENTAL: FrankenPHPDebugState prints the state of all PHP threads - debugging purposes only
func DebugState ¶ added in v1.5.0
func DebugState() FrankenPHPDebugState
EXPERIMENTAL: DebugState prints the state of all PHP threads - debugging purposes only
type Metrics ¶ added in v1.3.0
type Metrics interface {
// StartWorker collects started workers
StartWorker(name string)
// ReadyWorker collects ready workers
ReadyWorker(name string)
// StopWorker collects stopped workers
StopWorker(name string, reason StopReason)
// TotalWorkers collects expected workers
TotalWorkers(name string, num int)
// TotalThreads collects total threads
TotalThreads(num int)
// StartRequest collects started requests
StartRequest()
// StopRequest collects stopped requests
StopRequest()
// StopWorkerRequest collects stopped worker requests
StopWorkerRequest(name string, duration time.Duration)
// StartWorkerRequest collects started worker requests
StartWorkerRequest(name string)
Shutdown()
QueuedWorkerRequest(name string)
DequeuedWorkerRequest(name string)
QueuedRequest()
DequeuedRequest()
}
type Option ¶
type Option func(h *opt) error
Option instances allow to configure FrankenPHP.
func WithLogger ¶
WithLogger configures the global logger to use.
func WithMaxThreads ¶ added in v1.5.0
func WithMaxWaitTime ¶ added in v1.5.0
WithMaxWaitTime configures the max time a request may be stalled waiting for a thread.
func WithMetrics ¶ added in v1.3.0
func WithNumThreads ¶
WithNumThreads configures the number of PHP threads to start.
func WithPhpIni ¶ added in v1.5.0
WithPhpIni configures user defined PHP ini settings.
func WithWorkers ¶
func WithWorkers(name string, fileName string, num int, options ...WorkerOption) Option
WithWorkers configures the PHP workers to start
type PHPConfig ¶
type PHPConfig struct {
Version PHPVersion
ZTS bool
ZendSignals bool
ZendMaxExecutionTimers bool
}
type PHPVersion ¶
type PreparedEnv ¶ added in v1.1.1
func PrepareEnv ¶ added in v1.1.1
func PrepareEnv(env map[string]string) PreparedEnv
type PrometheusMetrics ¶ added in v1.3.0
type PrometheusMetrics struct {
// contains filtered or unexported fields
}
func NewPrometheusMetrics ¶ added in v1.3.0
func NewPrometheusMetrics(registry prometheus.Registerer) *PrometheusMetrics
func (*PrometheusMetrics) DequeuedRequest ¶ added in v1.5.0
func (m *PrometheusMetrics) DequeuedRequest()
func (*PrometheusMetrics) DequeuedWorkerRequest ¶ added in v1.5.0
func (m *PrometheusMetrics) DequeuedWorkerRequest(name string)
func (*PrometheusMetrics) QueuedRequest ¶ added in v1.5.0
func (m *PrometheusMetrics) QueuedRequest()
func (*PrometheusMetrics) QueuedWorkerRequest ¶ added in v1.5.0
func (m *PrometheusMetrics) QueuedWorkerRequest(name string)
func (*PrometheusMetrics) ReadyWorker ¶ added in v1.3.0
func (m *PrometheusMetrics) ReadyWorker(name string)
func (*PrometheusMetrics) Shutdown ¶ added in v1.3.0
func (m *PrometheusMetrics) Shutdown()
func (*PrometheusMetrics) StartRequest ¶ added in v1.3.0
func (m *PrometheusMetrics) StartRequest()
func (*PrometheusMetrics) StartWorker ¶ added in v1.3.0
func (m *PrometheusMetrics) StartWorker(name string)
func (*PrometheusMetrics) StartWorkerRequest ¶ added in v1.3.0
func (m *PrometheusMetrics) StartWorkerRequest(name string)
func (*PrometheusMetrics) StopRequest ¶ added in v1.3.0
func (m *PrometheusMetrics) StopRequest()
func (*PrometheusMetrics) StopWorker ¶ added in v1.3.0
func (m *PrometheusMetrics) StopWorker(name string, reason StopReason)
func (*PrometheusMetrics) StopWorkerRequest ¶ added in v1.3.0
func (m *PrometheusMetrics) StopWorkerRequest(name string, duration time.Duration)
func (*PrometheusMetrics) TotalThreads ¶ added in v1.3.0
func (m *PrometheusMetrics) TotalThreads(num int)
func (*PrometheusMetrics) TotalWorkers ¶ added in v1.3.0
func (m *PrometheusMetrics) TotalWorkers(string, int)
type RequestOption ¶
type RequestOption func(h *frankenPHPContext) error
RequestOption instances allow to configure a FrankenPHP Request.
func WithOriginalRequest ¶ added in v1.4.0
func WithOriginalRequest(r *http.Request) RequestOption
func WithRequestDocumentRoot ¶
func WithRequestDocumentRoot(documentRoot string, resolveSymlink bool) RequestOption
WithRequestDocumentRoot sets the root directory of the PHP application. if resolveSymlink is true, oath declared as root directory will be resolved to its absolute value after the evaluation of any symbolic links. Due to the nature of PHP opcache, root directory path is cached: when using a symlinked directory as root this could generate errors when symlink is changed without PHP being restarted; enabling this directive will set $_SERVER['DOCUMENT_ROOT'] to the real directory path.
func WithRequestEnv ¶
func WithRequestEnv(env map[string]string) RequestOption
WithRequestEnv set CGI-like environment variables that will be available in $_SERVER. Values set with WithEnv always have priority over automatically populated values.
func WithRequestLogger ¶
func WithRequestLogger(logger *slog.Logger) RequestOption
WithRequestLogger sets the logger associated with the current request
func WithRequestPreparedEnv ¶ added in v1.1.1
func WithRequestPreparedEnv(env PreparedEnv) RequestOption
func WithRequestResolvedDocumentRoot ¶ added in v1.2.5
func WithRequestResolvedDocumentRoot(documentRoot string) RequestOption
WithRequestResolvedDocumentRoot is similar to WithRequestDocumentRoot but doesn't do any checks or resolving on the path to improve performance.
func WithRequestSplitPath ¶
func WithRequestSplitPath(splitPath []string) RequestOption
WithRequestSplitPath contains a list of split path strings.
The path in the URL will be split into two, with the first piece ending with the value of splitPath. The first piece will be assumed as the actual resource (CGI script) name, and the second piece will be set to PATH_INFO for the CGI script to use.
Future enhancements should be careful to avoid CVE-2019-11043, which can be mitigated with use of a try_files-like behavior that 404s if the FastCGI path info is not found.
func WithWorkerName ¶ added in v1.6.0
func WithWorkerName(name string) RequestOption
WithWorkerName sets the worker that should handle the request
type StopReason ¶ added in v1.3.0
type StopReason int
type ThreadDebugState ¶ added in v1.5.0
type ThreadDebugState struct {
Index int
Name string
State string
IsWaiting bool
IsBusy bool
WaitingSinceMilliseconds int64
}
EXPERIMENTAL: ThreadDebugState prints the state of a single PHP thread - debugging purposes only
type WorkerOption ¶ added in v1.8.0
type WorkerOption func(*workerOpt) error
WorkerOption instances allow configuring FrankenPHP worker.
func WithWorkerEnv ¶ added in v1.8.0
func WithWorkerEnv(env map[string]string) WorkerOption
WithWorkerEnv sets environment variables for the worker
func WithWorkerMaxFailures ¶ added in v1.8.0
func WithWorkerMaxFailures(maxFailures int) WorkerOption
WithWorkerMaxFailures sets the maximum number of consecutive failures before panicking
func WithWorkerWatchMode ¶ added in v1.8.0
func WithWorkerWatchMode(watch []string) WorkerOption
WithWorkerWatchMode sets directories to watch for file changes

