Golang v2 environment -- runtime and builder (#427)
v2 environment for Go; includes runtime and builder.
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"plugin"
|
||||
|
||||
"github.com/fission/fission/environments/go/context"
|
||||
@@ -13,14 +16,52 @@ const (
|
||||
CODE_PATH = "/userfunc/user"
|
||||
)
|
||||
|
||||
type (
|
||||
FunctionLoadRequest struct {
|
||||
// FilePath is an absolute filesystem path to the
|
||||
// function. What exactly is stored here is
|
||||
// env-specific. Optional.
|
||||
FilePath string `json:"filepath"`
|
||||
|
||||
// FunctionName has an environment-specific meaning;
|
||||
// usually, it defines a function within a module
|
||||
// containing multiple functions. Optional; default is
|
||||
// environment-specific.
|
||||
FunctionName string `json:"functionName"`
|
||||
|
||||
// URL to expose this function at. Optional; defaults
|
||||
// to "/".
|
||||
URL string `json:"url"`
|
||||
}
|
||||
)
|
||||
|
||||
var userFunc http.HandlerFunc
|
||||
|
||||
func loadPlugin() http.HandlerFunc {
|
||||
p, err := plugin.Open(CODE_PATH)
|
||||
func loadPlugin(codePath, entrypoint string) http.HandlerFunc {
|
||||
|
||||
// if codepath's a directory, load the file inside it
|
||||
info, err := os.Stat(codePath)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
sym, err := p.Lookup("Handler")
|
||||
if info.IsDir() {
|
||||
files, err := ioutil.ReadDir(codePath)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if len(files) == 0 {
|
||||
panic("No files to load")
|
||||
}
|
||||
fi := files[0]
|
||||
codePath = filepath.Join(codePath, fi.Name())
|
||||
}
|
||||
|
||||
fmt.Printf("loading plugin from %v\n", codePath)
|
||||
p, err := plugin.Open(codePath)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
sym, err := p.Lookup(entrypoint)
|
||||
if err != nil {
|
||||
panic("Entry point not found")
|
||||
}
|
||||
@@ -44,7 +85,7 @@ func loadPlugin() http.HandlerFunc {
|
||||
|
||||
func specializeHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if userFunc != nil {
|
||||
w.WriteHeader(400)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte("Not a generic container"))
|
||||
return
|
||||
}
|
||||
@@ -61,12 +102,48 @@ func specializeHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
fmt.Println("Specializing ...")
|
||||
userFunc = loadPlugin()
|
||||
userFunc = loadPlugin(CODE_PATH, "Handler")
|
||||
fmt.Println("Done")
|
||||
}
|
||||
|
||||
func specializeHandlerV2(w http.ResponseWriter, r *http.Request) {
|
||||
if userFunc != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte("Not a generic container"))
|
||||
return
|
||||
}
|
||||
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
var loadreq FunctionLoadRequest
|
||||
err = json.Unmarshal(body, &loadreq)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = os.Stat(loadreq.FilePath)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
w.Write([]byte(CODE_PATH + ": not found"))
|
||||
return
|
||||
} else {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("Specializing ...")
|
||||
userFunc = loadPlugin(loadreq.FilePath, loadreq.FunctionName)
|
||||
fmt.Println("Done")
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/specialize", specializeHandler)
|
||||
http.HandleFunc("/v2/specialize", specializeHandlerV2)
|
||||
|
||||
// Generic route -- all http requests go to the user function.
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
Reference in New Issue
Block a user