Skip to content

Instantly share code, notes, and snippets.

@patrickjahns
Created March 10, 2019 13:53
Show Gist options
  • Save patrickjahns/f2d07abcf8b368f0053d42658049c4da to your computer and use it in GitHub Desktop.
Save patrickjahns/f2d07abcf8b368f0053d42658049c4da to your computer and use it in GitHub Desktop.
nulcio-php-handler
ARG NUCLIO_LABEL=0.7.1
ARG NUCLIO_ARCH=amd64
ARG NUCLIO_BASE_IMAGE=alpine:3.7
ARG NUCLIO_ONBUILD_IMAGE=nuclio/handler-builder-golang-onbuild:${NUCLIO_LABEL}-${NUCLIO_ARCH}-alpine
# Supplies processor uhttpc, used for healthcheck
FROM nuclio/uhttpc:0.0.1-amd64 as uhttpc
# Builds source, supplies processor binary and handler plugin
FROM ${NUCLIO_ONBUILD_IMAGE} as builder
# From the base image
FROM ${NUCLIO_BASE_IMAGE}
# Copy required objects from the suppliers
COPY --from=builder /home/nuclio/bin/processor /usr/local/bin/processor
COPY --from=builder /home/nuclio/bin/handler.so /opt/nuclio/handler.so
COPY --from=uhttpc /home/nuclio/bin/uhttpc /usr/local/bin/uhttpc
# Readiness probe
HEALTHCHECK --interval=1s --timeout=3s CMD /usr/local/bin/uhttpc --url http://127.0.0.1:8082/ready || exit 1
# further run
RUN apk update && apk add php7-fpm && mkdir -p /var/task/src
#
ADD test.php /var/task/src
# ensure permissions are valid
RUN chown -R nobody:nobody /var/task
# Run processor with configuration and platform configuration
CMD [ "processor" ]
/**
* @author Patrick Jahns <github@patrickjahns.de>
*
* @copyright Copyright (c) 2019, Patrick Jahns.
* @license GPL-2.0
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
package main
import (
"bytes"
"github.com/nuclio/nuclio-sdk-go"
"github.com/tomasen/fcgi_client"
"github.com/yookoala/gofast/tools/phpfpm"
"io/ioutil"
"strings"
"time"
)
func Handler(context *nuclio.Context, event nuclio.Event) (interface{}, error) {
context.Logger.Info("invocation started")
fpmSocket := "/var/task/fpm.sock"
fpmConfFile := "/var/task/php-fpm.conf"
// start fpm process
// TODO: better fpm helper that checks socket for availability
context.Logger.Info("starting fpm")
fpm := phpfpm.NewProcess("/usr/sbin/php-fpm7")
fpm.User = "nobody"
fpm.Worker = 1
fpm.ConfigFile = fpmConfFile
fpm.SetDatadir("/var/task")
fpm.Listen = fpmSocket
fpm.SaveConfig(fpmConfFile)
fpm.Start()
defer fpm.Stop()
time.Sleep(1 * time.Second)
context.Logger.Info("connecting to fcgi")
fcgi, err := fcgiclient.Dial("unix", fpmSocket)
if err != nil {
context.Logger.Error("err:", err)
}
scriptFilename := "/var/task/src/test.php"
env := map[string]string{
"CONTENT_LENGTH": event.GetHeaderString(string("Content-Length")),
"CONTENT_TYPE": event.GetContentType(),
"REQUEST_METHOD": event.GetMethod(),
"REQUEST_PATH": event.GetPath(),
"REQUEST_URI": event.GetURL(),
"SCRIPT_FILENAME": scriptFilename,
"SERVER_SOFTWARE": "nuclio-go / fcgiclient ",
"QUERY_STRING": "",
}
// fields == query fields
// needs parsing to be put into the query string
//for field, _ := range event.GetFields() {
// context.Logger.Info(field)
//}
for header, _ := range event.GetHeaders() {
env["HTTP_"+strings.Replace(strings.ToUpper(header), "-", "_", -1)] = event.GetHeaderString(header)
}
reqBody := bytes.NewReader(event.GetBody())
context.Logger.Info("sending request to fpm", env)
resp, err := fcgi.Request(env, reqBody)
if err != nil {
context.Logger.Error("err:", err)
}
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
context.Logger.Error("err:", err)
}
return nuclio.Response{
StatusCode: 200,
ContentType: "application/text",
Body: []byte(content),
}, nil
}
<?php
/**
* @author Patrick Jahns <github@patrickjahns.de>
*
* @copyright Copyright (c) 2019, Patrick Jahns.
* @license GPL-2.0
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
phpinfo();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment