Skip to content

Commit

Permalink
feat(SlimFaasPlanetSaver): add npm component
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaume-chervet committed Nov 29, 2024
1 parent cdd18f6 commit a43108f
Show file tree
Hide file tree
Showing 21 changed files with 8,500 additions and 1 deletion.
37 changes: 37 additions & 0 deletions .github/workflows/slimfaas-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ on:
branches: [ main ]
types: [opened, synchronize, reopened]

env:
PNPM_VERSION: 8.6.11
NODE_VERSION: 18

jobs:
sonarcloud:
name: SonarCloud
Expand Down Expand Up @@ -137,3 +141,36 @@ jobs:
secrets:
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}

build_slimfaas_planet_saver:
needs: tags
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- uses: actions/setup-node@v2
with:
node-version: ${{ env.NODE_VERSION }}
- uses: pnpm/action-setup@v2
name: Install pnpm
with:
version: ${{ env.PNPM_VERSION }}
run_install: false
- name: pnpm ci
run: pnpm i --frozen-lockfile
working-directory: ./src/SlimFaasPlanetSaver
- name: Git Configuration
id: gitconfig
run: |
git config --global user.email "[email protected]"
git config --global user.name "GitHub"
- name: pnpm version ${{ steps.tag.outputs.new_version }}
if: github.ref == 'refs/heads/main'
run: |
pnpm version ${{ steps.tag.outputs.new_version }}
working-directory: ./src/SlimFaasPlanetSaver
- id: Publish
uses: JS-DevTools/npm-publish@v1
if: github.ref == 'refs/heads/main'
with:
token: ${{ secrets.NPM_TOKEN }}
package: ./src/SlimFaasPlanetSaver/package.json
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ Now, you can access your pod via SlimFaas proxy:

Enjoy slimfaas!!!!

## Getting Started with @axa-fr/slimfaas-planet-saver

[`@axa-fr/slimfaas-planet-saver readme.md`](./src/SlimFaasPlanetSaver#README.md) : A vanilla JavaScript library to start and monitor replicas from javascript frontend.

![SlimFaasPlanetSaver.gif](https://github.com/AxaFrance/SlimFaas/blob/main/documentation/SlimfaasPlanetSaver.gif)

## How it works

SlimFaas act as an HTTP proxy with 2 modes:
Expand Down
Binary file added documentation/SlimfaasPlanetSaver.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion src/FibonacciReact/public/vite.svg

This file was deleted.

26 changes: 26 additions & 0 deletions src/SlimFaasPlanetSaver/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

*storybook.log
78 changes: 78 additions & 0 deletions src/SlimFaasPlanetSaver/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# @axa-fr/slimfaas-planet-saver

![SlimFaas.png](https://github.com/AxaFrance/SlimFaas/blob/main/documentation/SlimFaas.png)

A Vanilla JS project to save the planet. SlimFaas (https://github.com/AxaFrance/slimfaas) is the slimest and simplest Function As A Service on Kubernetes.
It works as a proxy that you can be deployed in your namespace.

SlimFaas API can give to the frontend information about the infrastructure state. **It is a mind changer !**

**Why?**

Because in production instead of setting up 2 replicas of your API backend, you can set up 0 replicas and use an UX that will show the user that the backend is down instead !
**@axa-fr/slimfaas-planet-saver** is here to for doing that easily.

![SlimFaasPlanetSaver.gif](https://github.com/AxaFrance/SlimFaas/blob/main/documentation/SlimfaasPlanetSaver.gif)

## Getting Started

```javascript
npm install @axa-fr/slimfaas-planet-saver
```

Example usage with react :
```javascript
import React, { useState, useEffect } from 'react';
import SlimFaasPlanetSaver from "@axa-fr/slimfass-planet-saver";

const PlanetSaver = ({ children, baseUrl, fetch }) => {
const [isFirstStart, setIsFirstStart] = useState(true); // State for first start

useEffect(() => {
if (!baseUrl) return;

const environmentStarter = new SlimFaasPlanetSaver(baseUrl, {
interval: 2000,
fetch,
updateCallback: (data) => {
const allReady = data.every((item) => item.NumberReady >= 1);
if (allReady && isFirstStart) {
setIsFirstStart(false);
}
},
errorCallback: (error) => {
console.error('Error detected :', error);
environmentStarter.updateOverlayMessage('An error occured when starting environment. Please contact an administrator.');
},
overlayMessage: 'Starting the environment...',
});

// Start polling
environmentStarter.startPolling();

// Cleanup
return () => environmentStarter.cleanup();
}, [baseUrl, isFirstStart]);

// During the first start, display a loading message
if (isFirstStart) {
return null;
}

// Once the environment is started, display the children
return <>{children}</>;

};

export default PlanetSaver;

```

## Run the demo

```javascript
git clone https://github.com/AxaFrance/slimfaas.git
cd slimfaas/src/SlimFaasPlanetSaver
npm i
npm run dev
```
38 changes: 38 additions & 0 deletions src/SlimFaasPlanetSaver/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import js from '@eslint/js'
import globals from 'globals'
import react from 'eslint-plugin-react'
import reactHooks from 'eslint-plugin-react-hooks'
import reactRefresh from 'eslint-plugin-react-refresh'

export default [
{ ignores: ['dist'] },
{
files: ['**/*.{js,jsx}'],
languageOptions: {
ecmaVersion: 2020,
globals: globals.browser,
parserOptions: {
ecmaVersion: 'latest',
ecmaFeatures: { jsx: true },
sourceType: 'module',
},
},
settings: { react: { version: '18.3' } },
plugins: {
react,
'react-hooks': reactHooks,
'react-refresh': reactRefresh,
},
rules: {
...js.configs.recommended.rules,
...react.configs.recommended.rules,
...react.configs['jsx-runtime'].rules,
...reactHooks.configs.recommended.rules,
'react/jsx-no-target-blank': 'off',
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
},
},
]
14 changes: 14 additions & 0 deletions src/SlimFaasPlanetSaver/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="icon" type="image/png" href="/AXA.png" />
<title>SlimFaas Planet Saver</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.jsx"></script>
</body>
</html>
Loading

0 comments on commit a43108f

Please sign in to comment.