Skip to content
Snippets Groups Projects
Commit 22b3e73b authored by Alex's avatar Alex
Browse files

Merge branch 'master' of https://github.com/alexellis/faas

parents fdd0ef4b c2eb41ee
No related branches found
No related tags found
No related merge requests found
...@@ -37,11 +37,14 @@ This binary fwatchdog acts as a watchdog for your function. Features: ...@@ -37,11 +37,14 @@ This binary fwatchdog acts as a watchdog for your function. Features:
## 2. Future items ## 2. Future items
* Asynchronous / long-running tasks * Asynchronous / long-running tasks
* Built-in TLS termination or guide for termination through NGinx etc * Function store - list of useful predefined functions
* Deeper tests coverage and integration tests * Guide for termination through NGinx or built-in TLS termination
* Guide for basic authentication over HTTPs (set up externally through NGinx etc)
* Documentation about Alexa sample function * Documentation about Alexa sample function
* Supporting request parameters * Supporting request parameters
* Deeper tests coverage and integration tests
## 3. Development and Contributing ## 3. Development and Contributing
If you would like to consume the project with your own functions then you can use the public images and the supplied `docker stack` file as a template (docker-compose.yml) If you would like to consume the project with your own functions then you can use the public images and the supplied `docker stack` file as a template (docker-compose.yml)
......
...@@ -8,6 +8,7 @@ import ( ...@@ -8,6 +8,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"strings" "strings"
"sync"
"time" "time"
) )
...@@ -20,42 +21,61 @@ func (OsEnv) Getenv(key string) string { ...@@ -20,42 +21,61 @@ func (OsEnv) Getenv(key string) string {
return os.Getenv(key) return os.Getenv(key)
} }
func pipeRequest(config *WatchdogConfig, w http.ResponseWriter, r *http.Request) {
parts := strings.Split(config.faasProcess, " ")
targetCmd := exec.Command(parts[0], parts[1:]...)
writer, _ := targetCmd.StdinPipe()
var out []byte
var err error
var res []byte
var wg sync.WaitGroup
wg.Add(2)
res, _ = ioutil.ReadAll(r.Body)
defer r.Body.Close()
go func() {
defer wg.Done()
writer.Write(res)
writer.Close()
}()
go func() {
defer wg.Done()
out, err = targetCmd.CombinedOutput()
}()
wg.Wait()
if err != nil {
if config.writeDebug == true {
log.Println(targetCmd, err)
}
w.WriteHeader(500)
response := bytes.NewBufferString(err.Error())
w.Write(response.Bytes())
return
}
if config.writeDebug == true {
os.Stdout.Write(out)
}
// Match header for strict services
if r.Header.Get("Content-Type") == "application/json" {
w.Header().Set("Content-Type", "application/json")
}
w.WriteHeader(200)
w.Write(out)
}
func makeRequestHandler(config *WatchdogConfig) func(http.ResponseWriter, *http.Request) { func makeRequestHandler(config *WatchdogConfig) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" { if r.Method == "POST" {
parts := strings.Split(config.faasProcess, " ") pipeRequest(config, w, r)
targetCmd := exec.Command(parts[0], parts[1:]...)
writer, _ := targetCmd.StdinPipe()
res, _ := ioutil.ReadAll(r.Body)
defer r.Body.Close()
writer.Write(res)
writer.Close()
out, err := targetCmd.CombinedOutput()
if err != nil {
if config.writeDebug == true {
log.Println(targetCmd, err)
}
w.WriteHeader(500)
response := bytes.NewBufferString(err.Error())
w.Write(response.Bytes())
return
}
if config.writeDebug == true {
os.Stdout.Write(out)
}
// Match header for strict services
if r.Header.Get("Content-Type") == "application/json" {
w.Header().Set("Content-Type", "application/json")
}
w.WriteHeader(200)
w.Write(out)
} else { } else {
w.WriteHeader(http.StatusMethodNotAllowed) w.WriteHeader(http.StatusMethodNotAllowed)
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment