Skip to content

Commit

Permalink
Merge pull request #41 from berty/html-mods
Browse files Browse the repository at this point in the history
feat: web module system
  • Loading branch information
n0izn0iz authored Feb 7, 2022
2 parents 805576c + 1799db2 commit 654593e
Show file tree
Hide file tree
Showing 67 changed files with 28,492 additions and 201 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ios.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ jobs:

- name: Install iOS app deps (node_modules)
working-directory: rn
run: make node_modules/.fresh
run: make node_modules/.mkt

- name: Install iOS app deps (XCode Workspace)
working-directory: rn
run: make ios/Pods/.fresh
run: make ios/Pods/.mkt

- name: Build the Archive
working-directory: rn
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ clean.gen:
.PHONY: clean-gen

generate:
$(MAKE) -C rn node_modules/.fresh
$(MAKE) -C rn node_modules/.mkt
buf generate api
.PHONY: generate

Expand Down
6 changes: 4 additions & 2 deletions go/bind/labs/config.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package labs

type Config struct {
Address string
Address string
HTMLModules string
}

func NewConfig() *Config {
return &Config{
Address: "127.0.0.1:9315",
Address: "127.0.0.1:9315",
HTMLModules: "html-mods.bundle",
}
}
31 changes: 31 additions & 0 deletions go/bind/labs/labs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package labs
import (
"fmt"
"net/http"
"path/filepath"
"sync"

"github.com/improbable-eng/grpc-web/go/grpcweb"
Expand All @@ -26,6 +27,36 @@ func NewLabs(config *Config) (*Labs, error) {
}
logger = logger.Named("labs")

htmlModules := config.HTMLModules

moduleMutex := new(sync.Mutex)
module := ""
staticSrv := http.Server{
Addr: "127.0.0.1:9316",
Handler: http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
moduleName := r.Header.Get("X-Labs-Module")
moduleMutex.Lock()
if moduleName == "" && module == "" {
rw.WriteHeader(422)
moduleMutex.Unlock()
return
}
if moduleName != "" {
module = moduleName
}
moduleName = module
moduleMutex.Unlock()
rw.Header().Set("X-Labs-Module", moduleName)
http.FileServer(http.Dir(filepath.Join(htmlModules, moduleName))).ServeHTTP(rw, r)
}),
}

go func() {
if err := staticSrv.ListenAndServe(); err != nil {
logger.Error("grpc server listener died", zap.Error(err))
}
}()

grpcServer := grpc.NewServer()
cleaners := make([]func() error, len(servers))
for i, def := range servers {
Expand Down
2 changes: 1 addition & 1 deletion go/mod/datastorebench/datastorebench.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ func (m *module) Info() (*blmod.ModuleInfo, error) {
func (m *module) Run(ctx context.Context) (*blmod.RunModuleResponse, error) {
return &blmod.RunModuleResponse{
ReportKind: blmod.RunModuleResponse_REPORT_KIND_UTF,
ReportData: ([]byte)("Hello module!"),
ReportData: ([]byte)("Hello from Go module!"),
}, nil
}
24 changes: 24 additions & 0 deletions go/pkg/ipfsman/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
context "context"
crand "crypto/rand"
"encoding/base64"
"encoding/json"
"fmt"
"path"
"strings"
"sync"

mobileipfs "github.com/ipfs-shipyard/gomobile-ipfs/go/bind/core"
ipfs_config "github.com/ipfs/go-ipfs-config"
"github.com/pkg/errors"
"go.uber.org/multierr"
"go.uber.org/zap"
Expand Down Expand Up @@ -143,6 +145,9 @@ func startIPFSNode(repoPath string, initialConfig *mobileipfs.Config, proxDriver
if initialConfig, err = mobileipfs.NewDefaultConfig(); err != nil {
return nil, errors.Wrap(err, "get default IPFS config")
}
if err := overrideAPIAllowedOrigins(initialConfig, "*"); err != nil {
return nil, errors.Wrap(err, "override default IPFS API allowed origins")
}
}
if err := mobileipfs.InitRepo(repoPath, initialConfig); err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("init repo at '%s'", repoPath))
Expand All @@ -159,6 +164,25 @@ func startIPFSNode(repoPath string, initialConfig *mobileipfs.Config, proxDriver
return node, nil
}

func overrideAPIAllowedOrigins(initialConfig *mobileipfs.Config, origin string) error {
apiConfigBytes, err := initialConfig.GetKey("API")
if err != nil {
return errors.Wrap(err, "get api config")
}
var apiConfig ipfs_config.API
if err := json.Unmarshal(apiConfigBytes, &apiConfig); err != nil {
return errors.Wrap(err, "unmarshal api config")
}
apiConfig.HTTPHeaders["Access-Control-Allow-Origin"] = []string{origin}
if apiConfigBytes, err = json.Marshal(apiConfig); err != nil {
return errors.Wrap(err, "remarshal api config")
}
if err := initialConfig.SetKey("API", apiConfigBytes); err != nil {
return errors.Wrap(err, "set api config")
}
return nil
}

type nodeWrapper struct {
node *mobileipfs.Node
repoPath string
Expand Down
2 changes: 2 additions & 0 deletions rn/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/html-mods
/html-mods.bundle
3 changes: 2 additions & 1 deletion rn/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,5 @@ android/app/gradlew.bat
/ios/vendor/
/gomobile-ipfs/
/ios/vendored_pods/
/react-native-labs-bridge/ios/Labsbridge.xcframework
/react-native-labs-bridge/ios/Labsbridge.xcframework
/html-mods.bundle
31 changes: 20 additions & 11 deletions rn/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,24 @@ gomobile-ipfs-dst=ios/vendored_pods

kill-program-using-port = $(foreach port,$(1),$(eval pid ?= $(shell lsof -t -i :$(port))) $(if $(pid),$(shell kill $(pid)),))

### html mods

html-mods=$(wildcard html-mods/*)

html-mods.all:
mkdir -p html-mods.bundle
@for mod in $(html-mods); do echo "--- Making HTML module $$(basename $$mod) ---"; make -C $$mod; done
@echo --- Done making HTML modules ---
.PHONY: html-mods.all

### js

.PHONY: ios
ios: node_modules/.fresh ios/Pods/.fresh
ios: node_modules/.mkt ios/Pods/.mkt html-mods.all
npx react-native run-ios \
--port="$(METRO_RN_PORT)" \
$(if $(IOS_DEVICE),--udid='$(IOS_DEVICE)',) \
$(IOS_RN_FLAGS)
--port="$(METRO_RN_PORT)" \
$(if $(IOS_DEVICE),--udid='$(IOS_DEVICE)',) \
$(IOS_RN_FLAGS)

$(IOS_CORE): ../go.mod ../go.sum $(shell find ../go -name '*.go')
$(GO) mod download
Expand All @@ -42,8 +52,7 @@ unlink:
cd react-native-labs-bridge && yarn unlink || true
rm -f .link


node_modules/.fresh: yarn.lock package.json $(wildcard patches/%)
node_modules/.mkt: yarn.lock package.json $(wildcard patches/*)
$(MAKE) unlink
yarn
$(MAKE) .link
Expand All @@ -55,33 +64,33 @@ ios/vendor/bundle: ios/Gemfile ios/Gemfile.lock
cd ios && bundle install
touch $@

ios/Pods/.fresh: $(IOS_CORE) ios/Podfile ios/Podfile.lock node_modules/.fresh ios/vendor/bundle
ios/Pods/.mkt: $(IOS_CORE) ios/Podfile ios/Podfile.lock node_modules/.mkt ios/vendor/bundle
cd ios && bundle exec pod install --repo-update
touch $@

#### Android

.PHONY: android
android: node_modules/.fresh
android: node_modules/.mkt
npx react-native run-android \
--port="$(METRO_RN_PORT)"

#### Lint

.PHONY: lint
lint: node_modules/.fresh
lint: node_modules/.mkt
yarn lint
npx tsc

.PHONY: lint.fix
lint.fix: node_modules/.fresh
lint.fix: node_modules/.mkt
yarn lint --fix
npx tsc

#### React-Native

.PHONY: metro.start
metro.start: node_modules/.fresh
metro.start: node_modules/.mkt
npx react-native start --port=$(METRO_RN_PORT)

.PHONY: clean
Expand Down
1 change: 1 addition & 0 deletions rn/html-mods/ipfs-upload-file/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/repo
30 changes: 30 additions & 0 deletions rn/html-mods/ipfs-upload-file/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
NAME=ipfs-upload-file
RN_ROOT=../..
BUNDLE_ROOT=$(RN_ROOT)/html-mods.bundle
BUILD_TARGET=$(BUNDLE_ROOT)/$(NAME)
INDEX_HTML=$(BUILD_TARGET)/index.html
REPO_ROOT=repo
NODE_MODULES_TARGET=$(REPO_ROOT)/node_modules/.mkt
REPO_TARGET=$(REPO_ROOT)/.mkt
DIST_DIR=$(REPO_ROOT)/dist

$(INDEX_HTML): $(REPO_TARGET) $(NODE_MODULES_TARGET) info.json
rm -fr $(DIST_DIR)
cd $(REPO_ROOT) && npm run build
rm -fr $(BUILD_TARGET)
cp -r $(DIST_DIR) $(BUILD_TARGET)
cp info.json $(BUILD_TARGET)/info.json

$(REPO_TARGET): remote commit
rm -fr $(REPO_ROOT)
git clone $(shell cat remote) $(REPO_ROOT)
cd $(REPO_ROOT) && git checkout $(shell cat commit)
touch $@

$(NODE_MODULES_TARGET): $(REPO_TARGET) $(REPO_ROOT)/package.json
cd $(REPO_ROOT) && npm install
touch $@

dev: $(REPO_TARGET) $(NODE_MODULES_TARGET)
cd $(REPO_ROOT) &&npm start
.PHONY: dev
1 change: 1 addition & 0 deletions rn/html-mods/ipfs-upload-file/commit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
44f4cd8bd0178e39e59eb8ab685dccecc4b64c89
6 changes: 6 additions & 0 deletions rn/html-mods/ipfs-upload-file/info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"displayName": "IPFS File Upload",
"shortDescription": "Upload a file to IPFS",
"iconKind": "UTF",
"iconUTF": "⬆️"
}
1 change: 1 addition & 0 deletions rn/html-mods/ipfs-upload-file/remote
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[email protected]:ipfs-examples/js-ipfs-http-client-upload-file.git
1 change: 1 addition & 0 deletions rn/html-mods/ipns-publish/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/repo
30 changes: 30 additions & 0 deletions rn/html-mods/ipns-publish/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
NAME=ipns-publish
RN_ROOT=../..
BUNDLE_ROOT=$(RN_ROOT)/html-mods.bundle
BUILD_TARGET=$(BUNDLE_ROOT)/$(NAME)
INDEX_HTML=$(BUILD_TARGET)/index.html
REPO_ROOT=repo
NODE_MODULES_TARGET=$(REPO_ROOT)/node_modules/.mkt
REPO_TARGET=$(REPO_ROOT)/.mkt
DIST_DIR=$(REPO_ROOT)/dist

$(INDEX_HTML): $(REPO_TARGET) $(NODE_MODULES_TARGET) info.json
rm -fr $(DIST_DIR)
cd $(REPO_ROOT) && npm run build
rm -fr $(BUILD_TARGET)
cp -r $(DIST_DIR) $(BUILD_TARGET)
cp info.json $(BUILD_TARGET)/info.json

$(REPO_TARGET): remote commit
rm -fr $(REPO_ROOT)
git clone $(shell cat remote) $(REPO_ROOT)
cd $(REPO_ROOT) && git checkout $(shell cat commit)
touch $@

$(NODE_MODULES_TARGET): $(REPO_TARGET) $(REPO_ROOT)/package.json
cd $(REPO_ROOT) && npm install
touch $@

dev: $(REPO_TARGET) $(NODE_MODULES_TARGET)
cd $(REPO_ROOT) &&npm start
.PHONY: dev
1 change: 1 addition & 0 deletions rn/html-mods/ipns-publish/commit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
b5d5bf16236ad3052101e5893b9adcde332fbc23
6 changes: 6 additions & 0 deletions rn/html-mods/ipns-publish/info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"displayName": "IPNS Publish",
"shortDescription": "Publish to IPNS using JS and Go",
"iconKind": "UTF",
"iconUTF": "📰"
}
1 change: 1 addition & 0 deletions rn/html-mods/ipns-publish/remote
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[email protected]:ipfs-examples/js-ipfs-browser-ipns-publish.git
23 changes: 23 additions & 0 deletions rn/html-mods/react-typescript/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
23 changes: 23 additions & 0 deletions rn/html-mods/react-typescript/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
NAME=react-typescript
RN_ROOT=../..
BUNDLE_ROOT=$(RN_ROOT)/html-mods.bundle
BUILD_TARGET=$(BUNDLE_ROOT)/$(NAME)
INDEX_HTML=$(BUILD_TARGET)/index.html
NODE_MODULES_TARGET=node_modules/.mkt
DIST_DIR=build
SOURCES=$(shell find src public)

$(INDEX_HTML): $(NODE_MODULES_TARGET) $(SOURCES) info.json
rm -fr $(DIST_DIR)
npm run build
rm -fr $(BUILD_TARGET)
cp -r $(DIST_DIR) $(BUILD_TARGET)
cp info.json $(BUILD_TARGET)/info.json

$(NODE_MODULES_TARGET): package.json
npm install
touch $@

dev: $(NODE_MODULES_TARGET)
npm start
.PHONY: dev
Loading

0 comments on commit 654593e

Please sign in to comment.