Explorar o código

Merge branch '0.8.0-no-docker-dev-env' into dev

mergin
Alexander Belanger %!s(int64=4) %!d(string=hai) anos
pai
achega
90a16ec0ab

+ 3 - 3
.air.toml

@@ -7,11 +7,11 @@ tmp_dir = "tmp"
 
 
 [build]
 [build]
 # Just plain old shell command. You could use `make` as well.
 # Just plain old shell command. You could use `make` as well.
-cmd = "go build -o ./tmp/ready ./cmd/ready; go build -o ./tmp/migrate ./cmd/migrate; go build -o ./tmp/app ./cmd/app"
+cmd = "go build -o ./tmp/app ./cmd/app"
 # Binary file yields from `cmd`.
 # Binary file yields from `cmd`.
-bin = "tmp/migrate; tmp/app"
+bin = "tmp/app"
 # Customize binary.
 # Customize binary.
-full_bin = "tmp/migrate; tmp/app"
+full_bin = "tmp/app"
 # Watch these filename extensions.
 # Watch these filename extensions.
 include_ext = ["go", "mod", "sum", "html"]
 include_ext = ["go", "mod", "sum", "html"]
 # Ignore these filename extensions or directories.
 # Ignore these filename extensions or directories.

+ 2 - 0
.gitignore

@@ -55,3 +55,5 @@ override.tf.json
 # Ignore CLI configuration files
 # Ignore CLI configuration files
 .terraformrc
 .terraformrc
 terraform.rc
 terraform.rc
+
+tmp

+ 14 - 0
Makefile

@@ -0,0 +1,14 @@
+#!make
+include ./docker/.env
+export $(shell sed 's/=.*//' ./docker/.env)
+
+setup:
+	go mod download;
+	cd dashboard && npm install;
+	cd ../;
+
+run-server: 
+	air -c .air.toml
+
+run-frontend:
+	cd ./dashboard && npm run start

+ 4 - 0
dashboard/babel.config.json

@@ -0,0 +1,4 @@
+{
+  "plugins": ["lodash"],
+  "presets": ["@babel/preset-env", "@babel/preset-react", "@babel/preset-typescript"]
+}

A diferenza do arquivo foi suprimida porque é demasiado grande
+ 2360 - 45
dashboard/package-lock.json


+ 25 - 12
dashboard/package.json

