Compare commits
105 Commits
0.7.4-alph
...
0.9.31-alp
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7df1c64740 | ||
|
|
91325f9ca3 | ||
|
|
7326325c19 | ||
|
|
59e195fa94 | ||
|
|
5ed4f8795a | ||
|
|
a02e7e66af | ||
|
|
9c1d093bb8 | ||
|
|
302877d4b4 | ||
|
|
871bb29dbe | ||
|
|
3be144f644 | ||
|
|
2560dc23fc | ||
|
|
3d6c3d10bf | ||
|
|
5f811693f1 | ||
|
|
0068fb92eb | ||
|
|
71174ead45 | ||
|
|
43c18caceb | ||
|
|
7b4c9c3154 | ||
|
|
9d2649433d | ||
|
|
6353fa7dd3 | ||
|
|
bfa837c88d | ||
|
|
bdc454e7e5 | ||
|
|
9b3e85754c | ||
|
|
af3dcc5f31 | ||
|
|
c375fb9eaf | ||
|
|
70c314229f | ||
|
|
66e23ead00 | ||
|
|
2e5666c2b6 | ||
|
|
7675656a54 | ||
|
|
3d7f7b0ad1 | ||
|
|
a1ccbd6cab | ||
|
|
33cb4ce63c | ||
|
|
aefb4497e2 | ||
|
|
0047e66f10 | ||
|
|
6bae4254af | ||
|
|
a9689993b0 | ||
|
|
8c0182b29f | ||
|
|
02d55c7143 | ||
|
|
f343b537f1 | ||
|
|
a84e7da65f | ||
|
|
f3b64387cb | ||
|
|
e132435ff8 | ||
|
|
fb492fa6f7 | ||
|
|
159714491d | ||
|
|
64cbbc70bb | ||
|
|
d0559f627e | ||
|
|
0a6784e270 | ||
|
|
b6fd3c7e98 | ||
|
|
1c05534071 | ||
|
|
3627d5bb40 | ||
|
|
1f7714c1e9 | ||
|
|
d868ebf4a1 | ||
|
|
4640379b06 | ||
|
|
922120efbb | ||
|
|
91fec99b00 | ||
|
|
2f89c1fe1f | ||
|
|
2298f39cca | ||
|
|
23d68bc27b | ||
|
|
74c0423f0d | ||
|
|
06f87c4d8e | ||
|
|
35262de828 | ||
|
|
34a495984c | ||
|
|
d7130c4e28 | ||
|
|
c9630a53c3 | ||
|
|
0522690472 | ||
|
|
a8a0fbed32 | ||
|
|
26ae9585f6 | ||
|
|
b69bd699c8 | ||
|
|
650ee5f63a | ||
|
|
e3c60cbb77 | ||
|
|
0daca43d10 | ||
|
|
d3c239dc54 | ||
|
|
05ac2441da | ||
|
|
c0009b1b64 | ||
|
|
82960824ef | ||
|
|
64b63cbd0f | ||
|
|
05771fb07f | ||
|
|
d1f680dacd | ||
|
|
14c9397b70 | ||
|
|
eb5e724899 | ||
|
|
80619bd800 | ||
|
|
8e2cdfc607 | ||
|
|
58f416b7b2 | ||
|
|
b6cf39e3e5 | ||
|
|
41bc98ab64 | ||
|
|
b007ac315a | ||
|
|
940f6b8f72 | ||
|
|
f9690b74a5 | ||
|
|
f2c58d545a | ||
|
|
4732426629 | ||
|
|
d4af4f67b2 | ||
|
|
6420e8b6c6 | ||
|
|
15c59fa31f | ||
|
|
294131b48f | ||
|
|
48413abaa1 | ||
|
|
d36b2b935b | ||
|
|
f493749689 | ||
|
|
9de10bc885 | ||
|
|
2d5446686a | ||
|
|
0d7d4f4a6a | ||
|
|
23c4171ece | ||
|
|
d8a1868fce | ||
|
|
d91a9959a8 | ||
|
|
87e7c7d6ae | ||
|
|
89c94daebc | ||
|
|
047fac2a0a |
@@ -1,73 +0,0 @@
|
||||
defaults: &defaults
|
||||
machine: true
|
||||
environment:
|
||||
IMPORT_PATH: "github.com/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME"
|
||||
OUTPUT_DIR: "./build"
|
||||
DIST_DIR: "./dist"
|
||||
|
||||
install_golang: &install_golang
|
||||
run:
|
||||
name: install Golang 1.11
|
||||
command: |
|
||||
sudo add-apt-repository ppa:gophers/archive
|
||||
sudo apt-get update
|
||||
sudo apt-get install golang-1.11-go
|
||||
alias go="/usr/lib/go-1.11/bin/go"
|
||||
go version
|
||||
|
||||
install_deps: &install_deps
|
||||
run:
|
||||
name: Install deps
|
||||
command: |
|
||||
/usr/lib/go-1.11/bin/go mod vendor
|
||||
/usr/lib/go-1.11/bin/go get -u github.com/gobuffalo/packr/packr
|
||||
|
||||
install_httpie: &install_httpie
|
||||
run:
|
||||
name: install httpie
|
||||
command: |
|
||||
sudo apt-get -y update && sudo apt-get -y install httpie
|
||||
|
||||
install_jq: &install_jq
|
||||
run:
|
||||
name: install jq
|
||||
command: |
|
||||
sudo apt-get update && sudo apt-get -y install jq
|
||||
|
||||
build_binary: &build_binary
|
||||
run:
|
||||
name: build binary
|
||||
command: |
|
||||
/usr/lib/go-1.11/bin/go build -o ${OUTPUT_DIR}/fx fx.go
|
||||
|
||||
unit_test: &unit_test
|
||||
run:
|
||||
name: unit test
|
||||
command: |
|
||||
make unit-test
|
||||
bash <(curl -s https://codecov.io/bash) -t ${CODECOV_TOKEN}
|
||||
cli_test: &cli_test
|
||||
run:
|
||||
name: cli test
|
||||
command: make cli-test
|
||||
|
||||
version: 2
|
||||
jobs:
|
||||
test:
|
||||
<<: *defaults
|
||||
steps:
|
||||
- checkout
|
||||
- *install_golang
|
||||
- *install_deps
|
||||
- *unit_test
|
||||
- *build_binary
|
||||
- run:
|
||||
name: Pull images
|
||||
command: make pull
|
||||
- *cli_test
|
||||
|
||||
workflows:
|
||||
version: 2
|
||||
workflow:
|
||||
jobs:
|
||||
- test
|
||||
65
.github/workflows/ci.yml
vendored
65
.github/workflows/ci.yml
vendored
@@ -13,61 +13,66 @@ jobs:
|
||||
- name: check out
|
||||
uses: actions/checkout@master
|
||||
|
||||
- name: setup docker
|
||||
- name: kind create a k8s cluster
|
||||
run: |
|
||||
./scripts/provision.sh
|
||||
kind create cluster
|
||||
|
||||
- name: setup k8s and kind
|
||||
- name: lint
|
||||
run: |
|
||||
export GOBIN=$(go env GOPATH)/bin
|
||||
export PATH=$PATH:$GOBIN
|
||||
mkdir -p $GOBIN
|
||||
curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl
|
||||
chmod +x kubectl && mv kubectl $GOBIN
|
||||
wget https://github.com/kubernetes-sigs/kind/releases/download/v0.5.0/kind-linux-amd64 && chmod +x kind-linux-amd64 && mv kind-linux-amd64 $GOBIN/kind
|
||||
./scripts/setup_kind.sh
|
||||
docker run --rm -v $(pwd):/app -w /app golangci/golangci-lint \
|
||||
golangci-lint run -v
|
||||
|
||||
- name: unit test
|
||||
env:
|
||||
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
|
||||
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
|
||||
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
|
||||
run: |
|
||||
export KUBECONFIG=/home/runner/.kube/kind-config-fx-test
|
||||
DEBUG=true go test -v ./container_runtimes/... ./deploy/...
|
||||
export KUBECONFIG="$(kind get kubeconfig-path)"
|
||||
make unit-test
|
||||
bash <(curl -s https://codecov.io/bash) -t ${CODECOV_TOKEN}
|
||||
|
||||
- name: build fx
|
||||
run: |
|
||||
make build
|
||||
|
||||
- name: lint
|
||||
- name: test fx-docker
|
||||
run: |
|
||||
export GOBIN=$(go env GOPATH)/bin
|
||||
export PATH=$PATH:$GOBIN
|
||||
go get -u github.com/golangci/golangci-lint/cmd/golangci-lint
|
||||
golangci-lint run
|
||||
|
||||
cd ./contrib/docker_packer
|
||||
make linux-build
|
||||
make docker-build
|
||||
make test
|
||||
# make docker-publish #TODO in release workflow
|
||||
- name: test fx docker cloud
|
||||
run: |
|
||||
make start_docker_infra
|
||||
make test_docker_infra
|
||||
make stop_docker_infra
|
||||
|
||||
- name: test fx cli
|
||||
env:
|
||||
REMOTE_HOST_ADDR: ${{secrets.DOCKER_REMOTE_HOST_ADDR}}
|
||||
REMOTE_HOST_USER: ${{secrets.DOCKER_REMOTE_HOST_USER}}
|
||||
REMOTE_HOST_PASSWORD: ${{secrets.DOCKER_REMOTE_HOST_PASSWORD}}
|
||||
run: |
|
||||
echo $KUBECONFIG
|
||||
unset KUBECONFIG
|
||||
make cli-test
|
||||
make cli-test-ci
|
||||
|
||||
- name: test AKS
|
||||
env:
|
||||
AKS_KUBECONFIG: ${{ secrets.AKS_KUBECONFIG }}
|
||||
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
|
||||
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
|
||||
run: |
|
||||
export KUBECONFIG=${HOME}/.kube/aks
|
||||
echo ${AKS_KUBECONFIG} | base64 -d > $KUBECONFIG
|
||||
if [[ -z "$DOCKER_USERNAME" || -z "$DOCKER_PASSWORD" || -z "$AKS_KUBECONFIG" ]];then
|
||||
echo "skip deploy test since no DOCKER_USERNAME and DOCKER_PASSWORD set"
|
||||
else
|
||||
DEBUG=true ./build/fx deploy -n hello -p 12345 examples/functions/JavaScript/func.js
|
||||
./build/fx destroy hello
|
||||
rm ${KUBECONFIG}
|
||||
fi
|
||||
echo "skip since aks environment not ready yet"
|
||||
# export KUBECONFIG=${HOME}/.kube/aks
|
||||
# echo ${AKS_KUBECONFIG} | base64 -d > $KUBECONFIG
|
||||
# if [[ -z "$AKS_KUBECONFIG" ]];then
|
||||
# echo "skip deploy test since no valid KUBECONFIG"
|
||||
# else
|
||||
# DEBUG=true ./build/fx up -n hello -p 12345 examples/functions/JavaScript/func.js
|
||||
# ./build/fx down hello
|
||||
# rm ${KUBECONFIG}
|
||||
# fi
|
||||
|
||||
Installation:
|
||||
runs-on: ${{ matrix.os }}
|
||||
|
||||
93
.github/workflows/docker.yml
vendored
93
.github/workflows/docker.yml
vendored
@@ -1,57 +1,70 @@
|
||||
on:
|
||||
schedule:
|
||||
- cron: '0 12 * * *'
|
||||
on: [push]
|
||||
# schedule:
|
||||
# - cron: '0 12 * * *'
|
||||
name: docker
|
||||
jobs:
|
||||
Docker:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
docker_version:
|
||||
- 18.09
|
||||
# - 19.03
|
||||
# - 19.09
|
||||
docker_channel:
|
||||
- stable
|
||||
# - test
|
||||
steps:
|
||||
- uses: actions/checkout@master
|
||||
- name: setup docker
|
||||
uses: docker-practice/actions-setup-docker@master
|
||||
with:
|
||||
docker_version: ${{ matrix.docker_version }}
|
||||
docker_channel: ${{ matrix.docker_channel }}
|
||||
|
||||
- name: login
|
||||
uses: actions/docker/login@8cdf801b322af5f369e00d85e9cf3a7122f49108
|
||||
- name: login docker hub
|
||||
env:
|
||||
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
|
||||
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
|
||||
run: |
|
||||
docker login --username $DOCKER_USERNAME --password $DOCKER_PASSWORD
|
||||
|
||||
- name: build-fx-go-image
|
||||
uses: actions/docker/cli@master
|
||||
with:
|
||||
args: build -t metrue/fx-go-base:latest -f api/asserts/dockerfiles/base/go/Dockerfile
|
||||
api/asserts/dockerfiles/base/go
|
||||
- name: build and publish fx d image
|
||||
if: always()
|
||||
run: |
|
||||
docker build -t metrue/fx-d-base:latest -f ./assets/dockerfiles/base/d/Dockerfile ./assets/dockerfiles/base/d
|
||||
docker push metrue/fx-d-base:latest
|
||||
|
||||
- name: push-fx-go-image
|
||||
uses: actions/docker/cli@master
|
||||
- name: build and publish fx go image
|
||||
run: |
|
||||
docker build -t metrue/fx-go-base:latest -f ./assets/dockerfiles/base/go/Dockerfile ./assets/dockerfiles/base/go
|
||||
docker push metrue/fx-go-base:latest
|
||||
|
||||
- name: build and publish fx node image
|
||||
if: always()
|
||||
run: |
|
||||
docker build -t metrue/fx-node-base:latest -f ./assets/dockerfiles/base/node/Dockerfile ./assets/dockerfiles/base/node
|
||||
docker push metrue/fx-node-base:latest
|
||||
|
||||
- name: build and publish fx python image
|
||||
if: always()
|
||||
run: |
|
||||
docker build -t metrue/fx-python-base:latest -f ./assets/dockerfiles/base/python/Dockerfile ./assets/dockerfiles/base/python
|
||||
- name: publish fx python image
|
||||
env:
|
||||
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
|
||||
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
|
||||
with:
|
||||
args: push metrue/fx-go-base:latest
|
||||
run: |
|
||||
docker push metrue/fx-python-base:latest
|
||||
|
||||
- name: build-fx-rust-image
|
||||
uses: actions/docker/cli@master
|
||||
with:
|
||||
args: build -t metrue/fx-rust-base:latest -f api/asserts/dockerfiles/base/rust/Dockerfile
|
||||
api/asserts/dockerfiles/base/rust
|
||||
- name: build and publish fx perl image
|
||||
if: always()
|
||||
run: |
|
||||
docker build -t metrue/fx-perl-base:latest -f ./assets/dockerfiles/base/perl/Dockerfile ./assets/dockerfiles/base/perl
|
||||
docker push metrue/fx-perl-base:latest
|
||||
|
||||
- name: push-fx-rust-image
|
||||
uses: actions/docker/cli@master
|
||||
env:
|
||||
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
|
||||
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
|
||||
with:
|
||||
args: push metrue/fx-rust-base:latest
|
||||
|
||||
- name: build-fx-node-image
|
||||
uses: actions/docker/cli@master
|
||||
with:
|
||||
args: build -t metrue/fx-node-base:latest -f api/asserts/dockerfiles/base/node/Dockerfile
|
||||
api/asserts/dockerfiles/base/node
|
||||
|
||||
- name: push-fx-node-image
|
||||
uses: actions/docker/cli@master
|
||||
env:
|
||||
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
|
||||
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
|
||||
with:
|
||||
args: push metrue/fx-node-base:latest
|
||||
- name: build and publish fx julia image
|
||||
if: always()
|
||||
run: |
|
||||
docker build -t metrue/fx-julia-base:latest -f ./assets/dockerfiles/base/julia/Dockerfile ./assets/dockerfiles/base/julia
|
||||
docker push metrue/fx-julia-base:latest
|
||||
|
||||
38
.github/workflows/release.yml
vendored
38
.github/workflows/release.yml
vendored
@@ -18,40 +18,27 @@ jobs:
|
||||
- name: check out
|
||||
uses: actions/checkout@master
|
||||
|
||||
- name: setup docker
|
||||
- name: kind create a k8s cluster
|
||||
run: |
|
||||
./scripts/provision.sh
|
||||
kind create cluster
|
||||
|
||||
- name: setup k8s and kind
|
||||
- name: lint
|
||||
run: |
|
||||
export GOBIN=$(go env GOPATH)/bin
|
||||
export PATH=$PATH:$GOBIN
|
||||
mkdir -p $GOBIN
|
||||
curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl
|
||||
chmod +x kubectl && mv kubectl $GOBIN
|
||||
wget https://github.com/kubernetes-sigs/kind/releases/download/v0.5.0/kind-linux-amd64 && chmod +x kind-linux-amd64 && mv kind-linux-amd64 $GOBIN/kind
|
||||
./scripts/setup_kind.sh
|
||||
docker run --rm -v $(pwd):/app -w /app golangci/golangci-lint \
|
||||
golangci-lint run -v
|
||||
|
||||
- name: unit test
|
||||
env:
|
||||
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
|
||||
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
|
||||
run: |
|
||||
export KUBECONFIG=/home/runner/.kube/kind-config-fx-test
|
||||
DEBUG=true go test -v ./container_runtimes/... ./deploy/...
|
||||
export KUBECONFIG="$(kind get kubeconfig-path)"
|
||||
make unit-test
|
||||
|
||||
- name: build fx
|
||||
run: |
|
||||
make build
|
||||
|
||||
- name: lint
|
||||
run: |
|
||||
export GOBIN=$(go env GOPATH)/bin
|
||||
export PATH=$PATH:$GOBIN
|
||||
go get -u github.com/golangci/golangci-lint/cmd/golangci-lint
|
||||
golangci-lint run
|
||||
|
||||
|
||||
- name: test fx cli
|
||||
run: |
|
||||
echo $KUBECONFIG
|
||||
@@ -64,11 +51,12 @@ jobs:
|
||||
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
|
||||
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
|
||||
run: |
|
||||
export KUBECONFIG=${HOME}/.kube/aks
|
||||
echo ${AKS_KUBECONFIG} | base64 -d > $KUBECONFIG
|
||||
DEBUG=true ./build/fx deploy -n hello -p 12345 examples/functions/JavaScript/func.js
|
||||
./build/fx destroy hello
|
||||
rm ${KUBECONFIG}
|
||||
echo "skip since aks environment not ready yet"
|
||||
# export KUBECONFIG=${HOME}/.kube/aks
|
||||
# echo ${AKS_KUBECONFIG} | base64 -d > $KUBECONFIG
|
||||
# DEBUG=true ./build/fx up -n hello -p 12345 examples/functions/JavaScript/func.js
|
||||
# ./build/fx down hello
|
||||
# rm ${KUBECONFIG}
|
||||
Release:
|
||||
runs-on: ${{ matrix.os }}
|
||||
needs: [Test]
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
run:
|
||||
deadline: 10m
|
||||
timeout: 10m
|
||||
deadline: 20m
|
||||
timeout: 20m
|
||||
issues-exit-code: 1
|
||||
tests: true
|
||||
skip-dirs:
|
||||
|
||||
@@ -32,3 +32,5 @@ brews:
|
||||
caveats: ""
|
||||
homepage: "https://github.com/metrue/fx"
|
||||
description: "fx, a simple but powerful Function as a Service build tools"
|
||||
dependencies:
|
||||
- docker
|
||||
|
||||
42
Makefile
42
Makefile
@@ -1,5 +1,7 @@
|
||||
OUTPUT_DIR=./build
|
||||
DIST_DIR=./dist
|
||||
OUTPUT_DIR ?=./build
|
||||
DIST_DIR ?=./dist
|
||||
DOCKER_REMOTE_HOST_ADDR ?= "127.0.0.1"
|
||||
DOCKER_REMOTE_HOST_USER ?= $(whoami)
|
||||
|
||||
lint:
|
||||
golangci-lint run
|
||||
@@ -7,8 +9,11 @@ lint:
|
||||
generate:
|
||||
packr
|
||||
|
||||
b:
|
||||
go build -ldflags="-s -w" -o ${OUTPUT_DIR}/fx fx.go
|
||||
|
||||
build:
|
||||
go build -o ${OUTPUT_DIR}/fx fx.go
|
||||
go build -ldflags="-s -w" -o ${OUTPUT_DIR}/fx fx.go
|
||||
|
||||
pull:
|
||||
./scripts/pull.sh
|
||||
@@ -21,10 +26,13 @@ clean:
|
||||
rm -rf ${DIST_DIR}
|
||||
|
||||
unit-test:
|
||||
./scripts/coverage.sh
|
||||
CI=true ./scripts/coverage.sh
|
||||
|
||||
cli-test-ci:
|
||||
./scripts/test_cli.sh 'js'
|
||||
|
||||
cli-test:
|
||||
./scripts/test_cli.sh
|
||||
./scripts/test_cli.sh 'js rb py go php java d rs pl'
|
||||
|
||||
http-test:
|
||||
./scripts/http_test.sh
|
||||
@@ -32,3 +40,27 @@ http-test:
|
||||
zip:
|
||||
zip -r images.zip images/
|
||||
.PHONY: test build start list clean generate
|
||||
|
||||
start_docker_infra:
|
||||
docker build -t fx-docker-infra -f test/Dockerfile ./test
|
||||
docker run --rm --name fx-docker-infra -p 2222:22 -v /var/run/docker.sock:/var/run/docker.sock -d fx-docker-infra
|
||||
|
||||
test_docker_infra:
|
||||
CICD=true SSH_PORT=2222 SSH_KEY_FILE=./test/id_rsa ./build/fx infra create --name docker-local -t docker --host root@127.0.0.1
|
||||
|
||||
stop_docker_infra:
|
||||
docker stop fx-docker-infra
|
||||
|
||||
start_k3s_infra:
|
||||
multipass launch --name k3s-master --cpus 1 --mem 512M --disk 3G --cloud-init ./test/k3s/ssh-cloud-init.yaml
|
||||
multipass launch --name k3s-worker1 --cpus 1 --mem 512M --disk 3G --cloud-init ./test/k3s/ssh-cloud-init.yaml
|
||||
multipass launch --name k3s-worker2 --cpus 1 --mem 512M --disk 3G --cloud-init ./test/k3s/ssh-cloud-init.yaml
|
||||
|
||||
test_k3s_infra:
|
||||
./scripts/test_k3s_infra.sh
|
||||
|
||||
stop_k3s_infra:
|
||||
multipass delete k3s-master
|
||||
multipass delete k3s-worker1
|
||||
multipass delete k3s-worker2
|
||||
multipass purge
|
||||
|
||||
232
README.md
232
README.md
@@ -1,10 +1,11 @@
|
||||
fx
|
||||
------
|
||||
|
||||
Poor man's function as a service.
|
||||
<br/>
|
||||

|
||||

|
||||
[](https://codecov.io/gh/metrue/fx)
|
||||

|
||||

|
||||
[](https://codecov.io/gh/metrue/fx)
|
||||
[](https://goreportcard.com/report/github.com/metrue/fx)
|
||||
[](http://godoc.org/github.com/metrue/fx)
|
||||

|
||||
@@ -14,12 +15,13 @@ Poor man's function as a service.
|
||||
- [Introduction](#introduction)
|
||||
- [Installation](#installation)
|
||||
- [Usage](#usage)
|
||||
- [Manage Infrastructure](#manage-infrastructure)
|
||||
- [Contribute](#contribute)
|
||||
|
||||
|
||||
## Introduction
|
||||
|
||||
fx is a tool to help you do Function as a Service on your own server. fx can make your stateless function a service in seconds. The most exciting thing is that you can write your functions with most programming languages.
|
||||
fx is a tool to help you do Function as a Service on your own server, fx can make your stateless function a service in seconds, both Docker host and Kubernetes cluster supported. The most exciting thing is that you can write your functions with most programming languages.
|
||||
|
||||
Feel free hacking fx to support the languages not listed. Welcome to tweet me [@_metrue](https://twitter.com/_metrue) on Twitter, [@metrue](https://www.weibo.com/u/2165714507) on Weibo.
|
||||
|
||||
@@ -35,10 +37,13 @@ Feel free hacking fx to support the languages not listed. Welcome to tweet me [@
|
||||
| PHP | Supported | [@chlins](https://github.com/chlins)| [/examples/PHP](https://github.com/metrue/fx/tree/master/examples/functions/PHP) |
|
||||
| Julia | Supported | [@matbesancon](https://github.com/matbesancon)| [/examples/Julia](https://github.com/metrue/fx/tree/master/examples/functions/Julia) |
|
||||
| D | Supported | [@andre2007](https://github.com/andre2007)| [/examples/D](https://github.com/metrue/fx/tree/master/examples/functions/D) |
|
||||
| Perl | Supported | fx | [/examples/Perl](https://github.com/metrue/fx/tree/master/examples/functions/Perl) |
|
||||
| R | Working on [need your help](https://github.com/metrue/fx/issues/31) | ||
|
||||
|
||||
# Installation
|
||||
|
||||
Binaries are available for Windows, MacOS and Linux/Unix on x86. For other architectures and platforms, follow instructions to [build fx from source](#buildtest).
|
||||
|
||||
* MacOS
|
||||
|
||||
```
|
||||
@@ -58,9 +63,9 @@ curl -o- https://raw.githubusercontent.com/metrue/fx/master/scripts/install.sh |
|
||||
curl -o- https://raw.githubusercontent.com/metrue/fx/master/scripts/install.sh | sudo bash
|
||||
```
|
||||
|
||||
fx will be installed into /usr/local/bin, sometimes you may need `source ~/.zshrc` or `source ~/.bashrc` to make fx available in `$PAHT`.
|
||||
fx will be installed into /usr/local/bin, sometimes you may need `source ~/.zshrc` or `source ~/.bashrc` to make fx available in `$PATH`.
|
||||
|
||||
* Window
|
||||
* Windows
|
||||
|
||||
You can go the release page to [download](https://github.com/metrue/fx/releases) fx manually;
|
||||
|
||||
@@ -76,16 +81,16 @@ USAGE:
|
||||
fx [global options] command [command options] [arguments...]
|
||||
|
||||
VERSION:
|
||||
0.6.0
|
||||
0.8.7
|
||||
|
||||
COMMANDS:
|
||||
infra manage infrastructure of fx
|
||||
image manage image of service
|
||||
doctor health check for fx
|
||||
up deploy a function or a group of functions
|
||||
infra manage infrastructure
|
||||
up deploy a function
|
||||
down destroy a service
|
||||
list, ls list deployed services
|
||||
call run a function instantly
|
||||
image manage image of service
|
||||
doctor health check for fx
|
||||
help, h Shows a list of commands or help for one command
|
||||
|
||||
GLOBAL OPTIONS:
|
||||
@@ -93,99 +98,36 @@ GLOBAL OPTIONS:
|
||||
--version, -v print the version
|
||||
```
|
||||
|
||||
1. List your current machines and activate you machine
|
||||
|
||||
```shell
|
||||
$ fx infra ls # list machines
|
||||
|
||||
{
|
||||
"localhost": {
|
||||
"Host": "localhost",
|
||||
"User": "",
|
||||
"Password": "",
|
||||
"Enabled": true,
|
||||
"Provisioned": false
|
||||
}
|
||||
}
|
||||
|
||||
$ fx infra activate localhost # activate 'localhost'
|
||||
|
||||
2019/08/10 13:21:20 info Provision:pull python Docker base iamge: ✓
|
||||
2019/08/10 13:21:21 info Provision:pull d Docker base image: ✓
|
||||
2019/08/10 13:21:23 info Provision:pull java Docker base image: ✓
|
||||
2019/08/10 13:21:28 info Provision:pull julia Docker base image: ✓
|
||||
2019/08/10 13:21:31 info Provision:pull node Docker base image: ✓
|
||||
2019/08/10 13:22:09 info Provision:pull go Docker base image: ✓
|
||||
2019/08/10 13:22:09 info provision machine localhost: ✓
|
||||
2019/08/10 13:22:09 info enble machine localhost: ✓
|
||||
```
|
||||
It may take seconds since `fx` needs to download some basic resources
|
||||
|
||||
*Note* you can add a remote host as fx machine also,
|
||||
```
|
||||
$ fx infra add --name my_aws_vm --host 13.121.202.227 --user root --password yourpassword
|
||||
|
||||
$ fx infra list
|
||||
{
|
||||
"my_aws_vm": {
|
||||
"Host": "13.121.202.227",
|
||||
"User": "root",
|
||||
"Password": "yourpassword",
|
||||
"Enabled": false,
|
||||
"Provisioned": false
|
||||
},
|
||||
"localhost": {
|
||||
"Host": "localhost",
|
||||
"User": "",
|
||||
"Password": "",
|
||||
"Enabled": true,
|
||||
"Provisioned": true
|
||||
}
|
||||
}
|
||||
|
||||
$ fx infra activate my_aws_vm
|
||||
### Deploy your function to Docker
|
||||
|
||||
```
|
||||
then your function will be deployed onto remote host also.
|
||||
$ fx up --name hello-fx ./examples/functions/JavaScript/func.js
|
||||
|
||||
2. Write a function
|
||||
|
||||
You can check out [examples](https://github.com/metrue/fx/tree/master/examples/functions) for reference. Let's write a function as an example, it calculates the sum of two numbers then returns:
|
||||
|
||||
```js
|
||||
module.exports = (ctx) => {
|
||||
ctx.body = 'hello world'
|
||||
}
|
||||
```
|
||||
Then save it to a file `func.js`.
|
||||
|
||||
3. Deploy your function as a service
|
||||
|
||||
Give your service a port with `--port`, and name with `--name`, heath checking with `--healthcheck` if you want.
|
||||
|
||||
```shell
|
||||
$ fx up -name fx_service_name -p 10001 --healthcheck func.js
|
||||
|
||||
2019/08/10 13:26:37 info Pack Service: ✓
|
||||
2019/08/10 13:26:39 info Build Service: ✓
|
||||
2019/08/10 13:26:39 info Run Service: ✓
|
||||
2019/08/10 13:26:39 info Service (fx_service_name) is running on: 0.0.0.0:10001
|
||||
2019/08/10 13:26:39 info up function fx_service_name(func.js) to machine localhost: ✓
|
||||
+------------------------------------------------------------------+-----------+---------------+
|
||||
| ID | NAME | ENDPOINT |
|
||||
+------------------------------------------------------------------+-----------+---------------+
|
||||
| 5b24d36608ee392c937a61a530805f74551ddec304aea3aca2ffa0fabcf98cf3 | /hello-fx | 0.0.0.0:58328 |
|
||||
+------------------------------------------------------------------+-----------+---------------+
|
||||
```
|
||||
|
||||
if you want see what the source code of your service looks like, you can export it into a dirctory,
|
||||
### Deploy your function to Kubernetes
|
||||
|
||||
```shell
|
||||
$ fx image export -o <path of dir> func.js
|
||||
2019/09/25 19:31:19 info exported to <path of dir>: ✓
|
||||
```
|
||||
$ KUBECONFIG=~/.kube/config ./build/fx up examples/functions/JavaScript/func.js --name hello-fx
|
||||
|
||||
+-------------------------------+------+----------------+
|
||||
| ID | NAME | ENDPOINT |
|
||||
+----+--------------------------+-----------------------+
|
||||
| 5b24d36608ee392c937a | hello-fx | 10.0.242.75:80 |
|
||||
+------------------------+-------------+----------------+
|
||||
```
|
||||
|
||||
4. Test your service
|
||||
### Test your service
|
||||
|
||||
then you can test your service:
|
||||
|
||||
```shell
|
||||
$ curl -v 0.0.0.0:10001
|
||||
$ curl -v 0.0.0.0:58328
|
||||
|
||||
|
||||
GET / HTTP/1.1
|
||||
@@ -207,34 +149,32 @@ hello world
|
||||
|
||||
```
|
||||
|
||||
## Docker
|
||||
## Manage Infrastructure
|
||||
|
||||
TODO
|
||||
**fx** is originally designed to turn a function into a runnable Docker container in a easiest way, on a host with Docker running, you can just deploy your function with `fx up` command, and now **fx** supports deploy function to be a service onto Kubernetes cluster infrasture, and we encourage you to do that other than on bare Docker environment, there are lots of advantage to run your function on Kubernetes like self-healing, load balancing, easy horizontal scaling, etc. It's pretty simple to deploy your function onto Kubernetes with **fx**, you just set KUBECONFIG in your enviroment.
|
||||
|
||||
## Kubernetes
|
||||
By default. **fx** use localhost as target infrastructure to run your service, and you can also setup your remote virtual machines as **fx**'s infrastructure and deploy your functions onto it.
|
||||
|
||||
**fx** supports deploy function to be a service onto Kubernetes cluster infrasture, and we encourage you to do that other than on bare Docker environment, there are lots of advantage to run your function on Kubernetes like self-healing, load balancing, easy horizontal scaling, etc. It's pretty simple to deploy your function onto Kubernetes with **fx**, you just set KUBECONFIG in your enviroment.
|
||||
### `fx infra create`
|
||||
|
||||
You can create types (docker and k8s) of infrastructures for **fx** to deploy functions
|
||||
|
||||
```shell
|
||||
KUBECONFIG=<Your KUBECONFIG> fx deploy -n fx-service-abc_js -p 12349 examples/functions/JavaScript/func.js # function will be deploy to your Kubernetes cluster and expose a IP address of your loadbalencer
|
||||
$ fx infra create --name infra_us --type docker --host <user>@<ip> ## create docker type infrasture on <ip>
|
||||
$ fx infra create --name infra_bj --type k8s --master <user>@<ip> --agents '<user1>@<ip1>,<user2>@<ip2>' ## create k8s type infrasture use <ip> as master node, and <ip1> and <ip2> as agents nodes
|
||||
```
|
||||
|
||||
or
|
||||
### `fx infra use`
|
||||
|
||||
To use a infrastructure, you can use `fx infra use` command to activate it.
|
||||
|
||||
```shell
|
||||
$ export KUBECONFIG=<Your KUBECONFIG>
|
||||
$ fx deploy -n fx-service-abc_js -p 12349 examples/functions/JavaScript/func.js # function will be deploy to your Kubernetes cluster and expose a IP address of your loadbalencer
|
||||
fx infra use <infrastructure name>
|
||||
```
|
||||
|
||||
* Local Kubernetes Cluster
|
||||
and you can list your infrastructure with `fx infra list`
|
||||
|
||||
Docker for Mac and Docker for Windows already support Kubernetes with single node cluster, we can use it directly, and the default `KUBECONFIG` is `~/.kube/config`.
|
||||
|
||||
```shell
|
||||
$ export KUBECONFIG=~/.kube/config # then fx will take the config to deloy function
|
||||
```
|
||||
|
||||
if you have multiple Kubernetes clusters configured, you have to set context correctly. FYI [configure-access-multiple-clusters](https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/)
|
||||
## Use Public Cloud Kubernetes Service as infrastructure to run your functions
|
||||
|
||||
* Azure Kubernetes Service (AKS)
|
||||
|
||||
@@ -266,8 +206,32 @@ But we would suggest you run `kubectl config current-context` to check if the cu
|
||||
* Amazon Elastic Kubernetes Service (EKS)
|
||||
TODO
|
||||
|
||||
* Google Kubernetes Engine (GKET)
|
||||
TODO
|
||||
* Google Kubernetes Engine (GKE)
|
||||
|
||||
First you should create a Kubernetes cluster in your GKE, then make sure your KUBECONFIG is ready in `~/.kube/config`, if not, you can run following commands,
|
||||
|
||||
``` shell
|
||||
$ gcloud auth login
|
||||
$ gcloud container clusters get-credentials <your cluster> --zone <zone> --project <project>
|
||||
```
|
||||
|
||||
Then make sure you current context is GKE cluster, you can check it with command,
|
||||
|
||||
``` shell
|
||||
$ kubectl config current-context
|
||||
```
|
||||
|
||||
Then you can deploy your function onto GKE cluster with,
|
||||
|
||||
```shell
|
||||
$ KUBECONFIG=~/.kube/config fx up examples/functions/JavaScript/func.js --name hellojs
|
||||
```
|
||||
|
||||
* Setup your own Kubernetes cluster
|
||||
|
||||
```shell
|
||||
fx infra create --type k3s --name fx-cluster-1 --master root@123.11.2.3 --agents 'root@1.1.1.1,root@2.2.2.2'
|
||||
```
|
||||
|
||||
## Contribute
|
||||
|
||||
@@ -278,6 +242,7 @@ fx uses [Project](https://github.com/metrue/fx/projects/4) to manage the develop
|
||||
Docker: make sure [Docker](https://docs.docker.com/engine/installation/) installed and running on your server.
|
||||
|
||||
|
||||
<a name="buildtest"></a>
|
||||
#### Build & Test
|
||||
|
||||
```
|
||||
@@ -304,33 +269,54 @@ Thank you to all the people who already contributed to fx!
|
||||
<a href="https://github.com/metrue" target="_blank">
|
||||
<img alt="metrue" src="https://avatars2.githubusercontent.com/u/1001246?v=4&s=50" width="50">
|
||||
</a>
|
||||
<a href="https://github.com/pplam" target="_blank">
|
||||
<img alt="pplam" src="https://avatars2.githubusercontent.com/u/12783579?v=4&s=50" width="50">
|
||||
</a>
|
||||
<a href="https://github.com/muka" target="_blank">
|
||||
<img alt="muka" src="https://avatars2.githubusercontent.com/u/1021269?v=4&s=50" width="50">
|
||||
</a>
|
||||
<a href="https://github.com/xwjdsh" target="_blank">
|
||||
<img alt="xwjdsh" src="https://avatars2.githubusercontent.com/u/11025519?v=4&s=50" width="50">
|
||||
<a href="https://github.com/pplam" target="_blank">
|
||||
<img alt="pplam" src="https://avatars2.githubusercontent.com/u/12783579?v=4&s=50" width="50">
|
||||
</a>
|
||||
<a href="https://github.com/mbesancon" target="_blank">
|
||||
<img alt="mbesancon" src="https://avatars2.githubusercontent.com/u/7623090?v=4&s=50" width="50">
|
||||
</a>
|
||||
<a href="https://github.com/avelino" target="_blank">
|
||||
<img alt="avelino" src="https://avatars2.githubusercontent.com/u/31996?v=4&s=50" width="50">
|
||||
</a>
|
||||
<a href="https://github.com/DaidoujiChen" target="_blank">
|
||||
<img alt="DaidoujiChen" src="https://avatars0.githubusercontent.com/u/670441?v=4&s=50" width="50">
|
||||
<a href="https://github.com/matbesancon" target="_blank">
|
||||
<img alt="mbesancon" src="https://avatars2.githubusercontent.com/u/7623090?s=60&v=4" width="50">
|
||||
</a>
|
||||
<a href="https://github.com/chlins" target="_blank">
|
||||
<img alt="chlins" src="https://avatars2.githubusercontent.com/u/31262637?v=4&s=50" width="50">
|
||||
</a>
|
||||
<a href="https://github.com/xwjdsh" target="_blank">
|
||||
<img alt="xwjdsh" src="https://avatars2.githubusercontent.com/u/11025519?v=4&s=50" width="50">
|
||||
</a>
|
||||
<a href="https://github.com/DaidoujiChen" target="_blank">
|
||||
<img alt="DaidoujiChen" src="https://avatars0.githubusercontent.com/u/670441?v=4&s=50" width="50">
|
||||
</a>
|
||||
<a href="https://github.com/avelino" target="_blank">
|
||||
<img alt="avelino" src="https://avatars2.githubusercontent.com/u/31996?v=4&s=50" width="50">
|
||||
</a>
|
||||
<a href="https://github.com/andre2007" target="_blank">
|
||||
<img alt="andre2007" src="https://avatars1.githubusercontent.com/u/1451047?s=50&v=4" width="50">
|
||||
</a>
|
||||
<a href="https://github.com/polyrabbit" target="_blank">
|
||||
<img alt="polyrabbit" src="https://avatars0.githubusercontent.com/u/2657334?s=60&v=4" width="50">
|
||||
</a>
|
||||
<a href="https://github.com/johnlunney" target="_blank">
|
||||
<img alt="johnlunney" src="https://avatars3.githubusercontent.com/u/536947?s=60&v=4" width="50">
|
||||
</a>
|
||||
<a href="https://github.com/tbrand" target="_blank">
|
||||
<img alt="tbrand" src="https://avatars0.githubusercontent.com/u/3483230?s=60&v=4" width="50">
|
||||
</a>
|
||||
<a href="https://github.com/steventhanna" target="_blank">
|
||||
<img alt="andre2007" src="https://avatars1.githubusercontent.com/u/2541678?s=50&v=4" width="50">
|
||||
</a>
|
||||
<a href="https://github.com/border-radius" target="_blank">
|
||||
<img alt="border-radius" src="https://avatars0.githubusercontent.com/u/3204785?s=60&v=4" width="50">
|
||||
</a>
|
||||
<a href="https://github.com/Russtopia" target="_blank">
|
||||
<img alt="Russtopia" src="https://avatars1.githubusercontent.com/u/2966177?s=60&v=4<Paste>" width="50">
|
||||
</a>
|
||||
<a href="https://github.com/FrontMage" target="_blank">
|
||||
<img alt="FrontMage" src="https://avatars2.githubusercontent.com/u/17007026?s=60&v=4" width="50">
|
||||
</a>
|
||||
<a href="https://github.com/DropNib" target="_blank">
|
||||
<img alt="DropNib" src="https://avatars0.githubusercontent.com/u/32019589?s=60&v=4" width="50">
|
||||
</a>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
4
assets/dockerfiles/base/go/Dockerfile
vendored
Normal file
4
assets/dockerfiles/base/go/Dockerfile
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
FROM golang:latest
|
||||
|
||||
# dependency management
|
||||
RUN go get github.com/gin-gonic/gin
|
||||
26
assets/dockerfiles/base/node/package-lock.json
generated
vendored
Normal file
26
assets/dockerfiles/base/node/package-lock.json
generated
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"name": "fx-node-base",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
"@koa/cors": {
|
||||
"version": "2.2.3",
|
||||
"resolved": "https://registry.npmjs.org/@koa/cors/-/cors-2.2.3.tgz",
|
||||
"integrity": "sha512-tCVVXa39ETsit5kGBtEWWimjLn1sDaeu8+0phgb8kT3GmBDZOykkI3ZO8nMjV2p3MGkJI4K5P+bxR8Ztq0bwsA==",
|
||||
"requires": {
|
||||
"vary": "^1.1.2"
|
||||
}
|
||||
},
|
||||
"node-fetch": {
|
||||
"version": "2.6.0",
|
||||
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz",
|
||||
"integrity": "sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA=="
|
||||
},
|
||||
"vary": {
|
||||
"version": "1.1.2",
|
||||
"resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",
|
||||
"integrity": "sha1-IpnwLG3tMNSllhsLn3RSShj2NPw="
|
||||
}
|
||||
}
|
||||
}
|
||||
9
assets/dockerfiles/base/node/package.json
vendored
9
assets/dockerfiles/base/node/package.json
vendored
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"name": "aok",
|
||||
"name": "fx-node-base",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
@@ -10,12 +10,11 @@
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@koa/cors": "^2.2.3",
|
||||
"get-port": "^3.2.0",
|
||||
"is-generator-function": "^1.0.6",
|
||||
"koa": "^2.3.0",
|
||||
"koa-bodyparser": "^4.2.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"get-port-cli": "^1.1.0"
|
||||
"koa-bodyparser": "^4.2.0",
|
||||
"node-fetch": "^2.6.0"
|
||||
}
|
||||
}
|
||||
|
||||
10
assets/dockerfiles/base/perl/Dockerfile
vendored
Normal file
10
assets/dockerfiles/base/perl/Dockerfile
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
FROM alpine:3.4
|
||||
MAINTAINER Mojolicious
|
||||
|
||||
ADD . .
|
||||
COPY cpanfile /
|
||||
ENV EV_EXTRA_DEFS -DEV_NO_ATFORK
|
||||
|
||||
RUN apk update && \
|
||||
apk add perl perl-io-socket-ssl perl-dbd-pg perl-dev g++ make wget curl && \
|
||||
curl -L https://cpanmin.us | perl - App::cpanminus && cpanm --installdeps . -M https://cpan.metacpan.org
|
||||
3
assets/dockerfiles/base/perl/cpanfile
vendored
Normal file
3
assets/dockerfiles/base/perl/cpanfile
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
requires "EV";
|
||||
requires "JSON";
|
||||
requires "Mojolicious::Lite";
|
||||
3
assets/dockerfiles/base/ruby/Dockerfile
vendored
Normal file
3
assets/dockerfiles/base/ruby/Dockerfile
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
FROM ruby:latest
|
||||
|
||||
RUN gem install sinatra
|
||||
44
bundle/bundle.go
Normal file
44
bundle/bundle.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package bundle
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/metrue/fx/bundler"
|
||||
"github.com/metrue/fx/bundler/d"
|
||||
golang "github.com/metrue/fx/bundler/go"
|
||||
"github.com/metrue/fx/bundler/java"
|
||||
"github.com/metrue/fx/bundler/julia"
|
||||
"github.com/metrue/fx/bundler/node"
|
||||
"github.com/metrue/fx/bundler/perl"
|
||||
"github.com/metrue/fx/bundler/python"
|
||||
"github.com/metrue/fx/bundler/ruby"
|
||||
"github.com/metrue/fx/bundler/rust"
|
||||
)
|
||||
|
||||
// Bundle function to project
|
||||
func Bundle(workdir string, language string, fn string, deps ...string) error {
|
||||
var bundler bundler.Bundler
|
||||
switch language {
|
||||
case "d":
|
||||
bundler = d.New()
|
||||
case "node":
|
||||
bundler = node.New()
|
||||
case "go":
|
||||
bundler = golang.New()
|
||||
case "java":
|
||||
bundler = java.New()
|
||||
case "julia":
|
||||
bundler = julia.New()
|
||||
case "perl":
|
||||
bundler = perl.New()
|
||||
case "python":
|
||||
bundler = python.New()
|
||||
case "ruby":
|
||||
bundler = ruby.New()
|
||||
case "rust":
|
||||
bundler = rust.New()
|
||||
default:
|
||||
return fmt.Errorf("%s not suppported yet", language)
|
||||
}
|
||||
return bundler.Bundle(workdir, fn, deps...)
|
||||
}
|
||||
117
bundle/bundler_test.go
Normal file
117
bundle/bundler_test.go
Normal file
@@ -0,0 +1,117 @@
|
||||
package bundle
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func createFn(content string, t *testing.T) string {
|
||||
fd, err := ioutil.TempFile("", "fx_func_*.js")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = ioutil.WriteFile(fd.Name(), []byte(content), 0666)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
return fd.Name()
|
||||
}
|
||||
|
||||
func TestBundle(t *testing.T) {
|
||||
workdir, err := ioutil.TempDir("", "fx-test")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(workdir)
|
||||
|
||||
cases := []struct {
|
||||
workdir string
|
||||
language string
|
||||
fn string
|
||||
deps []string
|
||||
}{
|
||||
{
|
||||
workdir: workdir,
|
||||
language: "d",
|
||||
fn: `
|
||||
module.exports = (ctx) => {
|
||||
ctx.body = 'hello fx'
|
||||
}
|
||||
`,
|
||||
},
|
||||
{
|
||||
workdir: workdir,
|
||||
language: "go",
|
||||
fn: `
|
||||
module.exports = (ctx) => {
|
||||
ctx.body = 'hello fx'
|
||||
}
|
||||
`,
|
||||
},
|
||||
{
|
||||
workdir: workdir,
|
||||
language: "java",
|
||||
fn: `
|
||||
module.exports = (ctx) => {
|
||||
ctx.body = 'hello fx'
|
||||
}
|
||||
`,
|
||||
},
|
||||
{
|
||||
workdir: workdir,
|
||||
language: "julia",
|
||||
fn: `
|
||||
module.exports = (ctx) => {
|
||||
ctx.body = 'hello fx'
|
||||
}
|
||||
`,
|
||||
},
|
||||
{
|
||||
workdir: workdir,
|
||||
language: "perl",
|
||||
fn: `
|
||||
module.exports = (ctx) => {
|
||||
ctx.body = 'hello fx'
|
||||
}
|
||||
`,
|
||||
},
|
||||
{
|
||||
workdir: workdir,
|
||||
language: "python",
|
||||
fn: `
|
||||
module.exports = (ctx) => {
|
||||
ctx.body = 'hello fx'
|
||||
}
|
||||
`,
|
||||
},
|
||||
{
|
||||
workdir: workdir,
|
||||
language: "ruby",
|
||||
fn: `
|
||||
module.exports = (ctx) => {
|
||||
ctx.body = 'hello fx'
|
||||
}
|
||||
`,
|
||||
},
|
||||
{
|
||||
workdir: workdir,
|
||||
language: "rust",
|
||||
fn: `
|
||||
module.exports = (ctx) => {
|
||||
ctx.body = 'hello fx'
|
||||
}
|
||||
`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, c := range cases {
|
||||
fn := createFn(c.fn, t)
|
||||
defer os.Remove(fn)
|
||||
|
||||
if err := Bundle(c.workdir, c.language, fn, c.deps...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
88
bundler/bundler.go
Normal file
88
bundler/bundler.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package bundler
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/gobuffalo/packd"
|
||||
"github.com/gobuffalo/packr/v2"
|
||||
"github.com/metrue/fx/constants"
|
||||
"github.com/metrue/fx/utils"
|
||||
)
|
||||
|
||||
// Bundler defines interface
|
||||
type Bundler interface {
|
||||
Scaffold(output string) error
|
||||
Bundle(output string, fn string, deps ...string) error
|
||||
}
|
||||
|
||||
// IsHandler check if it's handle file
|
||||
func IsHandler(name string, lang string) bool {
|
||||
basename := filepath.Base(name)
|
||||
nameWithoutExt := strings.TrimSuffix(basename, filepath.Ext(basename))
|
||||
if constants.ExtLangMapping[filepath.Ext(basename)] != lang {
|
||||
return false
|
||||
}
|
||||
|
||||
return (nameWithoutExt == "fx" ||
|
||||
// Fx is for Java
|
||||
nameWithoutExt == "Fx" ||
|
||||
// mod.rs is for Rust)
|
||||
nameWithoutExt == "mod")
|
||||
}
|
||||
|
||||
// Restore directory from packr box
|
||||
func Restore(box *packr.Box, output string) error {
|
||||
if err := box.Walk(func(name string, fd packd.File) error {
|
||||
content, err := box.Find(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dest := filepath.Join(output, name)
|
||||
if err := utils.EnsureFile(dest); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := ioutil.WriteFile(dest, content, 0644); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Bundle bundle a function
|
||||
func Bundle(box *packr.Box, output string, language string, fn string, deps ...string) error {
|
||||
if err := Restore(box, output); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := utils.Merge(output, deps...); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Replace the default handler source file with given function source file
|
||||
if err := filepath.Walk(output, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if IsHandler(path, language) {
|
||||
if err := utils.CopyFile(fn, path); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
51
bundler/bundler_test.go
Normal file
51
bundler/bundler_test.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package bundler
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/gobuffalo/packr/v2"
|
||||
)
|
||||
|
||||
func TestBundler(t *testing.T) {
|
||||
t.Run("Restore", func(t *testing.T) {
|
||||
box := packr.New("", "./node/assets")
|
||||
outputDir, err := ioutil.TempDir("", "fx_koa")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(outputDir)
|
||||
if err := Restore(box, outputDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Bundle", func(t *testing.T) {
|
||||
fd, err := ioutil.TempFile("", "fx_func_*.js")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(fd.Name())
|
||||
|
||||
content := `
|
||||
module.exports = (ctx) => {
|
||||
ctx.body = 'hello fx'
|
||||
}`
|
||||
if err = ioutil.WriteFile(fd.Name(), []byte(content), 0666); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
outputDir, err := ioutil.TempDir("", "fx_koa")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(outputDir)
|
||||
|
||||
box := packr.New("", "./node/assets")
|
||||
if err := Bundle(box, outputDir, "node", fd.Name()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
8
bundler/d/d-packr.go
Normal file
8
bundler/d/d-packr.go
Normal file
@@ -0,0 +1,8 @@
|
||||
// +build !skippackr
|
||||
// Code generated by github.com/gobuffalo/packr/v2. DO NOT EDIT.
|
||||
|
||||
// You can use the "packr clean" command to clean up this,
|
||||
// and any other packr generated files.
|
||||
package d
|
||||
|
||||
import _ "github.com/metrue/fx/bundler/d/packrd"
|
||||
32
bundler/d/d.go
Normal file
32
bundler/d/d.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package d
|
||||
|
||||
import (
|
||||
"github.com/gobuffalo/packr/v2"
|
||||
"github.com/metrue/fx/bundler"
|
||||
)
|
||||
|
||||
// D defines d bundler
|
||||
type D struct {
|
||||
assets *packr.Box
|
||||
}
|
||||
|
||||
// New a koa bundler
|
||||
func New() *D {
|
||||
return &D{
|
||||
assets: packr.New("d", "./assets"),
|
||||
}
|
||||
}
|
||||
|
||||
// Scaffold a koa app
|
||||
func (d *D) Scaffold(output string) error {
|
||||
return bundler.Restore(d.assets, output)
|
||||
}
|
||||
|
||||
// Bundle a function into a koa project
|
||||
func (d *D) Bundle(output string, fn string, deps ...string) error {
|
||||
return bundler.Bundle(d.assets, output, "d", fn, deps...)
|
||||
}
|
||||
|
||||
var (
|
||||
_ bundler.Bundler = &D{}
|
||||
)
|
||||
35
bundler/d/packrd/packed-packr.go
Normal file
35
bundler/d/packrd/packed-packr.go
Normal file
File diff suppressed because one or more lines are too long
@@ -1,4 +1,4 @@
|
||||
FROM metrue/fx-go-base:latest
|
||||
FROM metrue/fx-go-base
|
||||
|
||||
COPY . /go/src/github.com/metrue/fx
|
||||
WORKDIR /go/src/github.com/metrue/fx
|
||||
32
bundler/go/go.go
Normal file
32
bundler/go/go.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package golang
|
||||
|
||||
import (
|
||||
"github.com/gobuffalo/packr/v2"
|
||||
"github.com/metrue/fx/bundler"
|
||||
)
|
||||
|
||||
// Gin defines javascript bundler
|
||||
type Gin struct {
|
||||
assets *packr.Box
|
||||
}
|
||||
|
||||
// New a koa bundler
|
||||
func New() *Gin {
|
||||
return &Gin{
|
||||
assets: packr.New("go", "./assets"),
|
||||
}
|
||||
}
|
||||
|
||||
// Scaffold a koa app
|
||||
func (g *Gin) Scaffold(output string) error {
|
||||
return bundler.Restore(g.assets, output)
|
||||
}
|
||||
|
||||
// Bundle a function into a koa project
|
||||
func (g *Gin) Bundle(output string, fn string, deps ...string) error {
|
||||
return bundler.Bundle(g.assets, output, "go", fn, deps...)
|
||||
}
|
||||
|
||||
var (
|
||||
_ bundler.Bundler = &Gin{}
|
||||
)
|
||||
143
bundler/go/go_test.go
Normal file
143
bundler/go/go_test.go
Normal file
@@ -0,0 +1,143 @@
|
||||
package golang
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/metrue/fx/utils"
|
||||
)
|
||||
|
||||
func TestKoaBundler(t *testing.T) {
|
||||
t.Run("Scaffold", func(t *testing.T) {
|
||||
outputDir, err := ioutil.TempDir("", "fx_koa")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(outputDir)
|
||||
|
||||
koa := New()
|
||||
if err := koa.Scaffold(outputDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
diff, _, _, err := utils.Diff(outputDir, "./assets")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if diff {
|
||||
t.Fatalf("%s is not equal with %s", outputDir, "./assets")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("BundleSingleFunc", func(t *testing.T) {
|
||||
fd, err := ioutil.TempFile("", "fx_func_*.js")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(fd.Name())
|
||||
|
||||
content := `
|
||||
module.exports = (ctx) => {
|
||||
ctx.body = 'hello fx'
|
||||
}`
|
||||
err = ioutil.WriteFile(fd.Name(), []byte(content), 0666)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
outputDir, err := ioutil.TempDir("", "fx_koa")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(outputDir)
|
||||
|
||||
koa := New()
|
||||
if err := koa.Bundle(outputDir, fd.Name()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
diff, pre, cur, err := utils.Diff("./assets", outputDir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !diff {
|
||||
t.Fatalf("handle functino should be changed")
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(cur, []byte(content)) {
|
||||
t.Fatalf("it should be %s but got %s", content, cur)
|
||||
}
|
||||
|
||||
preHandleFunc, err := ioutil.ReadFile("./assets/fx.go")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(pre, preHandleFunc) {
|
||||
{
|
||||
}
|
||||
t.Fatalf("it should get %s but got %s", preHandleFunc, pre)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("BundleFuncAndDeps", func(t *testing.T) {
|
||||
fd, err := ioutil.TempFile("", "fx_func_*.js")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(fd.Name())
|
||||
|
||||
content, err := ioutil.ReadFile("./assets/fx.go")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = ioutil.WriteFile(fd.Name(), content, 0666)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
addFunc := `
|
||||
module.exports = (a, b) => a+b
|
||||
`
|
||||
addFd, err := ioutil.TempFile("", "fx_add_func_*.js")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile(addFd.Name(), []byte(addFunc), 0644)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
outputDir, err := ioutil.TempDir("", "fx_koa")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(outputDir)
|
||||
|
||||
koa := New()
|
||||
if err := koa.Bundle(outputDir, fd.Name(), addFd.Name()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
diff, pre, cur, err := utils.Diff("./assets", outputDir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !diff {
|
||||
t.Fatalf("handle functino should be changed")
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(cur, []byte(addFunc)) {
|
||||
t.Fatalf("it should be %s but got %s", content, cur)
|
||||
}
|
||||
if pre != nil {
|
||||
t.Fatal(pre)
|
||||
}
|
||||
})
|
||||
}
|
||||
8
bundler/go/golang-packr.go
Normal file
8
bundler/go/golang-packr.go
Normal file
@@ -0,0 +1,8 @@
|
||||
// +build !skippackr
|
||||
// Code generated by github.com/gobuffalo/packr/v2. DO NOT EDIT.
|
||||
|
||||
// You can use the "packr clean" command to clean up this,
|
||||
// and any other packr generated files.
|
||||
package golang
|
||||
|
||||
import _ "github.com/metrue/fx/bundler/go/packrd"
|
||||
33
bundler/go/packrd/packed-packr.go
Normal file
33
bundler/go/packrd/packed-packr.go
Normal file
@@ -0,0 +1,33 @@
|
||||
// +build !skippackr
|
||||
// Code generated by github.com/gobuffalo/packr/v2. DO NOT EDIT.
|
||||
|
||||
// You can use the "packr2 clean" command to clean up this,
|
||||
// and any other packr generated files.
|
||||
package packrd
|
||||
|
||||
import (
|
||||
"github.com/gobuffalo/packr/v2"
|
||||
"github.com/gobuffalo/packr/v2/file/resolver"
|
||||
)
|
||||
|
||||
var _ = func() error {
|
||||
const gk = "449af63614707e60b8b2ffe119b470cf"
|
||||
g := packr.New(gk, "")
|
||||
hgr, err := resolver.NewHexGzip(map[string]string{
|
||||
"76eca10e3a22b8409f5af0b284f82ee4": "1f8b08000000000000ff7ccabdaac2301407f0fd3cc59fec490af711da5e10a929115111877ee5586839a569318fefe6e8fefbf7aec23c6ceb3ed890348b6e9b3810e5aebec3c0b2d8b87696c7edb5b7a693d97e315d9d3f1607ff1b91bf9cc082761fa71e7aeac3d47084d26fe8a8a005212124c38266590c0b5179abddb9c45f9665447955e0a18c0d493de9130000ffff44ef0fc1ae000000",
|
||||
"cf8b44c6431c48afd768cf293ee38cab": "1f8b08000000000000ff4c8cc1aac2301045d799af98975502efa585b7ab74a7b854d41f88218983edb40c0d08d27f9766e5eede03e7cc3e3c7d8e387a62001ae7491634a074a6e551ee2e4c639389fff2c414b6a5c102a4c2a12ac6e21b9460d7632676fb987c19166341893b1e6e4637fa17d3abfef3e9fa0d2861946a8abb1436bafb6fdb56db5dc53f3d320d5b5ccd9e2998286241adb0c2270000ffff1428eddcb3000000",
|
||||
"da1f5928cfe751551db26cc0459d944e": "1f8b08000000000000ff14c9b10e82301000d0b9f715974e601089a3ab8b71d0c12fa8b5948bed1d81233621fcbb617bc91b9dffba18303b6200caa34c8a36920ecbbbf5924f91f81885c9efb200fdc21efb52792d7888c4ed555843d11a57305e4b7b7f3d1fd5b9eb1adcf7b682313687797631d80bda21a424f893297d6c0366ab61837f000000ffffe12418cf85000000",
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
g.DefaultResolver = hgr
|
||||
|
||||
func() {
|
||||
b := packr.New("go", "./assets")
|
||||
b.SetResolver("Dockerfile", packr.Pointer{ForwardBox: gk, ForwardPath: "76eca10e3a22b8409f5af0b284f82ee4"})
|
||||
b.SetResolver("app.go", packr.Pointer{ForwardBox: gk, ForwardPath: "cf8b44c6431c48afd768cf293ee38cab"})
|
||||
b.SetResolver("fx.go", packr.Pointer{ForwardBox: gk, ForwardPath: "da1f5928cfe751551db26cc0459d944e"})
|
||||
}()
|
||||
return nil
|
||||
}()
|
||||
8
bundler/java/java-packr.go
Normal file
8
bundler/java/java-packr.go
Normal file
@@ -0,0 +1,8 @@
|
||||
// +build !skippackr
|
||||
// Code generated by github.com/gobuffalo/packr/v2. DO NOT EDIT.
|
||||
|
||||
// You can use the "packr clean" command to clean up this,
|
||||
// and any other packr generated files.
|
||||
package java
|
||||
|
||||
import _ "github.com/metrue/fx/bundler/java/packrd"
|
||||
32
bundler/java/java.go
Normal file
32
bundler/java/java.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package java
|
||||
|
||||
import (
|
||||
"github.com/gobuffalo/packr/v2"
|
||||
"github.com/metrue/fx/bundler"
|
||||
)
|
||||
|
||||
// Java defines javascript bundler
|
||||
type Java struct {
|
||||
assets *packr.Box
|
||||
}
|
||||
|
||||
// New a koa bundler
|
||||
func New() *Java {
|
||||
return &Java{
|
||||
assets: packr.New("java", "./assets"),
|
||||
}
|
||||
}
|
||||
|
||||
// Scaffold a koa app
|
||||
func (k *Java) Scaffold(output string) error {
|
||||
return bundler.Restore(k.assets, output)
|
||||
}
|
||||
|
||||
// Bundle a function into a koa project
|
||||
func (k *Java) Bundle(output string, fn string, deps ...string) error {
|
||||
return bundler.Bundle(k.assets, output, "java", fn, deps...)
|
||||
}
|
||||
|
||||
var (
|
||||
_ bundler.Bundler = &Java{}
|
||||
)
|
||||
155
bundler/java/java_test.go
Normal file
155
bundler/java/java_test.go
Normal file
@@ -0,0 +1,155 @@
|
||||
package java
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/metrue/fx/utils"
|
||||
)
|
||||
|
||||
func TestJavaBundler(t *testing.T) {
|
||||
t.Run("Scaffold", func(t *testing.T) {
|
||||
outputDir, err := ioutil.TempDir("", "fx_java")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(outputDir)
|
||||
|
||||
java := New()
|
||||
if err := java.Scaffold(outputDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
diff, _, _, err := utils.Diff(outputDir, "./assets")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if diff {
|
||||
t.Fatalf("%s is not equal with %s", outputDir, "./assets")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("BundleSingleFunc", func(t *testing.T) {
|
||||
fd, err := ioutil.TempFile("", "fx_func_*.java")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(fd.Name())
|
||||
|
||||
content := `package fx;
|
||||
|
||||
import io.javalin.Javalin;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class app {
|
||||
public static void main(String[] args) {
|
||||
Javalin app = Javalin.start(3000);
|
||||
Fx handler = new Fx();
|
||||
app.post("/", ctx -> {
|
||||
JSONObject obj = new JSONObject(ctx.body());
|
||||
ctx.result(""+handler.handle(obj));
|
||||
});
|
||||
}
|
||||
}
|
||||
`
|
||||
err = ioutil.WriteFile(fd.Name(), []byte(content), 0666)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
outputDir, err := ioutil.TempDir("", "fx_java")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(outputDir)
|
||||
|
||||
java := New()
|
||||
if err := java.Bundle(outputDir, fd.Name()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
diff, pre, cur, err := utils.Diff("./assets", outputDir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !diff {
|
||||
t.Fatalf("handle functino should be changed")
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(cur, []byte(content)) {
|
||||
t.Fatalf("it should be %s but got %s", content, cur)
|
||||
}
|
||||
|
||||
preHandleFunc, err := ioutil.ReadFile("./assets/src/main/java/fx/Fx.java")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(pre, preHandleFunc) {
|
||||
{
|
||||
}
|
||||
t.Fatalf("it should get %s but got %s", preHandleFunc, pre)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("BundleFuncAndDeps", func(t *testing.T) {
|
||||
fd, err := ioutil.TempFile("", "fx_func_*.js")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(fd.Name())
|
||||
|
||||
content, err := ioutil.ReadFile("./assets/src/main/java/fx/Fx.java")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = ioutil.WriteFile(fd.Name(), content, 0666)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
addFunc := `
|
||||
module.exports = (a, b) => a+b
|
||||
`
|
||||
addFd, err := ioutil.TempFile("", "fx_add_func_*.js")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile(addFd.Name(), []byte(addFunc), 0644)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
outputDir, err := ioutil.TempDir("", "fx_java")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(outputDir)
|
||||
|
||||
java := New()
|
||||
if err := java.Bundle(outputDir, fd.Name(), addFd.Name()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
diff, pre, cur, err := utils.Diff("./assets", outputDir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !diff {
|
||||
t.Fatalf("handle functino should be changed")
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(cur, []byte(addFunc)) {
|
||||
t.Fatalf("it should be %s but got %s", content, cur)
|
||||
}
|
||||
if pre != nil {
|
||||
t.Fatal(pre)
|
||||
}
|
||||
})
|
||||
}
|
||||
35
bundler/java/packrd/packed-packr.go
Normal file
35
bundler/java/packrd/packed-packr.go
Normal file
@@ -0,0 +1,35 @@
|
||||
// +build !skippackr
|
||||
// Code generated by github.com/gobuffalo/packr/v2. DO NOT EDIT.
|
||||
|
||||
// You can use the "packr2 clean" command to clean up this,
|
||||
// and any other packr generated files.
|
||||
package packrd
|
||||
|
||||
import (
|
||||
"github.com/gobuffalo/packr/v2"
|
||||
"github.com/gobuffalo/packr/v2/file/resolver"
|
||||
)
|
||||
|
||||
var _ = func() error {
|
||||
const gk = "472df26e20796ab2f7966f2dbcce8ee2"
|
||||
g := packr.New(gk, "")
|
||||
hgr, err := resolver.NewHexGzip(map[string]string{
|
||||
"3ce2296391d96d76aaff2d829f1f612d": "1f8b08000000000000ff4c4fb16ec32014dcf98a274f586da9a58e563a66c8d00c19ab0e0f4c5d2801042f2955e57faf9c10c7b71cdc9dee20a2fac651c367e91933c718128109c2e2199df16277e5fe6685340a9b8317bbc3fe6d2fad56d433164fd21905ca61ce8031c21f0300a8722624a3e01ccc0047349e1f28193fbe7f00a631b7353ca3ae5d2a36b79bc88489f84bd7756dbf44b705bed00f4e27d880d73fb02d7c65638c22864cbc796e1e415181a7d7d5d0656cf90104696bcb5de48a8a9061f8e5edaa77c66c249d4f8e78d33cd457882bf320ed3a3fd5f3c426f61f0000ffff88067e0e6b010000",
|
||||
"74b2f70d9674f4e1a69013f26355fca9": "1f8b08000000000000ff8c55516fdb20107ecfafb0fc1eb0bb4aab2a4a1f264daad46ad3d64d7bab6ef8ec90da808024debf9f0c4e63c775dc3c85fb3eeebeef8033bb6f9b3ad9a37552abbb3427599aa012ba90aaba4b7f3d7f5ddfa4f77cc58cd55b143e699b5ab9bb74e3bdb9a5b4813d2a0206c40689b615fdfeed895e93accb1298b7ad936fecc3e1400e9f02ef2acb72fae7e9f1a7d860036ba99c0725305d254992b44edeba003c6a013e085b2c98cc314260bdbf7ec95e32d2ba22e5a1086b7481f5ef689c87148c8e62915659bd330f05d7b622ce58a9aad24283076d5f193d82910ad6cb12847f2878d9aec198f516f6c0e8201e8906c42b5452557c0b96d1d332c2fd69f08ce49da8e37215d1bf3b59f78962b27a5749e54e9141741c9cf8e9bb145bd6e7393335da3b3012bbea3650e03aee9cfa1ced3dbab822f9c0d284862d8a5d77e46e0a8e09efe3d1fc061cf2d85664342ee7e995867aa6dc88c3835d46c3fff97474211f135a95b2da59b86c2370bd05e54a6d1bb40b12cfe98be4ee271b536383caf7cf6cee5690609d58747a6705922750b244e77ff481e753dd74596690da80545f6a708e972d0163183d45968dd281d38516d28ff590d10f9c0ba317eedf003c7f8ef4fc3d1e233d91d1c1b3ee86ad41eb250ed2b0781e423746d66849ec3bbf61f47d6076a3075ba17f67630fac82b8910256a04155a01293c0bfb3312935e9865eddcd83d9e9f8c6984cc6e360c8497e3627181dd79c97d0dde1add317050478b6fa55967fceb3fce6b2003aee4a685af78de4abff010000ffff2998100355070000",
|
||||
"9d097ccc5e31491bd2ee70619fc36101": "1f8b08000000000000ff74cbc1aac2301005d07dbe62e82ae1417ea0bced83be855df40b26718ca97512922914a4ff2ed282a87897f79e9bd15f30109c9656a978cda908a412ec5813dbffa13ff46e242fad52797653f4e027ac15fe16b8290080bd8d2c70463e4ea49f27889c6731bb7c6490123900c2efb6d940a21b6c8c95b46ddab4efdabd6af7451792b930742c14a8d88ca552c7a2d1c0cf67ebf6e7aa56750f0000ffffeb12798604010000",
|
||||
"bde0ba82beaa4757c20608eb0bcc46bd": "1f8b08000000000000ff2ccecf4a033110c7f17b9e6288d7661351c46b71ebad562a82507a984da64bd2e60f93ece2e3cbae3dcde1cbfcf8bc1f0f7b88d478227df955016754035612e201b6cef93442cd135bda80cdb1f81b01260705ed1547029f5a06840b3608c862dbf750d982b6d991ae6cc5f1fb034e32ce496e40debfe45988ddcfe7e16b074fc618f1b6efe124f55459dffca0c31cf5ea7855b9500aeeaa30ba97673df8b486654a05e4e536e491da42c752fef9a67bec4cb7f4b3f80b0000ffff3eecd9bedf000000",
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
g.DefaultResolver = hgr
|
||||
|
||||
func() {
|
||||
b := packr.New("java", "./assets")
|
||||
b.SetResolver("Dockerfile", packr.Pointer{ForwardBox: gk, ForwardPath: "bde0ba82beaa4757c20608eb0bcc46bd"})
|
||||
b.SetResolver("pom.xml", packr.Pointer{ForwardBox: gk, ForwardPath: "74b2f70d9674f4e1a69013f26355fca9"})
|
||||
b.SetResolver("src/main/java/fx/Fx.java", packr.Pointer{ForwardBox: gk, ForwardPath: "9d097ccc5e31491bd2ee70619fc36101"})
|
||||
b.SetResolver("src/main/java/fx/app.java", packr.Pointer{ForwardBox: gk, ForwardPath: "3ce2296391d96d76aaff2d829f1f612d"})
|
||||
}()
|
||||
return nil
|
||||
}()
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
struct Input
|
||||
a::Number
|
||||
b::Number
|
||||
8
bundler/julia/julia-packr.go
Normal file
8
bundler/julia/julia-packr.go
Normal file
@@ -0,0 +1,8 @@
|
||||
// +build !skippackr
|
||||
// Code generated by github.com/gobuffalo/packr/v2. DO NOT EDIT.
|
||||
|
||||
// You can use the "packr clean" command to clean up this,
|
||||
// and any other packr generated files.
|
||||
package julia
|
||||
|
||||
import _ "github.com/metrue/fx/bundler/julia/packrd"
|
||||
32
bundler/julia/julia.go
Normal file
32
bundler/julia/julia.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package julia
|
||||
|
||||
import (
|
||||
"github.com/gobuffalo/packr/v2"
|
||||
"github.com/metrue/fx/bundler"
|
||||
)
|
||||
|
||||
// Julia defines javascript bundler
|
||||
type Julia struct {
|
||||
assets *packr.Box
|
||||
}
|
||||
|
||||
// New a koa bundler
|
||||
func New() *Julia {
|
||||
return &Julia{
|
||||
assets: packr.New("julia", "./assets"),
|
||||
}
|
||||
}
|
||||
|
||||
// Scaffold a koa app
|
||||
func (k *Julia) Scaffold(output string) error {
|
||||
return bundler.Restore(k.assets, output)
|
||||
}
|
||||
|
||||
// Bundle a function into a koa project
|
||||
func (k *Julia) Bundle(output string, fn string, deps ...string) error {
|
||||
return bundler.Bundle(k.assets, output, "julia", fn, deps...)
|
||||
}
|
||||
|
||||
var (
|
||||
_ bundler.Bundler = &Julia{}
|
||||
)
|
||||
148
bundler/julia/julia_test.go
Normal file
148
bundler/julia/julia_test.go
Normal file
@@ -0,0 +1,148 @@
|
||||
package julia
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/metrue/fx/utils"
|
||||
)
|
||||
|
||||
func TestJuliaBundler(t *testing.T) {
|
||||
t.Run("Scaffold", func(t *testing.T) {
|
||||
outputDir, err := ioutil.TempDir("", "fx_julia")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(outputDir)
|
||||
|
||||
julia := New()
|
||||
if err := julia.Scaffold(outputDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
diff, _, _, err := utils.Diff(outputDir, "./assets")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if diff {
|
||||
t.Fatalf("%s is not equal with %s", outputDir, "./assets")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("BundleSingleFunc", func(t *testing.T) {
|
||||
fd, err := ioutil.TempFile("", "fx_func_*.julia")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(fd.Name())
|
||||
|
||||
content := `struct Input
|
||||
a::Number
|
||||
b::Number
|
||||
end
|
||||
|
||||
fx = function(input::Input)
|
||||
return input.a + input.b
|
||||
end
|
||||
`
|
||||
err = ioutil.WriteFile(fd.Name(), []byte(content), 0666)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
outputDir, err := ioutil.TempDir("", "fx_julia")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(outputDir)
|
||||
|
||||
julia := New()
|
||||
if err := julia.Bundle(outputDir, fd.Name()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
diff, pre, cur, err := utils.Diff("./assets", outputDir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !diff {
|
||||
t.Fatalf("handle function should be changed")
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(cur, []byte(content)) {
|
||||
t.Fatalf("it should be %s but got %s", content, cur)
|
||||
}
|
||||
|
||||
preHandleFunc, err := ioutil.ReadFile("./assets/fx.jl")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(pre, preHandleFunc) {
|
||||
{
|
||||
}
|
||||
t.Fatalf("it should get %s but got %s", preHandleFunc, pre)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("BundleFuncAndDeps", func(t *testing.T) {
|
||||
fd, err := ioutil.TempFile("", "fx_func_*.js")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(fd.Name())
|
||||
|
||||
content, err := ioutil.ReadFile("./assets/fx.jl")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = ioutil.WriteFile(fd.Name(), content, 0666)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
addFunc := `
|
||||
module.exports = (a, b) => a+b
|
||||
`
|
||||
addFd, err := ioutil.TempFile("", "fx_add_func_*.js")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile(addFd.Name(), []byte(addFunc), 0644)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
outputDir, err := ioutil.TempDir("", "fx_julia")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(outputDir)
|
||||
|
||||
julia := New()
|
||||
if err := julia.Bundle(outputDir, fd.Name(), addFd.Name()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
diff, pre, cur, err := utils.Diff("./assets", outputDir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !diff {
|
||||
t.Fatalf("handle functino should be changed")
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(cur, []byte(addFunc)) {
|
||||
t.Fatalf("it should be %s but got %s", content, cur)
|
||||
}
|
||||
if pre != nil {
|
||||
t.Fatal(pre)
|
||||
}
|
||||
})
|
||||
}
|
||||
37
bundler/julia/packrd/packed-packr.go
Normal file
37
bundler/julia/packrd/packed-packr.go
Normal file
@@ -0,0 +1,37 @@
|
||||
// +build !skippackr
|
||||
// Code generated by github.com/gobuffalo/packr/v2. DO NOT EDIT.
|
||||
|
||||
// You can use the "packr2 clean" command to clean up this,
|
||||
// and any other packr generated files.
|
||||
package packrd
|
||||
|
||||
import (
|
||||
"github.com/gobuffalo/packr/v2"
|
||||
"github.com/gobuffalo/packr/v2/file/resolver"
|
||||
)
|
||||
|
||||
var _ = func() error {
|
||||
const gk = "9e78c349d756cf23066df37690663267"
|
||||
g := packr.New(gk, "")
|
||||
hgr, err := resolver.NewHexGzip(map[string]string{
|
||||
"17bc9961cffa014e8d7ac818d8a3ec14": "1f8b08000000000000ff4c8fc16ac3300c86ef7e8a9f9e6c2826b09d0a39edb2edb08d959dc61866515a975449243bf4f18793a69bc020ebe3d36f678d7cc0634ac39e642231f13cf492f0bc7f7d59fb0f3e07d163e88c89fcd3e586eca6bdf853b771c6a84ca8b1c8d6a1e92134ee76ef3466d2b48590969b0e3d2b1900882d3ae2433a5aa1d137210587ba4635c35242ea3585941535eeab055077d54b0d41949aefe2a29edfeadfca48fc4ceca98f6c3f1f8e41ece4d0f6820991b1e67d39775b75dd71fba3cf6b679f78c869fb3fec4fd324910fb6bdd879be00e2c6946324b35599b6b8abaaca99df000000ffffa9ee463365010000",
|
||||
"516dab4c6ff105b6210ce90d2afa826f": "1f8b08000000000000ff2a2e292a4d2e51f0cc2b282de15250505048b4b2f22bcd4d4a2d02f392e0bcd4bc142eaeb40a055b85b4d2bce492ccfc3c8d4c90262b2bb05e4db0f2a2d492d2a23c05b0845ea28236949504d60d080000ffff662990f46b000000",
|
||||
"b4a52bf8c0e242a9573eebd6bd5881fd": "1f8b08000000000000ff4ccd31cbc2301080e1fd7ec54dddd206bee103d75ab7da5211743c9b5053cf723417417fbd98415c9fe17d7743d7e29c38d0c696ff0075d79fb1c48a440086e31e49d44c5e318923f558145f094b546246f3c4691c33270d1c312daf2078a79b470e97abaa18a135fad538ffc8d13ccc93ca7989e5cc0075bbfd7512f97073eabb43837fd65a78070000ffff383c868aac000000",
|
||||
"c85770f1f90561b2df722b26d344976a": "1f8b08000000000000ff14c9310ac3300c46e1593a85f0642fcd093a06da2d0df4002ebf5c4c832de4f4fec1c35bded74d5b0c4b365bf6f5f57eee6b48822e85096a43eee29a71d4a62396c454ba0ba43699ca44dbef7bcb404462d2069ecdf7f9d703313cced3b6ec433d24be020000ffff9afc26056c000000",
|
||||
"ccfa88310f9e4f3419a50e352a228292": "1f8b08000000000000fff228292908482c2a4e2de2023183538bca528bb8bc82fdfdb842f372138b8a33127300010000ffffd7a8f0a324000000",
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
g.DefaultResolver = hgr
|
||||
|
||||
func() {
|
||||
b := packr.New("julia", "./assets")
|
||||
b.SetResolver("Dockerfile", packr.Pointer{ForwardBox: gk, ForwardPath: "b4a52bf8c0e242a9573eebd6bd5881fd"})
|
||||
b.SetResolver("REQUIRE", packr.Pointer{ForwardBox: gk, ForwardPath: "ccfa88310f9e4f3419a50e352a228292"})
|
||||
b.SetResolver("app.jl", packr.Pointer{ForwardBox: gk, ForwardPath: "17bc9961cffa014e8d7ac818d8a3ec14"})
|
||||
b.SetResolver("deps.jl", packr.Pointer{ForwardBox: gk, ForwardPath: "c85770f1f90561b2df722b26d344976a"})
|
||||
b.SetResolver("fx.jl", packr.Pointer{ForwardBox: gk, ForwardPath: "516dab4c6ff105b6210ce90d2afa826f"})
|
||||
}()
|
||||
return nil
|
||||
}()
|
||||
13
bundler/node/assets/app.js
Normal file
13
bundler/node/assets/app.js
Normal file
@@ -0,0 +1,13 @@
|
||||
const Koa = require('koa');
|
||||
const bodyParser = require('koa-bodyparser');
|
||||
const cors = require('@koa/cors');
|
||||
const fx = require('./fx');
|
||||
|
||||
const app = new Koa();
|
||||
app.use(cors({
|
||||
origin: '*',
|
||||
}));
|
||||
app.use(bodyParser());
|
||||
app.use(fx);
|
||||
|
||||
app.listen(3000);
|
||||
8
bundler/node/node-packr.go
Normal file
8
bundler/node/node-packr.go
Normal file
@@ -0,0 +1,8 @@
|
||||
// +build !skippackr
|
||||
// Code generated by github.com/gobuffalo/packr/v2. DO NOT EDIT.
|
||||
|
||||
// You can use the "packr clean" command to clean up this,
|
||||
// and any other packr generated files.
|
||||
package node
|
||||
|
||||
import _ "github.com/metrue/fx/bundler/node/packrd"
|
||||
34
bundler/node/node.go
Normal file
34
bundler/node/node.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package node
|
||||
|
||||
import (
|
||||
"github.com/gobuffalo/packr/v2"
|
||||
"github.com/metrue/fx/bundler"
|
||||
)
|
||||
|
||||
const language = "node"
|
||||
|
||||
// Node defines node bundler
|
||||
type Node struct {
|
||||
assets *packr.Box
|
||||
}
|
||||
|
||||
// New a koa bundler
|
||||
func New() *Node {
|
||||
return &Node{
|
||||
assets: packr.New("node", "./assets"),
|
||||
}
|
||||
}
|
||||
|
||||
// Scaffold a koa app
|
||||
func (k *Node) Scaffold(output string) error {
|
||||
return bundler.Restore(k.assets, output)
|
||||
}
|
||||
|
||||
// Bundle a function into a koa project
|
||||
func (k *Node) Bundle(output string, fn string, deps ...string) error {
|
||||
return bundler.Bundle(k.assets, output, language, fn, deps...)
|
||||
}
|
||||
|
||||
var (
|
||||
_ bundler.Bundler = &Node{}
|
||||
)
|
||||
143
bundler/node/node_test.go
Normal file
143
bundler/node/node_test.go
Normal file
@@ -0,0 +1,143 @@
|
||||
package node
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/metrue/fx/utils"
|
||||
)
|
||||
|
||||
func TestNodeBundler(t *testing.T) {
|
||||
t.Run("Scaffold", func(t *testing.T) {
|
||||
outputDir, err := ioutil.TempDir("", "fx_koa")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(outputDir)
|
||||
|
||||
koa := New()
|
||||
if err := koa.Scaffold(outputDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
diff, _, _, err := utils.Diff(outputDir, "./assets")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if diff {
|
||||
t.Fatalf("%s is not equal with %s", outputDir, "./assets")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("BundleSingleFunc", func(t *testing.T) {
|
||||
fd, err := ioutil.TempFile("", "fx_func_*.js")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(fd.Name())
|
||||
|
||||
content := `
|
||||
module.exports = (ctx) => {
|
||||
ctx.body = 'hello fx'
|
||||
}`
|
||||
err = ioutil.WriteFile(fd.Name(), []byte(content), 0666)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
outputDir, err := ioutil.TempDir("", "fx_koa")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(outputDir)
|
||||
|
||||
koa := New()
|
||||
if err := koa.Bundle(outputDir, fd.Name()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
diff, pre, cur, err := utils.Diff("./assets", outputDir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !diff {
|
||||
t.Fatalf("handle functino should be changed")
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(cur, []byte(content)) {
|
||||
t.Fatalf("it should be %s but got %s", content, cur)
|
||||
}
|
||||
|
||||
preHandleFunc, err := ioutil.ReadFile("./assets/fx.js")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(pre, preHandleFunc) {
|
||||
{
|
||||
}
|
||||
t.Fatalf("it should get %s but got %s", preHandleFunc, pre)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("BundleFuncAndDeps", func(t *testing.T) {
|
||||
fd, err := ioutil.TempFile("", "fx_func_*.js")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(fd.Name())
|
||||
|
||||
content, err := ioutil.ReadFile("./assets/fx.js")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = ioutil.WriteFile(fd.Name(), content, 0666)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
addFunc := `
|
||||
module.exports = (a, b) => a+b
|
||||
`
|
||||
addFd, err := ioutil.TempFile("", "fx_add_func_*.js")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile(addFd.Name(), []byte(addFunc), 0644)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
outputDir, err := ioutil.TempDir("", "fx_koa")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(outputDir)
|
||||
|
||||
koa := New()
|
||||
if err := koa.Bundle(outputDir, fd.Name(), addFd.Name()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
diff, pre, cur, err := utils.Diff("./assets", outputDir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !diff {
|
||||
t.Fatalf("handle functino should be changed")
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(cur, []byte(addFunc)) {
|
||||
t.Fatalf("it should be %s but got %s", content, cur)
|
||||
}
|
||||
if pre != nil {
|
||||
t.Fatal(pre)
|
||||
}
|
||||
})
|
||||
}
|
||||
33
bundler/node/packrd/packed-packr.go
Normal file
33
bundler/node/packrd/packed-packr.go
Normal file
@@ -0,0 +1,33 @@
|
||||
// +build !skippackr
|
||||
// Code generated by github.com/gobuffalo/packr/v2. DO NOT EDIT.
|
||||
|
||||
// You can use the "packr2 clean" command to clean up this,
|
||||
// and any other packr generated files.
|
||||
package packrd
|
||||
|
||||
import (
|
||||
"github.com/gobuffalo/packr/v2"
|
||||
"github.com/gobuffalo/packr/v2/file/resolver"
|
||||
)
|
||||
|
||||
var _ = func() error {
|
||||
const gk = "099a799bb033d71a85f319790ea9425b"
|
||||
g := packr.New(gk, "")
|
||||
hgr, err := resolver.NewHexGzip(map[string]string{
|
||||
"06d0f22f31cc0848f733e8dcaa11962c": "1f8b08000000000000ffcacd4f29cd49d54bad28c82f2a2956b055d0482ea9d054b0b553a8e65250482ea9d04bca4fa954b05550cf48cdc9c95728cf2fca4951e7aae502040000ffff4a686e4639000000",
|
||||
"2c75a41a4116ea4ba707c129527fd11c": "1f8b08000000000000ff5c8ec10ac2301044eff98abd25116d0bde2c05ef5efc8558b7122ad9b869b122febbac0a091e67de63989e429ae0400e3a60bccd9ed1e8919cb6adfab2139d1f47c709f94fd908891f92ed9e3895de7e24574b9995612985aa1e16613fe862840e02dee593b1ad7231567342231be6a90088fdc5871de8955eab972d94fcd494f5b0c8bca4ab4f1306b36d9ac6b6ea1d0000ffffc9e8e169fb000000",
|
||||
"98bb98a34bd158dd2fdd41ca90f4e64d": "1f8b08000000000000ff720bf2f755c84d2d292a4dd54fabd0cdcb4f49d54d4a2c4ee5e272f60f8854d053d0e3728d08f00f765530363030e072f67551885602a952d251504a2c28d0cb2a568ae502040000ffff6c95391846000000",
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
g.DefaultResolver = hgr
|
||||
|
||||
func() {
|
||||
b := packr.New("node", "./assets")
|
||||
b.SetResolver("Dockerfile", packr.Pointer{ForwardBox: gk, ForwardPath: "98bb98a34bd158dd2fdd41ca90f4e64d"})
|
||||
b.SetResolver("app.js", packr.Pointer{ForwardBox: gk, ForwardPath: "2c75a41a4116ea4ba707c129527fd11c"})
|
||||
b.SetResolver("fx.js", packr.Pointer{ForwardBox: gk, ForwardPath: "06d0f22f31cc0848f733e8dcaa11962c"})
|
||||
}()
|
||||
return nil
|
||||
}()
|
||||
6
bundler/perl/assets/Dockerfile
Normal file
6
bundler/perl/assets/Dockerfile
Normal file
@@ -0,0 +1,6 @@
|
||||
FROM metrue/fx-perl-base
|
||||
|
||||
ADD . .
|
||||
|
||||
EXPOSE 3000
|
||||
CMD ["perl", "app.pl", "daemon"]
|
||||
17
bundler/perl/assets/app.pl
Normal file
17
bundler/perl/assets/app.pl
Normal file
@@ -0,0 +1,17 @@
|
||||
use Mojolicious::Lite;
|
||||
|
||||
require "./fx.pl";
|
||||
|
||||
get '/' => sub {
|
||||
my $ctx = shift;
|
||||
my $res = fx($ctx);
|
||||
$ctx->render(json => $res);
|
||||
};
|
||||
|
||||
post '/' => sub {
|
||||
my $ctx = shift;
|
||||
my $res = fx($ctx);
|
||||
$ctx->render(json => $res);
|
||||
};
|
||||
|
||||
app->start;
|
||||
6
bundler/perl/assets/fx.pl
Normal file
6
bundler/perl/assets/fx.pl
Normal file
@@ -0,0 +1,6 @@
|
||||
sub fx {
|
||||
my $ctx = shift;
|
||||
return 'hello fx'
|
||||
}
|
||||
|
||||
1;
|
||||
33
bundler/perl/packrd/packed-packr.go
Normal file
33
bundler/perl/packrd/packed-packr.go
Normal file
@@ -0,0 +1,33 @@
|
||||
// +build !skippackr
|
||||
// Code generated by github.com/gobuffalo/packr/v2. DO NOT EDIT.
|
||||
|
||||
// You can use the "packr2 clean" command to clean up this,
|
||||
// and any other packr generated files.
|
||||
package packrd
|
||||
|
||||
import (
|
||||
"github.com/gobuffalo/packr/v2"
|
||||
"github.com/gobuffalo/packr/v2/file/resolver"
|
||||
)
|
||||
|
||||
var _ = func() error {
|
||||
const gk = "77d2d2753a1e41552ea722fb94d9bca6"
|
||||
g := packr.New(gk, "")
|
||||
hgr, err := resolver.NewHexGzip(map[string]string{
|
||||
"2e6c11f0e86d189a5f33d2eda0bc7406": "1f8b08000000000000ff720bf2f755c84d2d292a4dd54fabd02d482dcad14d4a2c4ee5e272747151d053d0e3e2728d08f00f765530363030e072f67551885602a952d251504a2c28d02b00b352125373f3f39462b900010000ffff0583b6ed50000000",
|
||||
"b3a2e75d2833aa5c271d28326dee512c": "1f8b08000000000000ff2a2e4d5248ab50a8e65250c8ad5450492ea950b05528cec84c2bb1e65250284a2d292dca5350cf48cdc9c95748ab50e7aae5e232b4e602040000ffffd1aa6a1336000000",
|
||||
"cd2f2d8e12b35147ad2b9cb35d65d3ab": "1f8b08000000000000ffb48c41aa83401044f77d8a423ea80b75efa027f83984316d32629c49770f1842ee1e067285ec8a578f9794710a6bd8fcec43d2befff7c68e48f891bc308ab65b8e366e8523bab2a1ec4a0c23349df122e0fec4df6c0706e8cd2fe6be4858316039aa7cd699e6d18cc2fb85a55a35ecb992bddad1db11c5a0bf8b4f3136a3da24e6e8130000ffff6c5481b5f1000000",
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
g.DefaultResolver = hgr
|
||||
|
||||
func() {
|
||||
b := packr.New("perl", "./assets")
|
||||
b.SetResolver("Dockerfile", packr.Pointer{ForwardBox: gk, ForwardPath: "2e6c11f0e86d189a5f33d2eda0bc7406"})
|
||||
b.SetResolver("app.pl", packr.Pointer{ForwardBox: gk, ForwardPath: "cd2f2d8e12b35147ad2b9cb35d65d3ab"})
|
||||
b.SetResolver("fx.pl", packr.Pointer{ForwardBox: gk, ForwardPath: "b3a2e75d2833aa5c271d28326dee512c"})
|
||||
}()
|
||||
return nil
|
||||
}()
|
||||
8
bundler/perl/perl-packr.go
Normal file
8
bundler/perl/perl-packr.go
Normal file
@@ -0,0 +1,8 @@
|
||||
// +build !skippackr
|
||||
// Code generated by github.com/gobuffalo/packr/v2. DO NOT EDIT.
|
||||
|
||||
// You can use the "packr clean" command to clean up this,
|
||||
// and any other packr generated files.
|
||||
package perl
|
||||
|
||||
import _ "github.com/metrue/fx/bundler/perl/packrd"
|
||||
32
bundler/perl/perl.go
Normal file
32
bundler/perl/perl.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package perl
|
||||
|
||||
import (
|
||||
"github.com/gobuffalo/packr/v2"
|
||||
"github.com/metrue/fx/bundler"
|
||||
)
|
||||
|
||||
// Julia defines javascript bundler
|
||||
type Julia struct {
|
||||
assets *packr.Box
|
||||
}
|
||||
|
||||
// New a koa bundler
|
||||
func New() *Julia {
|
||||
return &Julia{
|
||||
assets: packr.New("perl", "./assets"),
|
||||
}
|
||||
}
|
||||
|
||||
// Scaffold a koa app
|
||||
func (k *Julia) Scaffold(output string) error {
|
||||
return bundler.Restore(k.assets, output)
|
||||
}
|
||||
|
||||
// Bundle a function into a koa project
|
||||
func (k *Julia) Bundle(output string, fn string, deps ...string) error {
|
||||
return bundler.Bundle(k.assets, output, "perl", fn, deps...)
|
||||
}
|
||||
|
||||
var (
|
||||
_ bundler.Bundler = &Julia{}
|
||||
)
|
||||
148
bundler/perl/perl_test.go
Normal file
148
bundler/perl/perl_test.go
Normal file
@@ -0,0 +1,148 @@
|
||||
package perl
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/metrue/fx/utils"
|
||||
)
|
||||
|
||||
func TestJuliaBundler(t *testing.T) {
|
||||
t.Run("Scaffold", func(t *testing.T) {
|
||||
outputDir, err := ioutil.TempDir("", "fx_julia")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(outputDir)
|
||||
|
||||
julia := New()
|
||||
if err := julia.Scaffold(outputDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
diff, _, _, err := utils.Diff(outputDir, "./assets")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if diff {
|
||||
t.Fatalf("%s is not equal with %s", outputDir, "./assets")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("BundleSingleFunc", func(t *testing.T) {
|
||||
fd, err := ioutil.TempFile("", "fx_func_*.julia")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(fd.Name())
|
||||
|
||||
content := `struct Input
|
||||
a::Number
|
||||
b::Number
|
||||
end
|
||||
|
||||
fx = function(input::Input)
|
||||
return input.a + input.b
|
||||
end
|
||||
`
|
||||
err = ioutil.WriteFile(fd.Name(), []byte(content), 0666)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
outputDir, err := ioutil.TempDir("", "fx_julia")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(outputDir)
|
||||
|
||||
julia := New()
|
||||
if err := julia.Bundle(outputDir, fd.Name()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
diff, pre, cur, err := utils.Diff("./assets", outputDir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !diff {
|
||||
t.Fatalf("handle function should be changed")
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(cur, []byte(content)) {
|
||||
t.Fatalf("it should be %s but got %s", content, cur)
|
||||
}
|
||||
|
||||
preHandleFunc, err := ioutil.ReadFile("./assets/fx.pl")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(pre, preHandleFunc) {
|
||||
{
|
||||
}
|
||||
t.Fatalf("it should get %s but got %s", preHandleFunc, pre)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("BundleFuncAndDeps", func(t *testing.T) {
|
||||
fd, err := ioutil.TempFile("", "fx_func_*.js")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(fd.Name())
|
||||
|
||||
content, err := ioutil.ReadFile("./assets/fx.pl")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = ioutil.WriteFile(fd.Name(), content, 0666)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
addFunc := `
|
||||
module.exports = (a, b) => a+b
|
||||
`
|
||||
addFd, err := ioutil.TempFile("", "fx_add_func_*.js")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile(addFd.Name(), []byte(addFunc), 0644)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
outputDir, err := ioutil.TempDir("", "fx_julia")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(outputDir)
|
||||
|
||||
julia := New()
|
||||
if err := julia.Bundle(outputDir, fd.Name(), addFd.Name()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
diff, pre, cur, err := utils.Diff("./assets", outputDir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !diff {
|
||||
t.Fatalf("handle functino should be changed")
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(cur, []byte(addFunc)) {
|
||||
t.Fatalf("it should be %s but got %s", content, cur)
|
||||
}
|
||||
if pre != nil {
|
||||
t.Fatal(pre)
|
||||
}
|
||||
})
|
||||
}
|
||||
33
bundler/python/packrd/packed-packr.go
Normal file
33
bundler/python/packrd/packed-packr.go
Normal file
@@ -0,0 +1,33 @@
|
||||
// +build !skippackr
|
||||
// Code generated by github.com/gobuffalo/packr/v2. DO NOT EDIT.
|
||||
|
||||
// You can use the "packr2 clean" command to clean up this,
|
||||
// and any other packr generated files.
|
||||
package packrd
|
||||
|
||||
import (
|
||||
"github.com/gobuffalo/packr/v2"
|
||||
"github.com/gobuffalo/packr/v2/file/resolver"
|
||||
)
|
||||
|
||||
var _ = func() error {
|
||||
const gk = "37124a38bea92e4ec995016569c93ec9"
|
||||
g := packr.New(gk, "")
|
||||
hgr, err := resolver.NewHexGzip(map[string]string{
|
||||
"0541aa67f1217ee20e491552a545a7ca": "1f8b08000000000000ff4a494d5348abd0284a2d2c4d2d2ed1b4e252505050284a2d292dca5350ca48cdc9c95728cf2fca4951e202040000ffffc6a7b4282a000000",
|
||||
"46416dc28552c117e810b36196e043ca": "1f8b08000000000000ff720bf2f755c84d2d292a4dd54fabd02da82cc9c8cfd34d4a2c4ee5e272f60f8854d053d0e3728d08f00f765530363030e072f6755148cb492cce56282acd53d0cd5030d0034305dd02880240000000ffff21aa3b6b52000000",
|
||||
"65d09a53a70631fa1bbd7c5e1906d910": "1f8b08000000000000ff34cdbd0ac23014c5f1fd3ec5d99240d05d28b8a8a382dd4442a037b4da7c98a410df5ea43a9edf19fe2e470fd730f91473856bb4ca6ccbf38fc7efd0c8fc5ab8548d478961726fb229a15b5f694cb09e8d51447b9bd226c7a5b2145ba1e1b98e7128dd4d5cced75e6888d3a1177745033b8c360c334bb52300c85c971ce09afcc5147d020000ffff9da83eeaa1000000",
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
g.DefaultResolver = hgr
|
||||
|
||||
func() {
|
||||
b := packr.New("python", "./assets")
|
||||
b.SetResolver("Dockerfile", packr.Pointer{ForwardBox: gk, ForwardPath: "46416dc28552c117e810b36196e043ca"})
|
||||
b.SetResolver("app.py", packr.Pointer{ForwardBox: gk, ForwardPath: "65d09a53a70631fa1bbd7c5e1906d910"})
|
||||
b.SetResolver("fx.py", packr.Pointer{ForwardBox: gk, ForwardPath: "0541aa67f1217ee20e491552a545a7ca"})
|
||||
}()
|
||||
return nil
|
||||
}()
|
||||
8
bundler/python/python-packr.go
Normal file
8
bundler/python/python-packr.go
Normal file
@@ -0,0 +1,8 @@
|
||||
// +build !skippackr
|
||||
// Code generated by github.com/gobuffalo/packr/v2. DO NOT EDIT.
|
||||
|
||||
// You can use the "packr clean" command to clean up this,
|
||||
// and any other packr generated files.
|
||||
package python
|
||||
|
||||
import _ "github.com/metrue/fx/bundler/python/packrd"
|
||||
32
bundler/python/python.go
Normal file
32
bundler/python/python.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package python
|
||||
|
||||
import (
|
||||
"github.com/gobuffalo/packr/v2"
|
||||
"github.com/metrue/fx/bundler"
|
||||
)
|
||||
|
||||
// Julia defines javascript bundler
|
||||
type Julia struct {
|
||||
assets *packr.Box
|
||||
}
|
||||
|
||||
// New a koa bundler
|
||||
func New() *Julia {
|
||||
return &Julia{
|
||||
assets: packr.New("python", "./assets"),
|
||||
}
|
||||
}
|
||||
|
||||
// Scaffold a koa app
|
||||
func (k *Julia) Scaffold(output string) error {
|
||||
return bundler.Restore(k.assets, output)
|
||||
}
|
||||
|
||||
// Bundle a function into a koa project
|
||||
func (k *Julia) Bundle(output string, fn string, deps ...string) error {
|
||||
return bundler.Bundle(k.assets, output, "python", fn, deps...)
|
||||
}
|
||||
|
||||
var (
|
||||
_ bundler.Bundler = &Julia{}
|
||||
)
|
||||
148
bundler/python/python_test.go
Normal file
148
bundler/python/python_test.go
Normal file
@@ -0,0 +1,148 @@
|
||||
package python
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/metrue/fx/utils"
|
||||
)
|
||||
|
||||
func TestJuliaBundler(t *testing.T) {
|
||||
t.Run("Scaffold", func(t *testing.T) {
|
||||
outputDir, err := ioutil.TempDir("", "fx_julia")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(outputDir)
|
||||
|
||||
julia := New()
|
||||
if err := julia.Scaffold(outputDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
diff, _, _, err := utils.Diff(outputDir, "./assets")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if diff {
|
||||
t.Fatalf("%s is not equal with %s", outputDir, "./assets")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("BundleSingleFunc", func(t *testing.T) {
|
||||
fd, err := ioutil.TempFile("", "fx_func_*.julia")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(fd.Name())
|
||||
|
||||
content := `struct Input
|
||||
a::Number
|
||||
b::Number
|
||||
end
|
||||
|
||||
fx = function(input::Input)
|
||||
return input.a + input.b
|
||||
end
|
||||
`
|
||||
err = ioutil.WriteFile(fd.Name(), []byte(content), 0666)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
outputDir, err := ioutil.TempDir("", "fx_julia")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(outputDir)
|
||||
|
||||
julia := New()
|
||||
if err := julia.Bundle(outputDir, fd.Name()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
diff, pre, cur, err := utils.Diff("./assets", outputDir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !diff {
|
||||
t.Fatalf("handle function should be changed")
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(cur, []byte(content)) {
|
||||
t.Fatalf("it should be %s but got %s", content, cur)
|
||||
}
|
||||
|
||||
preHandleFunc, err := ioutil.ReadFile("./assets/fx.py")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(pre, preHandleFunc) {
|
||||
{
|
||||
}
|
||||
t.Fatalf("it should get %s but got %s", preHandleFunc, pre)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("BundleFuncAndDeps", func(t *testing.T) {
|
||||
fd, err := ioutil.TempFile("", "fx_func_*.js")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(fd.Name())
|
||||
|
||||
content, err := ioutil.ReadFile("./assets/fx.py")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = ioutil.WriteFile(fd.Name(), content, 0666)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
addFunc := `
|
||||
module.exports = (a, b) => a+b
|
||||
`
|
||||
addFd, err := ioutil.TempFile("", "fx_add_func_*.js")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile(addFd.Name(), []byte(addFunc), 0644)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
outputDir, err := ioutil.TempDir("", "fx_julia")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(outputDir)
|
||||
|
||||
julia := New()
|
||||
if err := julia.Bundle(outputDir, fd.Name(), addFd.Name()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
diff, pre, cur, err := utils.Diff("./assets", outputDir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !diff {
|
||||
t.Fatalf("handle functino should be changed")
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(cur, []byte(addFunc)) {
|
||||
t.Fatalf("it should be %s but got %s", content, cur)
|
||||
}
|
||||
if pre != nil {
|
||||
t.Fatal(pre)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -1,6 +1,4 @@
|
||||
FROM ruby:latest
|
||||
|
||||
RUN gem install sinatra
|
||||
FROM metrue/fx-ruby-base
|
||||
|
||||
COPY . .
|
||||
EXPOSE 3000
|
||||
33
bundler/ruby/packrd/packed-packr.go
Normal file
33
bundler/ruby/packrd/packed-packr.go
Normal file
@@ -0,0 +1,33 @@
|
||||
// +build !skippackr
|
||||
// Code generated by github.com/gobuffalo/packr/v2. DO NOT EDIT.
|
||||
|
||||
// You can use the "packr2 clean" command to clean up this,
|
||||
// and any other packr generated files.
|
||||
package packrd
|
||||
|
||||
import (
|
||||
"github.com/gobuffalo/packr/v2"
|
||||
"github.com/gobuffalo/packr/v2/file/resolver"
|
||||
)
|
||||
|
||||
var _ = func() error {
|
||||
const gk = "5957c0f80ccc65e6ad8f2c329312596e"
|
||||
g := packr.New(gk, "")
|
||||
hgr, err := resolver.NewHexGzip(map[string]string{
|
||||
"5d0faa4e62eb53572bfc7410d0f064f9": "1f8b08000000000000ff720bf2f755c84d2d292a4dd54fabd02d2a4daad44d4a2c4ee5e272f60f8854d053d0e3728d08f00f765530363030e072f6755100a951482c28d02b4a52d02d008b2be8e62b18e8812117200000ffff79eb4a3952000000",
|
||||
"93130b893788876c223c2a0f3de05793": "1f8b08000000000000ffccce41aac23010c6f1fd9ce2dbcda6bc577057a8579168a75a91a4ce4ca520de5ddad4780537e19ffc207c2af7695001db10836b602a2f574b91e9733fa8dc820f0f01f7f39f1e99c8c4d18c49bdc2aeae6ba2319983ff195d220038f98c16cfb58166f949ccd1eeb165f5251b5334c996bba079f0c916ca55e022a1135d65cb4caff5ece7650049ec88cef29bc3de010000ffff22ee4db77f010000",
|
||||
"973570cec900912c2cdd5c5531be70ac": "1f8b08000000000000ff3ccd410ac2301085e1fd9ce2d16c14c40308f122e2a276a674111acd4c3022de5d6262773fef8319470e931578bcc9013825796451833fa3e7e10f7a8fab4a93d69dd446cb5aa1559f171959d26fef59e1432c33e6b29bacec09f5fb653b7e3dde22bfe0312c1242c433a6c003c9caf40d0000ffff99b9e753aa000000",
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
g.DefaultResolver = hgr
|
||||
|
||||
func() {
|
||||
b := packr.New("ruby", "./assets")
|
||||
b.SetResolver("Dockerfile", packr.Pointer{ForwardBox: gk, ForwardPath: "5d0faa4e62eb53572bfc7410d0f064f9"})
|
||||
b.SetResolver("app.rb", packr.Pointer{ForwardBox: gk, ForwardPath: "93130b893788876c223c2a0f3de05793"})
|
||||
b.SetResolver("fx.rb", packr.Pointer{ForwardBox: gk, ForwardPath: "973570cec900912c2cdd5c5531be70ac"})
|
||||
}()
|
||||
return nil
|
||||
}()
|
||||
8
bundler/ruby/ruby-packr.go
Normal file
8
bundler/ruby/ruby-packr.go
Normal file
@@ -0,0 +1,8 @@
|
||||
// +build !skippackr
|
||||
// Code generated by github.com/gobuffalo/packr/v2. DO NOT EDIT.
|
||||
|
||||
// You can use the "packr clean" command to clean up this,
|
||||
// and any other packr generated files.
|
||||
package ruby
|
||||
|
||||
import _ "github.com/metrue/fx/bundler/ruby/packrd"
|
||||
32
bundler/ruby/ruby.go
Normal file
32
bundler/ruby/ruby.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package ruby
|
||||
|
||||
import (
|
||||
"github.com/gobuffalo/packr/v2"
|
||||
"github.com/metrue/fx/bundler"
|
||||
)
|
||||
|
||||
// Julia defines javascript bundler
|
||||
type Julia struct {
|
||||
assets *packr.Box
|
||||
}
|
||||
|
||||
// New a koa bundler
|
||||
func New() *Julia {
|
||||
return &Julia{
|
||||
assets: packr.New("ruby", "./assets"),
|
||||
}
|
||||
}
|
||||
|
||||
// Scaffold a koa app
|
||||
func (k *Julia) Scaffold(output string) error {
|
||||
return bundler.Restore(k.assets, output)
|
||||
}
|
||||
|
||||
// Bundle a function into a koa project
|
||||
func (k *Julia) Bundle(output string, fn string, deps ...string) error {
|
||||
return bundler.Bundle(k.assets, output, "ruby", fn, deps...)
|
||||
}
|
||||
|
||||
var (
|
||||
_ bundler.Bundler = &Julia{}
|
||||
)
|
||||
148
bundler/ruby/ruby_test.go
Normal file
148
bundler/ruby/ruby_test.go
Normal file
@@ -0,0 +1,148 @@
|
||||
package ruby
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/metrue/fx/utils"
|
||||
)
|
||||
|
||||
func TestJuliaBundler(t *testing.T) {
|
||||
t.Run("Scaffold", func(t *testing.T) {
|
||||
outputDir, err := ioutil.TempDir("", "fx_julia")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(outputDir)
|
||||
|
||||
julia := New()
|
||||
if err := julia.Scaffold(outputDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
diff, _, _, err := utils.Diff(outputDir, "./assets")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if diff {
|
||||
t.Fatalf("%s is not equal with %s", outputDir, "./assets")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("BundleSingleFunc", func(t *testing.T) {
|
||||
fd, err := ioutil.TempFile("", "fx_func_*.julia")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(fd.Name())
|
||||
|
||||
content := `struct Input
|
||||
a::Number
|
||||
b::Number
|
||||
end
|
||||
|
||||
fx = function(input::Input)
|
||||
return input.a + input.b
|
||||
end
|
||||
`
|
||||
err = ioutil.WriteFile(fd.Name(), []byte(content), 0666)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
outputDir, err := ioutil.TempDir("", "fx_julia")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(outputDir)
|
||||
|
||||
julia := New()
|
||||
if err := julia.Bundle(outputDir, fd.Name()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
diff, pre, cur, err := utils.Diff("./assets", outputDir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !diff {
|
||||
t.Fatalf("handle function should be changed")
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(cur, []byte(content)) {
|
||||
t.Fatalf("it should be %s but got %s", content, cur)
|
||||
}
|
||||
|
||||
preHandleFunc, err := ioutil.ReadFile("./assets/fx.rb")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(pre, preHandleFunc) {
|
||||
{
|
||||
}
|
||||
t.Fatalf("it should get %s but got %s", preHandleFunc, pre)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("BundleFuncAndDeps", func(t *testing.T) {
|
||||
fd, err := ioutil.TempFile("", "fx_func_*.js")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(fd.Name())
|
||||
|
||||
content, err := ioutil.ReadFile("./assets/fx.rb")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = ioutil.WriteFile(fd.Name(), content, 0666)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
addFunc := `
|
||||
module.exports = (a, b) => a+b
|
||||
`
|
||||
addFd, err := ioutil.TempFile("", "fx_add_func_*.js")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile(addFd.Name(), []byte(addFunc), 0644)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
outputDir, err := ioutil.TempDir("", "fx_julia")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(outputDir)
|
||||
|
||||
julia := New()
|
||||
if err := julia.Bundle(outputDir, fd.Name(), addFd.Name()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
diff, pre, cur, err := utils.Diff("./assets", outputDir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !diff {
|
||||
t.Fatalf("handle functino should be changed")
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(cur, []byte(addFunc)) {
|
||||
t.Fatalf("it should be %s but got %s", content, cur)
|
||||
}
|
||||
if pre != nil {
|
||||
t.Fatal(pre)
|
||||
}
|
||||
})
|
||||
}
|
||||
41
bundler/rust/packrd/packed-packr.go
Normal file
41
bundler/rust/packrd/packed-packr.go
Normal file
File diff suppressed because one or more lines are too long
8
bundler/rust/rust-packr.go
Normal file
8
bundler/rust/rust-packr.go
Normal file
@@ -0,0 +1,8 @@
|
||||
// +build !skippackr
|
||||
// Code generated by github.com/gobuffalo/packr/v2. DO NOT EDIT.
|
||||
|
||||
// You can use the "packr clean" command to clean up this,
|
||||
// and any other packr generated files.
|
||||
package rust
|
||||
|
||||
import _ "github.com/metrue/fx/bundler/rust/packrd"
|
||||
32
bundler/rust/rust.go
Normal file
32
bundler/rust/rust.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package rust
|
||||
|
||||
import (
|
||||
"github.com/gobuffalo/packr/v2"
|
||||
"github.com/metrue/fx/bundler"
|
||||
)
|
||||
|
||||
// Julia defines javascript bundler
|
||||
type Julia struct {
|
||||
assets *packr.Box
|
||||
}
|
||||
|
||||
// New a koa bundler
|
||||
func New() *Julia {
|
||||
return &Julia{
|
||||
assets: packr.New("rust", "./assets"),
|
||||
}
|
||||
}
|
||||
|
||||
// Scaffold a koa app
|
||||
func (k *Julia) Scaffold(output string) error {
|
||||
return bundler.Restore(k.assets, output)
|
||||
}
|
||||
|
||||
// Bundle a function into a koa project
|
||||
func (k *Julia) Bundle(output string, fn string, deps ...string) error {
|
||||
return bundler.Bundle(k.assets, output, "rust", fn, deps...)
|
||||
}
|
||||
|
||||
var (
|
||||
_ bundler.Bundler = &Julia{}
|
||||
)
|
||||
148
bundler/rust/rust_test.go
Normal file
148
bundler/rust/rust_test.go
Normal file
@@ -0,0 +1,148 @@
|
||||
package rust
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/metrue/fx/utils"
|
||||
)
|
||||
|
||||
func TestJuliaBundler(t *testing.T) {
|
||||
t.Run("Scaffold", func(t *testing.T) {
|
||||
outputDir, err := ioutil.TempDir("", "fx_julia")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(outputDir)
|
||||
|
||||
julia := New()
|
||||
if err := julia.Scaffold(outputDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
diff, _, _, err := utils.Diff(outputDir, "./assets")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if diff {
|
||||
t.Fatalf("%s is not equal with %s", outputDir, "./assets")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("BundleSingleFunc", func(t *testing.T) {
|
||||
fd, err := ioutil.TempFile("", "fx_func_*.julia")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(fd.Name())
|
||||
|
||||
content := `struct Input
|
||||
a::Number
|
||||
b::Number
|
||||
end
|
||||
|
||||
fx = function(input::Input)
|
||||
return input.a + input.b
|
||||
end
|
||||
`
|
||||
err = ioutil.WriteFile(fd.Name(), []byte(content), 0666)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
outputDir, err := ioutil.TempDir("", "fx_julia")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(outputDir)
|
||||
|
||||
julia := New()
|
||||
if err := julia.Bundle(outputDir, fd.Name()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
diff, pre, cur, err := utils.Diff("./assets", outputDir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !diff {
|
||||
t.Fatalf("handle function should be changed")
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(cur, []byte(content)) {
|
||||
t.Fatalf("it should be %s but got %s", content, cur)
|
||||
}
|
||||
|
||||
preHandleFunc, err := ioutil.ReadFile("./assets/src/fns/mod.rs")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(pre, preHandleFunc) {
|
||||
{
|
||||
}
|
||||
t.Fatalf("it should get %s but got %s", preHandleFunc, pre)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("BundleFuncAndDeps", func(t *testing.T) {
|
||||
fd, err := ioutil.TempFile("", "fx_func_*.js")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Remove(fd.Name())
|
||||
|
||||
content, err := ioutil.ReadFile("./assets/src/fns/mod.rs")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = ioutil.WriteFile(fd.Name(), content, 0666)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
addFunc := `
|
||||
module.exports = (a, b) => a+b
|
||||
`
|
||||
addFd, err := ioutil.TempFile("", "fx_add_func_*.js")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile(addFd.Name(), []byte(addFunc), 0644)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
outputDir, err := ioutil.TempDir("", "fx_julia")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(outputDir)
|
||||
|
||||
julia := New()
|
||||
if err := julia.Bundle(outputDir, fd.Name(), addFd.Name()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
diff, pre, cur, err := utils.Diff("./assets", outputDir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !diff {
|
||||
t.Fatalf("handle functino should be changed")
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(cur, []byte(addFunc)) {
|
||||
t.Fatalf("it should be %s but got %s", content, cur)
|
||||
}
|
||||
if pre != nil {
|
||||
t.Fatal(pre)
|
||||
}
|
||||
})
|
||||
}
|
||||
304
config/config.go
304
config/config.go
@@ -1,213 +1,207 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/user"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
dockerInfra "github.com/metrue/fx/infra/docker"
|
||||
"github.com/metrue/fx/types"
|
||||
"github.com/metrue/fx/utils"
|
||||
"github.com/mitchellh/go-homedir"
|
||||
)
|
||||
|
||||
// Configer interface
|
||||
// Configer manage fx config
|
||||
type Configer interface {
|
||||
GetMachine(name string) (Host, error)
|
||||
AddMachine(name string, host Host) error
|
||||
RemoveHost(name string) error
|
||||
ListActiveMachines() (map[string]Host, error)
|
||||
ListMachines() (map[string]Host, error)
|
||||
EnableMachine(name string) error
|
||||
DisableMachine(name string) error
|
||||
UpdateProvisionedStatus(name string, ok bool) error
|
||||
GetCurrentCloud() ([]byte, error)
|
||||
GetCurrentCloudType() (string, error)
|
||||
GetKubeConfig() (string, error)
|
||||
UseCloud(name string) error
|
||||
View() ([]byte, error)
|
||||
AddCloud(name string, meta []byte) error
|
||||
Dir() (string, error)
|
||||
}
|
||||
|
||||
// Config config of fx
|
||||
type Config struct {
|
||||
dir string
|
||||
configFile string
|
||||
container *Container
|
||||
}
|
||||
|
||||
// New create a config
|
||||
func New(dir string) *Config {
|
||||
return &Config{dir: dir}
|
||||
const defaultFxConfig = "~/.fx/config.yml"
|
||||
|
||||
// LoadDefault load default config
|
||||
func LoadDefault() (*Config, error) {
|
||||
configFile, err := homedir.Expand(defaultFxConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if os.Getenv("FX_CONFIG") != "" {
|
||||
configFile = os.Getenv("FX_CONFIG")
|
||||
}
|
||||
|
||||
if _, err := os.Stat(configFile); os.IsNotExist(err) {
|
||||
if err := utils.EnsureFile(configFile); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return load(configFile)
|
||||
}
|
||||
|
||||
// Init config
|
||||
func (c *Config) Init() error {
|
||||
if err := os.MkdirAll(c.dir, os.ModePerm); err != nil {
|
||||
func load(configFile string) (*Config, error) {
|
||||
container, err := CreateContainer(configFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config := &Config{
|
||||
configFile: configFile,
|
||||
container: container,
|
||||
}
|
||||
|
||||
if container.get("clouds") == nil {
|
||||
if err := config.writeDefaultConfig(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return config, nil
|
||||
}
|
||||
|
||||
// Load config
|
||||
func Load(configFile string) (*Config, error) {
|
||||
if configFile == "" {
|
||||
return nil, fmt.Errorf("invalid config file")
|
||||
}
|
||||
|
||||
if _, err := os.Stat(configFile); os.IsNotExist(err) {
|
||||
if err := utils.EnsureFile(configFile); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return load(configFile)
|
||||
}
|
||||
|
||||
// AddCloud add k8s cloud
|
||||
func (c *Config) AddCloud(name string, meta []byte) error {
|
||||
var cloudMeta map[string]interface{}
|
||||
if err := json.Unmarshal(meta, &cloudMeta); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ext := "yaml"
|
||||
name := "config"
|
||||
viper.SetConfigType(ext)
|
||||
viper.SetConfigName(name)
|
||||
viper.AddConfigPath(c.dir)
|
||||
cloudType, ok := cloudMeta["type"].(string)
|
||||
if !ok || cloudType == "" {
|
||||
return fmt.Errorf("unknown cloud type")
|
||||
}
|
||||
|
||||
// detect if file exists
|
||||
configFilePath := path.Join(c.dir, name+"."+ext)
|
||||
if _, err := os.Stat(configFilePath); os.IsNotExist(err) {
|
||||
fd, err := os.Create(configFilePath)
|
||||
if err != nil {
|
||||
if cloudType == types.CloudTypeK8S {
|
||||
dir := path.Dir(c.configFile)
|
||||
kubecfg := path.Join(dir, name+".kubeconfig")
|
||||
if err := utils.EnsureFile(kubecfg); err != nil {
|
||||
return err
|
||||
}
|
||||
fd.Close()
|
||||
|
||||
localhost := Host{
|
||||
Host: "localhost",
|
||||
Password: "",
|
||||
User: "",
|
||||
Enabled: true,
|
||||
Provisioned: false,
|
||||
config, ok := cloudMeta["config"].(string)
|
||||
if !ok {
|
||||
return fmt.Errorf("invalid k8s config")
|
||||
}
|
||||
if err := ioutil.WriteFile(kubecfg, []byte(config), 0666); err != nil {
|
||||
return err
|
||||
}
|
||||
viper.Set("hosts", map[string]Host{"localhost": localhost})
|
||||
return viper.WriteConfig()
|
||||
}
|
||||
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
return fmt.Errorf("fatal error config file: %s", err)
|
||||
if err := c.container.set("clouds."+name, cloudMeta); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetMachine get host by name
|
||||
func (c *Config) GetMachine(name string) (Host, error) {
|
||||
var hosts map[string]Host
|
||||
if err := viper.UnmarshalKey("hosts", &hosts); err != nil {
|
||||
return Host{}, err
|
||||
// UseCloud set cloud instance with name as current context
|
||||
func (c *Config) UseCloud(name string) error {
|
||||
if name == "" {
|
||||
return fmt.Errorf("could not use empty name")
|
||||
}
|
||||
host, ok := hosts[name]
|
||||
|
||||
if c.container.get("clouds."+name) == nil {
|
||||
return fmt.Errorf("no such cloud with name: %s", name)
|
||||
}
|
||||
return c.container.set("current_cloud", name)
|
||||
}
|
||||
|
||||
// View view current config
|
||||
func (c *Config) View() ([]byte, error) {
|
||||
return ioutil.ReadFile(c.configFile)
|
||||
}
|
||||
|
||||
// GetCurrentCloud get current using cloud's meta
|
||||
func (c *Config) GetCurrentCloud() ([]byte, error) {
|
||||
name, ok := c.container.get("current_cloud").(string)
|
||||
if !ok {
|
||||
return Host{}, fmt.Errorf("no such host %v", name)
|
||||
return nil, fmt.Errorf("no active cloud")
|
||||
}
|
||||
return host, nil
|
||||
meta := c.container.get("clouds." + name)
|
||||
if meta == nil {
|
||||
return nil, fmt.Errorf("invalid config")
|
||||
}
|
||||
return json.Marshal(meta)
|
||||
}
|
||||
|
||||
// ListActiveMachines list enabled machines
|
||||
func (c *Config) ListActiveMachines() (map[string]Host, error) {
|
||||
hosts, err := c.ListMachines()
|
||||
if err != nil {
|
||||
return map[string]Host{}, err
|
||||
// GetCurrentCloudType get current cloud type
|
||||
func (c *Config) GetCurrentCloudType() (string, error) {
|
||||
name, ok := c.container.get("current_cloud").(string)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("no active cloud")
|
||||
}
|
||||
lst := map[string]Host{}
|
||||
for name, h := range hosts {
|
||||
if h.Enabled {
|
||||
lst[name] = h
|
||||
}
|
||||
}
|
||||
return lst, nil
|
||||
return c.container.get("clouds." + name + ".type").(string), nil
|
||||
}
|
||||
|
||||
// AddMachine add host
|
||||
func (c *Config) AddMachine(name string, host Host) error {
|
||||
if !viper.IsSet("hosts") {
|
||||
viper.Set("hosts", map[string]Host{})
|
||||
// GetKubeConfig get kubeconfig
|
||||
func (c *Config) GetKubeConfig() (string, error) {
|
||||
name, ok := c.container.get("current_cloud").(string)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("no active cloud")
|
||||
}
|
||||
|
||||
hosts, err := c.ListMachines()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
hosts[name] = host
|
||||
viper.Set("hosts", hosts)
|
||||
return viper.WriteConfig()
|
||||
dir := path.Dir(c.configFile)
|
||||
kubecfg := path.Join(dir, name+".kubeconfig")
|
||||
return kubecfg, nil
|
||||
}
|
||||
|
||||
// RemoveHost remote a host
|
||||
func (c *Config) RemoveHost(name string) error {
|
||||
hosts, err := c.ListMachines()
|
||||
func (c *Config) writeDefaultConfig() error {
|
||||
me, err := user.Current()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(hosts) == 1 {
|
||||
return fmt.Errorf("only one host left now, at least one host required by fx")
|
||||
defaultCloud := &dockerInfra.Cloud{
|
||||
IP: "127.0.0.1",
|
||||
User: me.Username,
|
||||
Name: "default",
|
||||
Type: types.CloudTypeDocker,
|
||||
}
|
||||
|
||||
if _, ok := hosts[name]; ok {
|
||||
delete(hosts, name)
|
||||
|
||||
viper.Set("hosts", hosts)
|
||||
return viper.WriteConfig()
|
||||
}
|
||||
return fmt.Errorf("no such host %s", name)
|
||||
}
|
||||
|
||||
// ListMachines list hosts
|
||||
func (c *Config) ListMachines() (map[string]Host, error) {
|
||||
var hosts map[string]Host
|
||||
if err := viper.UnmarshalKey("hosts", &hosts); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return hosts, nil
|
||||
}
|
||||
|
||||
// EnableMachine enable a machine, after machine enabled, function will be deployed onto it when ever `fx up` invoked
|
||||
func (c *Config) EnableMachine(name string) error {
|
||||
host, err := c.GetMachine(name)
|
||||
meta, err := defaultCloud.Dump()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
host.Enabled = true
|
||||
|
||||
if !viper.IsSet("hosts") {
|
||||
viper.Set("hosts", map[string]Host{})
|
||||
}
|
||||
|
||||
hosts, err := c.ListMachines()
|
||||
if err != nil {
|
||||
if err := c.container.set("clouds", map[string]interface{}{}); err != nil {
|
||||
return err
|
||||
}
|
||||
hosts[name] = host
|
||||
viper.Set("hosts", hosts)
|
||||
return viper.WriteConfig()
|
||||
if err := c.AddCloud("default", meta); err != nil {
|
||||
return err
|
||||
}
|
||||
return c.UseCloud("default")
|
||||
}
|
||||
|
||||
// DisableMachine disable a machine, after machine disabled, function will not be deployed onto it
|
||||
func (c *Config) DisableMachine(name string) error {
|
||||
host, err := c.GetMachine(name)
|
||||
// Dir get directory of config
|
||||
func (c *Config) Dir() (string, error) {
|
||||
p, err := filepath.Abs(c.configFile)
|
||||
if err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
host.Enabled = false
|
||||
|
||||
if !viper.IsSet("hosts") {
|
||||
viper.Set("hosts", map[string]Host{})
|
||||
}
|
||||
|
||||
hosts, err := c.ListMachines()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
hosts[name] = host
|
||||
viper.Set("hosts", hosts)
|
||||
return viper.WriteConfig()
|
||||
return path.Dir(p), nil
|
||||
}
|
||||
|
||||
// UpdateProvisionedStatus update provisioned status
|
||||
func (c *Config) UpdateProvisionedStatus(name string, ok bool) error {
|
||||
host, err := c.GetMachine(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
host.Provisioned = ok
|
||||
|
||||
if !viper.IsSet("hosts") {
|
||||
viper.Set("hosts", map[string]Host{})
|
||||
}
|
||||
|
||||
hosts, err := c.ListMachines()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
hosts[name] = host
|
||||
viper.Set("hosts", hosts)
|
||||
return viper.WriteConfig()
|
||||
}
|
||||
|
||||
// IsMachineProvisioned check if machine provisioned
|
||||
func (c *Config) IsMachineProvisioned(name string) bool {
|
||||
host, err := c.GetMachine(name)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return host.Provisioned
|
||||
}
|
||||
var (
|
||||
_ Configer = &Config{}
|
||||
)
|
||||
|
||||
@@ -1,98 +1,131 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
k8sInfra "github.com/metrue/fx/infra/k8s"
|
||||
"github.com/metrue/fx/types"
|
||||
)
|
||||
|
||||
func TestConfig(t *testing.T) {
|
||||
configPath := "/tmp/.fx"
|
||||
configPath := "./tmp/config.yml"
|
||||
defer func() {
|
||||
if err := os.RemoveAll(configPath); err != nil {
|
||||
if err := os.RemoveAll("./tmp/config.yml"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
|
||||
c := New(configPath)
|
||||
if err := c.Init(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
hosts, err := c.ListMachines()
|
||||
// default cloud
|
||||
c, err := Load(configPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(hosts) != 1 {
|
||||
t.Fatalf("should have localhost as default machine")
|
||||
}
|
||||
|
||||
host := hosts["localhost"]
|
||||
if !reflect.DeepEqual(host, Host{Host: "localhost", Enabled: true}) {
|
||||
t.Fatalf("should get %v but got %v", Host{Host: "localhost"}, host)
|
||||
}
|
||||
|
||||
name := "remote-a"
|
||||
h := Host{
|
||||
Host: "192.168.1.1",
|
||||
User: "user-a",
|
||||
Password: "password-a",
|
||||
Enabled: false,
|
||||
}
|
||||
if err := c.AddMachine(name, h); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
hosts, err = c.ListMachines()
|
||||
defaultMeta, err := c.GetCurrentCloud()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(hosts) != 2 {
|
||||
t.Fatalf("should have %d machines now, but got %d", 2, len(hosts))
|
||||
var cloudMeta map[string]string
|
||||
if err := json.Unmarshal(defaultMeta, &cloudMeta); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cloudMeta["ip"] != "127.0.0.1" {
|
||||
t.Fatalf("should get %s but got %s", "127.0.0.1", cloudMeta["ip"])
|
||||
}
|
||||
|
||||
lst, err := c.ListActiveMachines()
|
||||
me, _ := user.Current()
|
||||
if cloudMeta["user"] != me.Username {
|
||||
t.Fatalf("should get %s but got %s", me.Username, cloudMeta["user"])
|
||||
}
|
||||
if cloudMeta["type"] != types.CloudTypeDocker {
|
||||
t.Fatalf("should get %s but got %s", types.CloudTypeDocker, cloudMeta["type"])
|
||||
}
|
||||
if cloudMeta["name"] != "default" {
|
||||
t.Fatalf("should get %s but got %s", "default", cloudMeta["name"])
|
||||
}
|
||||
|
||||
n1, err := k8sInfra.CreateNode(
|
||||
"1.1.1.1",
|
||||
"user-1",
|
||||
"k3s-master",
|
||||
"master-node",
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
n2, err := k8sInfra.CreateNode(
|
||||
"1.1.1.1",
|
||||
"user-1",
|
||||
"k3s-agent",
|
||||
"agent-node-1",
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(lst) != 1 {
|
||||
t.Fatalf("should only have %d machine enabled, but got %d", 1, len(lst))
|
||||
}
|
||||
kName := "k8s-1"
|
||||
kubeconf := "./tmp/" + kName + "config.yml"
|
||||
defer func() {
|
||||
if err := os.RemoveAll(kubeconf); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
|
||||
if err := c.EnableMachine(name); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
lst, err = c.ListActiveMachines()
|
||||
// add k8s cloud
|
||||
kCloud := k8sInfra.NewCloud(kubeconf, n1, n2)
|
||||
kMeta, err := kCloud.Dump()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(lst) != 2 {
|
||||
t.Fatalf("should only have %d machine enabled, but got %d", 2, len(lst))
|
||||
}
|
||||
|
||||
h.Enabled = true
|
||||
if !reflect.DeepEqual(lst[name], h) {
|
||||
t.Fatalf("should get %v but got %v", h, lst[name])
|
||||
}
|
||||
|
||||
if lst[name].Provisioned != false {
|
||||
t.Fatalf("should get %v but got %v", false, lst[name].Provisioned)
|
||||
}
|
||||
|
||||
if err := c.UpdateProvisionedStatus(name, true); err != nil {
|
||||
if err := c.AddCloud(kName, kMeta); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
updatedHost, err := c.GetMachine(name)
|
||||
curMeta, err := c.GetCurrentCloud()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(curMeta, defaultMeta) {
|
||||
t.Fatalf("should get %v but got %v", defaultMeta, curMeta)
|
||||
}
|
||||
|
||||
if updatedHost.Provisioned != true {
|
||||
t.Fatalf("should get %v but got %v", true, updatedHost.Provisioned)
|
||||
if err := c.UseCloud("cloud-not-existed"); err == nil {
|
||||
t.Fatalf("should get error when there is not given cloud name")
|
||||
}
|
||||
|
||||
if err := c.UseCloud(kName); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
curMeta, err = c.GetCurrentCloud()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if reflect.DeepEqual(curMeta, kMeta) {
|
||||
t.Fatalf("should get %v but got %v", kMeta, curMeta)
|
||||
}
|
||||
|
||||
body, err := c.View()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fmt.Println(string(body))
|
||||
|
||||
dir, err := c.Dir()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
here, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if dir != filepath.Join(here, "./tmp") {
|
||||
t.Fatalf("should get %s but got %s", "./tmp", dir)
|
||||
}
|
||||
}
|
||||
|
||||
73
config/container.go
Normal file
73
config/container.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/metrue/fx/utils"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// Container config container, wrap viper as a key-value store with lock
|
||||
type Container struct {
|
||||
mux sync.Mutex
|
||||
store string
|
||||
}
|
||||
|
||||
// CreateContainer new a container
|
||||
func CreateContainer(storeFile string) (*Container, error) {
|
||||
if err := utils.EnsureFile(storeFile); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
dir := filepath.Dir(storeFile)
|
||||
ext := filepath.Ext(storeFile)
|
||||
name := filepath.Base(storeFile)
|
||||
viper.AddConfigPath(dir)
|
||||
viper.SetConfigName(strings.Replace(name, ext, "", 1))
|
||||
viper.SetConfigType(strings.Replace(ext, ".", "", 1))
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Container{
|
||||
store: storeFile,
|
||||
}, nil
|
||||
|
||||
}
|
||||
|
||||
func (c *Container) set(key string, value interface{}) error {
|
||||
c.mux.Lock()
|
||||
defer c.mux.Unlock()
|
||||
|
||||
if key == "" {
|
||||
return fmt.Errorf("empty key not allowed")
|
||||
}
|
||||
|
||||
keys := strings.Split(key, ".")
|
||||
if len(keys) == 1 {
|
||||
viper.Set(key, value)
|
||||
} else {
|
||||
prePath := keys[0]
|
||||
for i := 1; i < len(keys)-2; i++ {
|
||||
prePath += "." + keys[i]
|
||||
}
|
||||
if viper.Get(prePath) == nil {
|
||||
return fmt.Errorf("%s not existed", prePath)
|
||||
}
|
||||
viper.Set(key, value)
|
||||
}
|
||||
// viper.Set(key, value)
|
||||
if err := viper.WriteConfig(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Container) get(key string) interface{} {
|
||||
c.mux.Lock()
|
||||
defer c.mux.Unlock()
|
||||
|
||||
return viper.Get(key)
|
||||
}
|
||||
84
config/container_test.go
Normal file
84
config/container_test.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestContainer(t *testing.T) {
|
||||
configPath := "./tmp/container.yml"
|
||||
defer func() {
|
||||
if err := os.RemoveAll("./tmp/container.yml"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
c, err := CreateContainer(configPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := c.set("", ""); err == nil {
|
||||
t.Fatalf("should get error when key is empty")
|
||||
}
|
||||
|
||||
if c.get("1") != nil {
|
||||
t.Fatalf("should get %v but got %v", nil, c.get("key"))
|
||||
}
|
||||
|
||||
// create
|
||||
if err := c.set("1", "1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// read
|
||||
if c.get("1").(string) != "1" {
|
||||
t.Fatalf("should get %s but got %s", "val-1", c.get("key"))
|
||||
}
|
||||
|
||||
// invaliad set
|
||||
if err := c.set("1.1", "1.1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if c.get("1.1").(string) != "1.1" {
|
||||
t.Fatalf("should get 1.1 but got %s", c.get("1.1"))
|
||||
}
|
||||
|
||||
// update
|
||||
if err := c.set("1", "11"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if c.get("1").(string) != "11" {
|
||||
t.Fatalf("should get 11 but got %s", c.get("1").(string))
|
||||
}
|
||||
|
||||
// nested set
|
||||
if err := c.set("2.2.2.2", "2222"); err == nil {
|
||||
t.Fatalf("should throw error since 2.2.2 not ready yet")
|
||||
}
|
||||
|
||||
if err := c.set("2", map[string]interface{}{
|
||||
"2": map[string]interface{}{
|
||||
"2": "2",
|
||||
},
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if c.get("2.2.2").(string) != "2" {
|
||||
t.Fatalf("should get 2 but got %s", c.get("2.2.2"))
|
||||
}
|
||||
if err := c.set("2.2.2.2", "2222"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if c.get("2.2.2.2").(string) != "2222" {
|
||||
t.Fatalf("should get 2222 but got %s", c.get("2.2.2.2"))
|
||||
}
|
||||
|
||||
if err := c.set("2.2.2.1", "1111"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if c.get("2.2.2.1").(string) != "1111" {
|
||||
t.Fatalf("should get 1111 but got %s", c.get("2.2.2.1"))
|
||||
}
|
||||
}
|
||||
14
config/env.go
Normal file
14
config/env.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
)
|
||||
|
||||
// DisableContainerAutoremove to tell if to run container with --rm
|
||||
var DisableContainerAutoremove = false
|
||||
|
||||
func init() {
|
||||
if os.Getenv("DISABLE_CONTAINER_AUTOREMOVE") == "true" {
|
||||
DisableContainerAutoremove = true
|
||||
}
|
||||
}
|
||||
17
config/env_test.go
Normal file
17
config/env_test.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var _ = func() (_ struct{}) {
|
||||
os.Setenv("DISABLE_CONTAINER_AUTOREMOVE", "true")
|
||||
return
|
||||
}()
|
||||
|
||||
func TestEnvLoad(t *testing.T) {
|
||||
if !DisableContainerAutoremove {
|
||||
t.Fatalf("should be true after set")
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package config
|
||||
|
||||
// Host host entity
|
||||
type Host struct {
|
||||
Host string
|
||||
User string
|
||||
Password string
|
||||
Enabled bool
|
||||
Provisioned bool
|
||||
}
|
||||
|
||||
// NewHost new a host
|
||||
func NewHost(addr, user, password string) Host {
|
||||
return Host{
|
||||
Host: addr,
|
||||
User: user,
|
||||
Password: password,
|
||||
Enabled: false,
|
||||
Provisioned: false,
|
||||
}
|
||||
}
|
||||
|
||||
// Valid if host is valid
|
||||
func (h Host) Valid() bool {
|
||||
// TODO stronger check
|
||||
return h.Host != ""
|
||||
}
|
||||
|
||||
// IsLocal if host is localhost
|
||||
func (h Host) IsLocal() bool {
|
||||
if !h.Valid() {
|
||||
return false
|
||||
}
|
||||
return h.Host == "127.0.0.1" || h.Host == "localhost"
|
||||
}
|
||||
|
||||
// IsRemote is host is remote
|
||||
func (h Host) IsRemote() bool {
|
||||
return !h.IsLocal()
|
||||
}
|
||||
@@ -1,149 +0,0 @@
|
||||
// Code generated by MockGen. DO NOT EDIT.
|
||||
// Source: ./config.go
|
||||
|
||||
// Package mock_config is a generated GoMock package.
|
||||
package mock_config
|
||||
|
||||
import (
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
config "github.com/metrue/fx/config"
|
||||
reflect "reflect"
|
||||
)
|
||||
|
||||
// MockConfiger is a mock of Configer interface
|
||||
type MockConfiger struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *MockConfigerMockRecorder
|
||||
}
|
||||
|
||||
// MockConfigerMockRecorder is the mock recorder for MockConfiger
|
||||
type MockConfigerMockRecorder struct {
|
||||
mock *MockConfiger
|
||||
}
|
||||
|
||||
// NewMockConfiger creates a new mock instance
|
||||
func NewMockConfiger(ctrl *gomock.Controller) *MockConfiger {
|
||||
mock := &MockConfiger{ctrl: ctrl}
|
||||
mock.recorder = &MockConfigerMockRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
// EXPECT returns an object that allows the caller to indicate expected use
|
||||
func (m *MockConfiger) EXPECT() *MockConfigerMockRecorder {
|
||||
return m.recorder
|
||||
}
|
||||
|
||||
// GetMachine mocks base method
|
||||
func (m *MockConfiger) GetMachine(name string) (config.Host, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "GetMachine", name)
|
||||
ret0, _ := ret[0].(config.Host)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// GetMachine indicates an expected call of GetMachine
|
||||
func (mr *MockConfigerMockRecorder) GetMachine(name interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetMachine", reflect.TypeOf((*MockConfiger)(nil).GetMachine), name)
|
||||
}
|
||||
|
||||
// AddMachine mocks base method
|
||||
func (m *MockConfiger) AddMachine(name string, host config.Host) error {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "AddMachine", name, host)
|
||||
ret0, _ := ret[0].(error)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// AddMachine indicates an expected call of AddMachine
|
||||
func (mr *MockConfigerMockRecorder) AddMachine(name, host interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AddMachine", reflect.TypeOf((*MockConfiger)(nil).AddMachine), name, host)
|
||||
}
|
||||
|
||||
// RemoveHost mocks base method
|
||||
func (m *MockConfiger) RemoveHost(name string) error {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "RemoveHost", name)
|
||||
ret0, _ := ret[0].(error)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// RemoveHost indicates an expected call of RemoveHost
|
||||
func (mr *MockConfigerMockRecorder) RemoveHost(name interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RemoveHost", reflect.TypeOf((*MockConfiger)(nil).RemoveHost), name)
|
||||
}
|
||||
|
||||
// ListActiveMachines mocks base method
|
||||
func (m *MockConfiger) ListActiveMachines() (map[string]config.Host, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "ListActiveMachines")
|
||||
ret0, _ := ret[0].(map[string]config.Host)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// ListActiveMachines indicates an expected call of ListActiveMachines
|
||||
func (mr *MockConfigerMockRecorder) ListActiveMachines() *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListActiveMachines", reflect.TypeOf((*MockConfiger)(nil).ListActiveMachines))
|
||||
}
|
||||
|
||||
// ListMachines mocks base method
|
||||
func (m *MockConfiger) ListMachines() (map[string]config.Host, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "ListMachines")
|
||||
ret0, _ := ret[0].(map[string]config.Host)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// ListMachines indicates an expected call of ListMachines
|
||||
func (mr *MockConfigerMockRecorder) ListMachines() *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListMachines", reflect.TypeOf((*MockConfiger)(nil).ListMachines))
|
||||
}
|
||||
|
||||
// EnableMachine mocks base method
|
||||
func (m *MockConfiger) EnableMachine(name string) error {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "EnableMachine", name)
|
||||
ret0, _ := ret[0].(error)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// EnableMachine indicates an expected call of EnableMachine
|
||||
func (mr *MockConfigerMockRecorder) EnableMachine(name interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "EnableMachine", reflect.TypeOf((*MockConfiger)(nil).EnableMachine), name)
|
||||
}
|
||||
|
||||
// DisableMachine mocks base method
|
||||
func (m *MockConfiger) DisableMachine(name string) error {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "DisableMachine", name)
|
||||
ret0, _ := ret[0].(error)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// DisableMachine indicates an expected call of DisableMachine
|
||||
func (mr *MockConfigerMockRecorder) DisableMachine(name interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DisableMachine", reflect.TypeOf((*MockConfiger)(nil).DisableMachine), name)
|
||||
}
|
||||
|
||||
// UpdateProvisionedStatus mocks base method
|
||||
func (m *MockConfiger) UpdateProvisionedStatus(name string, ok bool) error {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "UpdateProvisionedStatus", name, ok)
|
||||
ret0, _ := ret[0].(error)
|
||||
return ret0
|
||||
}
|
||||
|
||||
// UpdateProvisionedStatus indicates an expected call of UpdateProvisionedStatus
|
||||
func (mr *MockConfigerMockRecorder) UpdateProvisionedStatus(name, ok interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateProvisionedStatus", reflect.TypeOf((*MockConfiger)(nil).UpdateProvisionedStatus), name, ok)
|
||||
}
|
||||
15
constants/languages.go
Normal file
15
constants/languages.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package constants
|
||||
|
||||
// ExtLangMapping file extension mapping with programming language
|
||||
var ExtLangMapping = map[string]string{
|
||||
".js": "node",
|
||||
".go": "go",
|
||||
".rb": "ruby",
|
||||
".py": "python",
|
||||
".php": "php",
|
||||
".jl": "julia",
|
||||
".java": "java",
|
||||
".d": "d",
|
||||
".rs": "rust",
|
||||
".pl": "perl",
|
||||
}
|
||||
@@ -1,19 +1,33 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/apex/log"
|
||||
dockerTypes "github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/container"
|
||||
dockerTypesContainer "github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/docker/api/types/network"
|
||||
"github.com/docker/go-connections/nat"
|
||||
"github.com/google/go-querystring/query"
|
||||
"github.com/google/uuid"
|
||||
fxConfig "github.com/metrue/fx/config"
|
||||
containerruntimes "github.com/metrue/fx/container_runtimes"
|
||||
"github.com/metrue/fx/types"
|
||||
"github.com/metrue/fx/utils"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// API interact with dockerd http api
|
||||
@@ -24,27 +38,29 @@ type API struct {
|
||||
|
||||
// Create a API
|
||||
func Create(host string, port string) (*API, error) {
|
||||
version, err := utils.DockerVersion(host, port)
|
||||
addr := host + ":" + port
|
||||
v, err := version(addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
endpoint := fmt.Sprintf("http://%s:%s/v%s", host, port, version)
|
||||
endpoint := fmt.Sprintf("http://%s:%s/v%s", host, port, v)
|
||||
return &API{
|
||||
endpoint: endpoint,
|
||||
version: version,
|
||||
version: v,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// MustCreate a api object, panic if not
|
||||
func MustCreate(host string, port string) *API {
|
||||
version, err := utils.DockerVersion(host, port)
|
||||
addr := host + ":" + port
|
||||
v, err := version(addr)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
endpoint := fmt.Sprintf("http://%s:%s/v%s", host, port, version)
|
||||
endpoint := fmt.Sprintf("http://%s:%s/v%s", host, port, v)
|
||||
return &API{
|
||||
endpoint: endpoint,
|
||||
version: version,
|
||||
version: v,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,8 +132,47 @@ func (api *API) post(path string, body []byte, expectStatus int, v interface{})
|
||||
return nil
|
||||
}
|
||||
|
||||
// List list service
|
||||
func (api *API) list(name string) ([]types.Service, error) {
|
||||
// Version get version of docker engine
|
||||
func (api *API) Version(ctx context.Context) (string, error) {
|
||||
return version(api.endpoint)
|
||||
}
|
||||
|
||||
func version(endpoint string) (string, error) {
|
||||
path := endpoint + "/version"
|
||||
if !strings.HasPrefix(path, "http") {
|
||||
path = "http://" + path
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("GET", path, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
client := &http.Client{Timeout: 20 * time.Second}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
return "", fmt.Errorf("request %s failed: %d - %s", path, resp.StatusCode, resp.Status)
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var res dockerTypes.Version
|
||||
err = json.Unmarshal(body, &res)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return res.APIVersion, nil
|
||||
}
|
||||
|
||||
// ListContainer list service
|
||||
func (api *API) ListContainer(ctx context.Context, name string) ([]types.Service, error) {
|
||||
if name != "" {
|
||||
info, err := api.inspect(name)
|
||||
if err != nil {
|
||||
@@ -141,7 +196,7 @@ func (api *API) list(name string) ([]types.Service, error) {
|
||||
}
|
||||
|
||||
type filterItem struct {
|
||||
Status []string `json:"url,omitempty"`
|
||||
Status []string `json:"status,omitempty"`
|
||||
Label []string `json:"label,omitempty"`
|
||||
Name []string `json:"name,omitempty"`
|
||||
}
|
||||
@@ -173,17 +228,27 @@ func (api *API) list(name string) ([]types.Service, error) {
|
||||
|
||||
svs := make(map[string]types.Service)
|
||||
for _, container := range containers {
|
||||
name := "UNKNOWN"
|
||||
if len(container.Names) > 0 {
|
||||
name = container.Names[0]
|
||||
}
|
||||
|
||||
port := -1
|
||||
ip := "UNKNOWN"
|
||||
if len(container.Ports) > 0 {
|
||||
ip = container.Ports[0].IP
|
||||
port = int(container.Ports[0].PublicPort)
|
||||
}
|
||||
|
||||
// container name have extra forward slash
|
||||
// https://github.com/moby/moby/issues/6705
|
||||
if strings.HasPrefix(container.Names[0], fmt.Sprintf("/%s", name)) {
|
||||
svs[container.Image] = types.Service{
|
||||
Name: container.Names[0],
|
||||
Image: container.Image,
|
||||
ID: container.ID,
|
||||
Host: container.Ports[0].IP,
|
||||
Port: int(container.Ports[0].PublicPort),
|
||||
State: container.State,
|
||||
}
|
||||
svs[container.Image] = types.Service{
|
||||
Name: name,
|
||||
Image: container.Image,
|
||||
ID: container.ID,
|
||||
Host: ip,
|
||||
Port: port,
|
||||
State: container.State,
|
||||
}
|
||||
}
|
||||
services := []types.Service{}
|
||||
@@ -193,3 +258,223 @@ func (api *API) list(name string) ([]types.Service, error) {
|
||||
|
||||
return services, nil
|
||||
}
|
||||
|
||||
// BuildImage build image
|
||||
func (api *API) BuildImage(ctx context.Context, workdir string, name string) error {
|
||||
tarDir, err := ioutil.TempDir("/tmp", "fx-tar")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer os.RemoveAll(tarDir)
|
||||
|
||||
imageID := uuid.New().String()
|
||||
tarFilePath := filepath.Join(tarDir, fmt.Sprintf("%s.tar", imageID))
|
||||
|
||||
if err := utils.TarDir(workdir, tarFilePath); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dockerBuildContext, err := os.Open(tarFilePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer dockerBuildContext.Close()
|
||||
|
||||
type buildQuery struct {
|
||||
Labels string `url:"labels,omitempty"`
|
||||
Tags string `url:"t,omitempty"`
|
||||
Dockerfile string `url:"dockerfile,omitempty"`
|
||||
}
|
||||
|
||||
// Apply default labels
|
||||
labelsJSON, _ := json.Marshal(map[string]string{
|
||||
"belong-to": "fx",
|
||||
})
|
||||
q := buildQuery{
|
||||
Labels: string(labelsJSON),
|
||||
Dockerfile: "Dockerfile",
|
||||
}
|
||||
|
||||
qs, err := query.Values(q)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
qs.Add("t", name)
|
||||
qs.Add("t", imageID)
|
||||
|
||||
path := "/build"
|
||||
url := fmt.Sprintf("%s%s?%s", api.endpoint, path, qs.Encode())
|
||||
req, err := http.NewRequest("POST", url, dockerBuildContext)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/x-tar")
|
||||
client := &http.Client{Timeout: 600 * time.Second}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
scanner := bufio.NewScanner(resp.Body)
|
||||
|
||||
for scanner.Scan() {
|
||||
if os.Getenv("DEBUG") != "" {
|
||||
log.Infof(scanner.Text())
|
||||
}
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// PushImage push a image
|
||||
func (api *API) PushImage(ctx context.Context, name string) (string, error) {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
// InspectImage inspect image
|
||||
func (api *API) InspectImage(ctx context.Context, name string, image interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// TagImage tag image
|
||||
func (api *API) TagImage(ctx context.Context, name string, tag string) error {
|
||||
query := url.Values{}
|
||||
query.Set("repo", name)
|
||||
query.Set("tag", tag)
|
||||
path := fmt.Sprintf("/images/%s/tag?%s", name, query.Encode())
|
||||
|
||||
url := fmt.Sprintf("%s%s", api.endpoint, path)
|
||||
req, err := http.NewRequest("POST", url, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
client := &http.Client{Timeout: 10 * time.Second}
|
||||
if _, err = client.Do(req); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// StartContainer start container
|
||||
func (api *API) StartContainer(ctx context.Context, name string, image string, bindings []types.PortBinding) error {
|
||||
networks, err := api.GetNetwork(fxNetworkName)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "get network failed: %s", err)
|
||||
}
|
||||
|
||||
if len(networks) == 0 {
|
||||
if err := api.CreateNetwork(fxNetworkName); err != nil {
|
||||
return errors.Wrapf(err, "error create network: %s", err)
|
||||
}
|
||||
}
|
||||
networks, _ = api.GetNetwork(fxNetworkName)
|
||||
|
||||
endpoint := &network.EndpointSettings{
|
||||
NetworkID: networks[0].ID,
|
||||
}
|
||||
networkConfig := &network.NetworkingConfig{
|
||||
EndpointsConfig: map[string]*network.EndpointSettings{
|
||||
"fx-net": endpoint,
|
||||
},
|
||||
}
|
||||
|
||||
portSet := nat.PortSet{}
|
||||
portMap := nat.PortMap{}
|
||||
for _, binding := range bindings {
|
||||
bindings := []nat.PortBinding{
|
||||
nat.PortBinding{
|
||||
HostIP: types.DefaultHost,
|
||||
HostPort: fmt.Sprintf("%d", binding.ServiceBindingPort),
|
||||
},
|
||||
}
|
||||
port := nat.Port(fmt.Sprintf("%d/tcp", binding.ContainerExposePort))
|
||||
portSet[port] = struct{}{}
|
||||
portMap[port] = bindings
|
||||
}
|
||||
config := &dockerTypesContainer.Config{
|
||||
Image: image,
|
||||
ExposedPorts: portSet,
|
||||
}
|
||||
|
||||
hostConfig := &dockerTypesContainer.HostConfig{
|
||||
AutoRemove: !fxConfig.DisableContainerAutoremove,
|
||||
PortBindings: portMap,
|
||||
}
|
||||
|
||||
req := ContainerCreateRequestPayload{
|
||||
Config: config,
|
||||
HostConfig: hostConfig,
|
||||
NetworkingConfig: networkConfig,
|
||||
}
|
||||
|
||||
body, err := json.Marshal(req)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error mashal container create req")
|
||||
}
|
||||
|
||||
// create container
|
||||
path := fmt.Sprintf("/containers/create?name=%s", name)
|
||||
var createRes container.ContainerCreateCreatedBody
|
||||
if err := api.post(path, body, 201, &createRes); err != nil {
|
||||
return errors.Wrap(err, "create container request failed")
|
||||
}
|
||||
|
||||
if createRes.ID == "" {
|
||||
return fmt.Errorf("container id is missing")
|
||||
}
|
||||
|
||||
// start container
|
||||
path = fmt.Sprintf("/containers/%s/start", createRes.ID)
|
||||
url := fmt.Sprintf("%s%s", api.endpoint, path)
|
||||
request, err := http.NewRequest("POST", url, nil)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error new container create request")
|
||||
}
|
||||
client := &http.Client{Timeout: 20 * time.Second}
|
||||
resp, err := client.Do(request)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error do start container request")
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(b) != 0 {
|
||||
msg := fmt.Sprintf("start container met issue: %s", string(b))
|
||||
return errors.New(msg)
|
||||
}
|
||||
|
||||
if _, err = api.inspect(createRes.ID); err != nil {
|
||||
msg := fmt.Sprintf("inspect container %s error", name)
|
||||
return errors.Wrap(err, msg)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// StopContainer stop a container
|
||||
func (api *API) StopContainer(ctx context.Context, name string) error {
|
||||
return api.Stop(name)
|
||||
}
|
||||
|
||||
// InspectContainer inspect container
|
||||
func (api *API) InspectContainer(ctx context.Context, name string, container interface{}) error {
|
||||
path := fmt.Sprintf("/containers/%s/json", name)
|
||||
if err := api.get(path, "", &container); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var (
|
||||
_ containerruntimes.ContainerRuntime = &API{}
|
||||
)
|
||||
|
||||
@@ -1,89 +1,31 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/metrue/fx/config"
|
||||
"github.com/metrue/fx/constants"
|
||||
"github.com/metrue/fx/types"
|
||||
"github.com/docker/docker/api/types"
|
||||
)
|
||||
|
||||
func TestDockerHTTP(t *testing.T) {
|
||||
host := config.Host{Host: "127.0.0.1"}
|
||||
api, err := Create(host.Host, constants.AgentPort)
|
||||
host := os.Getenv("DOCKER_ENGINE_HOST")
|
||||
port := os.Getenv("DOCKER_ENGINE_PORT")
|
||||
if host == "" ||
|
||||
port == "" {
|
||||
t.Skip("DOCKER_ENGINE_HOST and DOCKER_ENGINE_PORT required")
|
||||
}
|
||||
|
||||
api, err := Create(host, port)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
serviceName := "a-test-service"
|
||||
project := types.Project{
|
||||
Name: serviceName,
|
||||
Language: "node",
|
||||
Files: []types.ProjectSourceFile{
|
||||
types.ProjectSourceFile{
|
||||
Path: "Dockerfile",
|
||||
Body: `
|
||||
FROM metrue/fx-node-base
|
||||
|
||||
COPY . .
|
||||
EXPOSE 3000
|
||||
CMD ["node", "app.js"]`,
|
||||
IsHandler: false,
|
||||
},
|
||||
types.ProjectSourceFile{
|
||||
Path: "app.js",
|
||||
Body: `
|
||||
const Koa = require('koa');
|
||||
const bodyParser = require('koa-bodyparser');
|
||||
const func = require('./fx');
|
||||
|
||||
const app = new Koa();
|
||||
app.use(bodyParser());
|
||||
app.use(ctx => {
|
||||
const msg = func(ctx.request.body);
|
||||
ctx.body = msg;
|
||||
});
|
||||
|
||||
app.listen(3000);`,
|
||||
IsHandler: false,
|
||||
},
|
||||
types.ProjectSourceFile{
|
||||
Path: "fx.js",
|
||||
Body: `
|
||||
module.exports = (input) => {
|
||||
return input.a + input.b
|
||||
}
|
||||
`,
|
||||
IsHandler: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
service, err := api.Build(project)
|
||||
if err != nil {
|
||||
name := "fx-agent"
|
||||
var container types.ContainerJSON
|
||||
if err := api.InspectContainer(context.Background(), name, &container); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if service.Name != serviceName {
|
||||
t.Fatalf("should get %s but got %s", serviceName, service.Name)
|
||||
}
|
||||
|
||||
if err := api.Run(9999, &service); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
services, err := api.list(serviceName)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(services) != 1 {
|
||||
t.Fatal("service number should be 1")
|
||||
}
|
||||
|
||||
if err := api.Stop(serviceName); err != nil {
|
||||
t.Fatal(err)
|
||||
if container.Name != "/"+name {
|
||||
t.Fatalf("should get %s but got %s", name, container.Name)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,60 +1,51 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/apex/log"
|
||||
"github.com/metrue/fx/types"
|
||||
"github.com/metrue/fx/utils"
|
||||
)
|
||||
|
||||
// Call function directly with given params
|
||||
func (api *API) Call(file string, param string, project types.Project) error {
|
||||
service, err := api.Build(project)
|
||||
if err != nil {
|
||||
log.Fatalf("Build Service: %v", err)
|
||||
return err
|
||||
}
|
||||
log.Info("Build Service: \u2713")
|
||||
|
||||
if err := api.Run(9999, &service); err != nil {
|
||||
log.Fatalf("Run Service: %v", err)
|
||||
return err
|
||||
}
|
||||
log.Info("Run Service: \u2713")
|
||||
|
||||
params := utils.PairsToParams(strings.Fields(param))
|
||||
body, err := json.Marshal(params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Wait 2 seconds for service startup
|
||||
time.Sleep(time.Second * 2)
|
||||
|
||||
url := fmt.Sprintf("http://%s:%d", service.Host, service.Port)
|
||||
r, err := http.NewRequest("POST", url, bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.Header.Set("Content-Type", "application/json")
|
||||
client := &http.Client{Timeout: 20 * time.Second}
|
||||
resp, err := client.Do(r)
|
||||
if err != nil {
|
||||
log.Fatalf("Call Service: %v", err)
|
||||
return err
|
||||
}
|
||||
buf, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Fatalf("Call Service: %v", err)
|
||||
return err
|
||||
}
|
||||
log.Info("Call Service: \u2713")
|
||||
return utils.OutputJSON(string(buf))
|
||||
return nil
|
||||
// service, err := api.Build(project)
|
||||
// if err != nil {
|
||||
// log.Fatalf("Build Service: %v", err)
|
||||
// return err
|
||||
// }
|
||||
// log.Info("Build Service: \u2713")
|
||||
//
|
||||
// if err := api.Run(9999, &service); err != nil {
|
||||
// log.Fatalf("Run Service: %v", err)
|
||||
// return err
|
||||
// }
|
||||
// log.Info("Run Service: \u2713")
|
||||
//
|
||||
// params := utils.PairsToParams(strings.Fields(param))
|
||||
// body, err := json.Marshal(params)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
//
|
||||
// // Wait 2 seconds for service startup
|
||||
// time.Sleep(time.Second * 2)
|
||||
//
|
||||
// url := fmt.Sprintf("http://%s:%d", service.Host, service.Port)
|
||||
// r, err := http.NewRequest("POST", url, bytes.NewReader(body))
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// r.Header.Set("Content-Type", "application/json")
|
||||
// client := &http.Client{Timeout: 20 * time.Second}
|
||||
// resp, err := client.Do(r)
|
||||
// if err != nil {
|
||||
// log.Fatalf("Call Service: %v", err)
|
||||
// return err
|
||||
// }
|
||||
// buf, err := ioutil.ReadAll(resp.Body)
|
||||
// if err != nil {
|
||||
// log.Fatalf("Call Service: %v", err)
|
||||
// return err
|
||||
// }
|
||||
// log.Info("Call Service: \u2713")
|
||||
// return utils.OutputJSON(string(buf))
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user