Skip to content

Commit

Permalink
Centralize frontend API endpoints and update with versioned apis (#493)
Browse files Browse the repository at this point in the history
  • Loading branch information
mamy-CS authored Sep 17, 2024
2 parents 6a6d1a8 + 83e79dc commit 14c5d4b
Show file tree
Hide file tree
Showing 11 changed files with 311 additions and 220 deletions.
1 change: 1 addition & 0 deletions frontend/.env
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ NODE_PATH=src/
SKIP_PREFLIGHT_CHECK=true
REACT_APP_DEBUG_TORNJAK=true
REACT_APP_TORNJAK_MANAGER=false
REACT_APP_API_VERSION=v1

##### Backend Server uri
REACT_APP_API_SERVER_URI=http://localhost:10000/
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/components/agent-create-join-token.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { serverSelectedFunc } from 'redux/actions';
import { RootState } from 'redux/reducers';
import { ToastContainer } from "react-toastify"
import { showResponseToast, showToast } from './error-api';
import apiEndpoints from './apiConfig';

type CreateJoinTokenProp = {
globalServerSelected: string,
Expand Down Expand Up @@ -102,7 +103,7 @@ class CreateJoinToken extends Component<CreateJoinTokenProp, CreateJoinTokenStat

getApiTokenEndpoint(): string {
if (!IsManager) {
return GetApiServerUri('/api/agent/createjointoken')
return GetApiServerUri(apiEndpoints.spireJoinTokenApi)
} else if (IsManager && this.state.selectedServer !== "") {
return GetApiServerUri('/manager-api/agent/createjointoken') + "/" + this.state.selectedServer
} else {
Expand Down
18 changes: 18 additions & 0 deletions frontend/src/components/apiConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {env} from '../env';

const API_VERSION = env.REACT_APP_API_VERSION;
const API_BASE_URL = `/api/${API_VERSION}`;
const apiEndpoints = {
spireServerInfoApi: `${API_BASE_URL}/spire/serverinfo`,
spireHealthCheckApi: `${API_BASE_URL}/spire/healthcheck`,
spireAgentsApi: `${API_BASE_URL}/spire/agents`,
spireAgentsBanApi: `${API_BASE_URL}/spire/agents/ban`,
spireJoinTokenApi: `${API_BASE_URL}/spire/agents/jointoken`,
spireEntriesApi: `${API_BASE_URL}/spire/entries`,
tornjakServerInfoApi: `${API_BASE_URL}/tornjak/serverinfo`,
tornjakSelectorsApi: `${API_BASE_URL}/tornjak/selectors`,
tornjakAgentsApi: `${API_BASE_URL}/tornjak/agents`,
tornjakClustersApi: `${API_BASE_URL}/tornjak/clusters`,
};

export default apiEndpoints;
3 changes: 2 additions & 1 deletion frontend/src/components/cluster-create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
TornjakServerInfo
} from './types'
import { showResponseToast, showToast } from './error-api';
import apiEndpoints from './apiConfig';

type ClusterCreateProp = {
// dispatches a payload for the server trust domain and nodeAttestorPlugin as a ServerInfoType and has a return type of void
Expand Down Expand Up @@ -205,7 +206,7 @@ class ClusterCreate extends Component<ClusterCreateProp, ClusterCreateState> {

getApiEntryCreateEndpoint(): string {
if (!IsManager) {
return GetApiServerUri('/api/tornjak/clusters/create')
return GetApiServerUri(apiEndpoints.tornjakClustersApi)
} else if (IsManager && this.state.selectedServer !== "") {
return GetApiServerUri('/manager-api/tornjak/clusters/create') + "/" + this.state.selectedServer
} else {
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/components/cluster-edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
DebugServerInfo,
} from './types';
import { showResponseToast, showToast } from './error-api';
import apiEndpoints from './apiConfig';

type ClusterEditProp = {
// tornjak server debug info of the selected server
Expand Down Expand Up @@ -259,7 +260,7 @@ class ClusterEdit extends Component<ClusterEditProp, ClusterEditState> {

getApiEntryCreateEndpoint(): string {
if (!IsManager) {
return GetApiServerUri('/api/tornjak/clusters/edit')
return GetApiServerUri(apiEndpoints.tornjakClustersApi)
}
if (IsManager && this.state.selectedServer !== "") {
return GetApiServerUri('/manager-api/tornjak/clusters/edit') + "/" + this.state.selectedServer
Expand Down Expand Up @@ -342,7 +343,7 @@ class ClusterEdit extends Component<ClusterEditProp, ClusterEditState> {
return
}

axios.post(endpoint, cjtData)
axios.patch(endpoint, cjtData)
.then(
res => this.setState({
message: "Request:" + JSON.stringify(cjtData, null, ' ') + "\n\nSuccess:" + JSON.stringify(res.data, null, ' '),
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/components/entry-create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import EntryExpiryFeatures from './entry-expiry-features';
import CreateEntryJson from './entry-create-json';
import { ToastContainer } from "react-toastify"
import { showResponseToast, showToast } from './error-api';
import apiEndpoints from './apiConfig';
// import PropTypes from "prop-types"; // needed for testing will be removed on last pr

type CreateEntryProp = {
Expand Down Expand Up @@ -573,7 +574,7 @@ class CreateEntry extends Component<CreateEntryProp, CreateEntryState> {

getApiEntryCreateEndpoint(): string {
if (!IsManager) {
return GetApiServerUri('/api/entry/create')
return GetApiServerUri(apiEndpoints.spireEntriesApi)
}
if (IsManager && this.state.selectedServer !== "") {
return GetApiServerUri('/manager-api/entry/create') + "/" + this.state.selectedServer
Expand Down
Loading

0 comments on commit 14c5d4b

Please sign in to comment.