@@ -4,14 +4,6 @@
   "private": true,
   "private": true,
   "dependencies": {
   "dependencies": {
     "@material-ui/core": "^4.11.3",
     "@material-ui/core": "^4.11.3",
-    "@types/d3-array": "^2.9.0",
-    "@types/d3-time-format": "^3.0.0",
-    "@types/js-yaml": "^4.0.1",
-    "@types/lodash": "^4.14.165",
-    "@types/markdown-to-jsx": "^6.11.3",
-    "@types/material-ui": "^0.21.8",
-    "@types/qs": "^6.9.5",
-    "@types/random-words": "^1.1.0",
     "@visx/axis": "^1.6.1",
     "@visx/axis": "^1.6.1",
     "@visx/curve": "^1.0.0",
     "@visx/curve": "^1.0.0",
     "@visx/event": "^1.3.0",
     "@visx/event": "^1.3.0",
@@ -27,6 +19,7 @@
     "axios": "^0.20.0",
     "axios": "^0.20.0",
     "brace": "^0.11.1",
     "brace": "^0.11.1",
     "clipboard": "^2.0.8",
     "clipboard": "^2.0.8",
+    "core-js": "^3.16.1",
     "d3-array": "^2.11.0",
     "d3-array": "^2.11.0",
     "d3-time-format": "^3.0.0",
     "d3-time-format": "^3.0.0",
     "dotenv": "^8.2.0",
     "dotenv": "^8.2.0",
@@ -41,24 +34,37 @@
     "react": "^16.13.1",
     "react": "^16.13.1",
     "react-ace": "^9.1.3",
     "react-ace": "^9.1.3",
     "react-dom": "^16.13.1",
     "react-dom": "^16.13.1",
+    "react-error-boundary": "^3.1.3",
     "react-modal": "^3.11.2",
     "react-modal": "^3.11.2",
     "react-router-dom": "^5.2.0",
     "react-router-dom": "^5.2.0",
     "react-table": "^7.7.0",
     "react-table": "^7.7.0",
+    "regenerator-runtime": "^0.13.9",
     "semver": "^7.3.5",
     "semver": "^7.3.5",
-    "styled-components": "^5.2.0",
-    "react-error-boundary": "^3.1.3"
+    "styled-components": "^5.2.0"
   },
   },
   "scripts": {
   "scripts": {
     "test": "echo \"Error: no test specified\" && exit 1",
     "test": "echo \"Error: no test specified\" && exit 1",
-    "start": "webpack-dev-server --host 0.0.0.0 --hot --inline --port 8080",
-    "build": "webpack"
+    "start": "webpack-dev-server",
+    "build": "NODE_ENV=\"production\" webpack",
+    "build-and-analyze": "ENABLE_ANALYZER=true NODE_ENV=\"production\" webpack"
   },
   },
   "devDependencies": {
   "devDependencies": {
+    "@babel/core": "^7.15.0",
+    "@babel/preset-env": "^7.15.0",
+    "@babel/preset-react": "^7.14.5",
+    "@babel/preset-typescript": "^7.15.0",
+    "@pmmmwh/react-refresh-webpack-plugin": "^0.4.3",
     "@testing-library/jest-dom": "^4.2.4",
     "@testing-library/jest-dom": "^4.2.4",
     "@testing-library/react": "^9.3.2",
     "@testing-library/react": "^9.3.2",
     "@testing-library/user-event": "^7.1.2",
     "@testing-library/user-event": "^7.1.2",
+    "@types/d3-array": "^2.9.0",
+    "@types/d3-time-format": "^3.0.0",
     "@types/jest": "^24.0.0",
     "@types/jest": "^24.0.0",
     "@types/js-base64": "^3.0.0",
     "@types/js-base64": "^3.0.0",
+    "@types/js-yaml": "^4.0.1",
+    "@types/lodash": "^4.14.165",
+    "@types/markdown-to-jsx": "^6.11.3",
+    "@types/material-ui": "^0.21.8",
     "@types/node": "^12.12.62",
     "@types/node": "^12.12.62",
     "@types/qs": "^6.9.5",
     "@types/qs": "^6.9.5",
     "@types/random-words": "^1.1.0",
     "@types/random-words": "^1.1.0",
@@ -70,14 +76,21 @@
     "@types/react-table": "^7.7.1",
     "@types/react-table": "^7.7.1",
     "@types/semver": "^7.3.5",
     "@types/semver": "^7.3.5",
     "@types/styled-components": "^5.1.3",
     "@types/styled-components": "^5.1.3",
+    "@types/terser-webpack-plugin": "^4.2.2",
+    "@types/webpack-dev-server": "^3.11.5",
+    "babel-loader": "^8.2.2",
+    "babel-plugin-lodash": "^3.3.4",
     "file-loader": "^6.1.0",
     "file-loader": "^6.1.0",
     "html-webpack-plugin": "^4.5.0",
     "html-webpack-plugin": "^4.5.0",
     "prettier": "2.2.1",
     "prettier": "2.2.1",
     "qs": "^6.9.4",
     "qs": "^6.9.4",
+    "react-refresh": "^0.10.0",
     "source-map-loader": "^1.1.0",
     "source-map-loader": "^1.1.0",
+    "terser-webpack-plugin": "^4.2.3",
     "ts-loader": "^8.0.4",
     "ts-loader": "^8.0.4",
     "typescript": "^4.1.2",
     "typescript": "^4.1.2",
     "webpack": "^4.44.2",
     "webpack": "^4.44.2",
+    "webpack-bundle-analyzer": "^4.4.2",
     "webpack-cli": "^3.3.12",
     "webpack-cli": "^3.3.12",
     "webpack-dev-server": "^3.11.0"
     "webpack-dev-server": "^3.11.0"
   }
   }

+ 3 - 0
dashboard/src/index.tsx

@@ -2,6 +2,9 @@ import * as React from "react";
 import * as ReactDOM from "react-dom";
 import * as ReactDOM from "react-dom";
 import App from "./App";
 import App from "./App";
 
 
