Browse Source

First connection from ui to server

Kirk Trombley 5 năm trước cách đây
mục cha
commit
25f0220029
3 tập tin đã thay đổi với 44 bổ sung2 xóa
  1. 1 1
      server/app.py
  2. 32 1
      ui/src/App.js
  3. 11 0
      ui/src/services/ggsh.service.js

+ 1 - 1
server/app.py

@@ -1,4 +1,4 @@
-from flask import Flask
+from flask import Flask, jsonify
 from flask_cors import CORS
 import toml
 

+ 32 - 1
ui/src/App.js

@@ -1,10 +1,41 @@
 import React from 'react';
+import { getStatus } from "./services/ggsh.service";
 import './App.css';
 
-function App() {
+class InfoComponent extends React.Component {
+  constructor(props) {
+    super(props);
+    this.state = {
+      loading: true,
+      version: null,
+      status: null,
+    }
+  }
+
+  async componentDidMount() {
+    const { version, status } = await getStatus();
+    this.setState({loading: false, version, status});
+  }
+
+  render() {
+    if (this.state.loading) {
+      return <p>Connecting to back-end...</p>
+    }
+
+    if (this.state.status !== "healthy") {
+      return <p>Unable to communicate with API server! Error: {this.state.status}</p>
+    }
+    
+    return <p>API Version: {this.state.version}</p>
+  }
+}
+
+const App = () => {
   return (
     <div className="App">
       <p>TerrAssumptions!</p>
+      <hr/>
+      <InfoComponent/>
     </div>
   );
 }

+ 11 - 0
ui/src/services/ggsh.service.js

@@ -1,2 +1,13 @@
 import { API_BASE } from "../config";
 
+export const getStatus = async () => {
+    try {
+        const res = await fetch(API_BASE + "/");
+        if (!res.ok) {
+            throw Error(res.statusText);
+        }
+        return await res.json();
+    } catch (err) {
+        return {status: err.message, version: null}
+    }
+}