+import "core-js/stable";
+import "regenerator-runtime/runtime";
+
 declare global {
 declare global {
   interface Window {
   interface Window {
     analytics: any;
     analytics: any;

+ 94 - 7
dashboard/webpack.config.js

@@ -1,24 +1,48 @@
 const path = require("path");
 const path = require("path");
 const HtmlWebpackPlugin = require("html-webpack-plugin");
 const HtmlWebpackPlugin = require("html-webpack-plugin");
 const webpack = require("webpack");
 const webpack = require("webpack");
+const ReactRefreshWebpackPlugin = require("@pmmmwh/react-refresh-webpack-plugin");
+
 const dotenv = require("dotenv");
 const dotenv = require("dotenv");
 
 
+const BundleAnalyzerPlugin = require("webpack-bundle-analyzer")
+  .BundleAnalyzerPlugin;
+
+const TerserPlugin = require("terser-webpack-plugin");
+
 module.exports = () => {
 module.exports = () => {
   const env = dotenv.config().parsed;
   const env = dotenv.config().parsed;
   const envKeys = Object.keys(env).reduce((prev, next) => {
   const envKeys = Object.keys(env).reduce((prev, next) => {
     prev[`process.env.${next}`] = JSON.stringify(env[next]);
     prev[`process.env.${next}`] = JSON.stringify(env[next]);
     return prev;
     return prev;
   }, {});
   }, {});
-
-  return {
-    entry: "./src/index.tsx",
+  // Check first the env file and if it's empty, check out the node env of the process.
+  let isDevelopment = env.NODE_ENV !== "production";
+  if (process.env.NODE_ENV !== env.NODE_ENV) {
+    isDevelopment = process.env.NODE_ENV !== "production";
+  }
+  /**
+   * @type {webpack.Configuration}
+   */
+  const config = {
+    entry: ["regenerator-runtime/runtime.js", "./src/index.tsx"],
     target: "web",
     target: "web",
-    mode: "development",
+    mode: isDevelopment ? "development" : "production",
     module: {
     module: {
       rules: [
       rules: [
         {
         {
-          test: /\.(ts|tsx)$/,
-          loader: "ts-loader",
+          test: /\.(ts|tsx|mjs|js|jsx)$/,
+          exclude: /node_modules/,
+          use: [
+            {
+              loader: require.resolve("babel-loader"),
+              options: {
+                plugins: [
+                  isDevelopment && require.resolve("react-refresh/babel"),
+                ].filter(Boolean),
+              },
+            },
+          ],
         },
         },
         {
         {
           enforce: "pre",
           enforce: "pre",
@@ -57,6 +81,9 @@ module.exports = () => {
       port: env["PORT"],
       port: env["PORT"],
       historyApiFallback: true,
       historyApiFallback: true,
       disableHostCheck: true,
       disableHostCheck: true,
+      host: "0.0.0.0",
+      port: env.DEV_SERVER_PORT || 8080,
+      hot: true,
     },
     },
     plugins: [
     plugins: [
       new HtmlWebpackPlugin({
       new HtmlWebpackPlugin({
@@ -64,6 +91,66 @@ module.exports = () => {
         segmentKey: `${process.env.SEGMENT_PUBLIC_KEY}`,
         segmentKey: `${process.env.SEGMENT_PUBLIC_KEY}`,
       }),
       }),
       new webpack.DefinePlugin(envKeys),
       new webpack.DefinePlugin(envKeys),
-    ],
+      isDevelopment && new ReactRefreshWebpackPlugin(),
+    ].filter(Boolean),
   };
   };
+
+  if (!isDevelopment) {
+    config.optimization = {
+      minimize: true,
+      minimizer: [
+        new TerserPlugin({
+          test: /\.(ts|tsx|mjs|js|jsx)$/,
+          terserOptions: {
+            parse: {
+              // We want terser to parse ecma 8 code. However, we don't want it
+              // to apply minification steps that turns valid ecma 5 code
+              // into invalid ecma 5 code. This is why the `compress` and `output`
+              ecma: 8,
+            },
+            compress: {
+              ecma: 5,
+              warnings: false,
+              inline: 2,
+            },
+            mangle: {
+              // Find work around for Safari 10+
+              safari10: true,
+            },
+            output: {
+              ecma: 5,
+              comments: false,
+              ascii_only: true,
+            },
+          },
+
+          // Use multi-process parallel running to improve the build speed
+          parallel: true,
+        }),
+      ],
+    };
+  }
+
+  if (env.ENABLE_ANALYZER) {
+    config.plugins.push(new BundleAnalyzerPlugin());
+  }
+  console.log(env);
+  if (env.ENABLE_PROXY) {
+    console.log("WORKED!");
+    if (!env.API_SERVER) {
+      throw new Error(
+        "API_SERVER is not present on .env! Please setup the api server url if you want the proxy to work! API_SERVER example: http://localhost:8080"
+      );
+    }
+    config.devServer.proxy = {
+      "/api": {
+        logLevel: "debug",
+        target: env.API_SERVER, // target host
+        changeOrigin: true, // needed for virtual hosted sites
+        ws: true, // proxy websockets
+      },
+    };
+  }
+
+  return config;
 };
 };

+ 46 - 6
docs/developing/setup.md

@@ -22,9 +22,47 @@ DB_NAME=porter
 SQL_LITE=false
 SQL_LITE=false
 ```
 ```
 
 
-Once you've done this, go to the root repository, and run `docker-compose -f docker-compose.dev.yaml up`. You should see postgres, webpack, and porter containers spin up. When the webpack and porter containers have finished compiling and have spun up successfully (this will take 5-10 minutes after the containers start), you can navigate to `localhost:8080` and you should be greeted with the "Log In" screen. Create a user by entering an email/password on the "Register" screen. 
+Once you've done this, go to the root repository, and run `docker-compose -f docker-compose.dev.yaml up`. You should see postgres, webpack, and porter containers spin up. When the webpack and porter containers have finished compiling and have spun up successfully (this will take 5-10 minutes after the containers start), you can navigate to `localhost:8080` and you should be greeted with the "Log In" screen. Create a user by entering an email/password on the "Register" screen.
 
 
-At this point, you can make a change to any `.go` file to trigger a backend rebuild, and any file in `/dashboard/src` to trigger a hot reload. 
+At this point, you can make a change to any `.go` file to trigger a backend rebuild, and any file in `/dashboard/src` to trigger a hot reload.
+
+## Setup without docker
+
+While docker is an awesome way of getting started as it simulates the real environment that we use on our hosted dashboard, for some people this may bee too much.
+
+In order to decrease the complexity of all the environment, you can just run the development environment locally without docker.
+
+To do so you should follow the next steps:
+
+1- First, inside the folder where you cloned the repo (e.g: /home/user/porter) run `make setup`. This should download all the modules for go to work and install all the dependencies that the frontend needs to work!
+
+2- After installing everything you should update the env variables:
+The `/dashboard/.env` should look like this:
+
+```
+NODE_ENV=development
+DEV_SERVER_PORT=8081 # Tell the webpack dev server in wich port we wanna run, it defaults to 8080 but we have to be carefull this is not the same port as the backend
+ENABLE_PROXY=true # Usually we would use nginx, but for this environment we're going to enable webpack-dev-server proxy
+API_SERVER=http://localhost:8080 # API server url, this url will be used for the proxy to redirect all /api calls
+```
+
+And the `/docker/.env` variables should look like this:
+
+```
+SERVER_URL=http://localhost:8080
+SERVER_PORT=8080
+SQL_LITE=true
+SQL_LITE_PATH=./porter.db
+REDIS_ENABLED=false
+```
+
+In this case SQLLite is the simplest solution for getting the environment running, but you can enable postgres or redis if you want!
+
+3- Open two terminals and run `make run-server` and in the second one `make run-frontend`. This should get everything up and running!
+
+### Disclaimer
+
+This environment is experimental, if you run into any issue don't doubt in contact us through our [discord!](https://discord.gg/GJynMR3KXK)
 
 
 ## Getting PostgreSQL Access
 ## Getting PostgreSQL Access
 
 
@@ -34,7 +72,7 @@ You can get `psql` access by running the following:
 
 
 This will prompt you for a password. Enter `porter`, and you should see the `psql` shell!
 This will prompt you for a password. Enter `porter`, and you should see the `psql` shell!
 
 
-### Setting your email to be verified 
+### Setting your email to be verified
 
 
 If you are getting blocked out of the dashboard because your email is not verified (fixed in `v0.6.2` of Porter, so make sure you've pulled from `master` recently), you can update your email in the database to `verified":
 If you are getting blocked out of the dashboard because your email is not verified (fixed in `v0.6.2` of Porter, so make sure you've pulled from `master` recently), you can update your email in the database to `verified":
 
 
@@ -43,16 +81,18 @@ If you are getting blocked out of the dashboard because your email is not verifi
 ## Setting up Minikube
 ## Setting up Minikube
 
 
 These steps will help you get set up with a minikube cluster that can be used for development. Prerequisities:
 These steps will help you get set up with a minikube cluster that can be used for development. Prerequisities:
+
 - `kubectl` installed locally
 - `kubectl` installed locally
 - Development instance of Porter is running
 - Development instance of Porter is running
 
 
 Following the OS-specific steps to get minikube running:
 Following the OS-specific steps to get minikube running:
+
 - [MacOS](#macos)
 - [MacOS](#macos)
 - [Linux](#linux)
 - [Linux](#linux)
 
 
 If you now navigate to `http://localhost:8080`, you should see the minikube cluster attached! There will be some limitations:
 If you now navigate to `http://localhost:8080`, you should see the minikube cluster attached! There will be some limitations:
-- **It is not possible to expose a service that you create. Whenever you create a web service, de-select the "Expose to external traffic" option.**
 
 
+- **It is not possible to expose a service that you create. Whenever you create a web service, de-select the "Expose to external traffic" option.**
 
 
 ### MacOS
 ### MacOS
 
 
@@ -119,6 +159,6 @@ If using Chrome, paste the following into the Chrome address bar:
 
 
 > chrome://flags/#allow-insecure-localhost
 > chrome://flags/#allow-insecure-localhost
 
 
-And then Enable the **Allow invalid certificates for resources loaded from localhost** field. 
+And then Enable the **Allow invalid certificates for resources loaded from localhost** field.
 
 
-Finally, run `docker-compose -f docker-compose.dev-secure.yaml up` instead of the standard docker-compose file. 
+Finally, run `docker-compose -f docker-compose.dev-secure.yaml up` instead of the standard docker-compose file.

Algúns arquivos non se mostraron porque demasiados arquivos cambiaron neste cambio