Skip to content

Commit

Permalink
Add a single page router that will redirect all missing requests to i…
Browse files Browse the repository at this point in the history
…ndex.html (jenkinsci#163)

* Add a single page router that will redirect all missing requests to index.html

* Handle API_URL being set to / by double adding slashes
  • Loading branch information
halkeye authored Sep 9, 2020
1 parent ec20a83 commit 4e44689
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
HELP.md
target/
.mvn/
!.mvn/wrapper/maven-wrapper.jar
.git
.cache
Expand Down Expand Up @@ -37,3 +38,5 @@ build/

# ignore node_modules
/node_modules/
frontend/build
frontend/node_modules
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ Once you have started the spring boot server the next thing is to configure the
* Inside this file this line needs to be changed `REACT_APP_API_URL=INSERT_NEW_PORT_HERE`
for eg: If the backend is running on port 8081
```
REACT_APP_API_URL=http://localhost:8081
REACT_APP_API_URL=http://localhost:8081/
```

## How to change ports and run with Docker
Expand Down Expand Up @@ -112,7 +112,7 @@ Once you have started the spring boot server the next thing is to configure the
* Inside this file this line needs to be changed `REACT_APP_API_URL=INSERT_NEW_PORT_HERE`
for eg: If the backend is running on port 8081
```
REACT_APP_API_URL=http://localhost:8081
REACT_APP_API_URL=http://localhost:8081/
```

## How to change Community-Configurations URL
Expand Down
2 changes: 1 addition & 1 deletion frontend/.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
REACT_APP_GITHUB_COMMUNITY_URL=https://api.github.com/repos/sladyn98/custom-distribution-service-community-configurations/contents/configurations
REACT_APP_API_URL=http://localhost:8080
REACT_APP_API_URL=http://localhost:8080/
2 changes: 1 addition & 1 deletion frontend/.env.docker
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
REACT_APP_API_URL=http://localhost:8080
REACT_APP_API_URL=http://localhost:8080/
REACT_APP_GITHUB_COMMUNITY_URL=https://api.github.com/repos/sladyn98/custom-distribution-service-community-configurations/contents/configurations
6 changes: 3 additions & 3 deletions frontend/src/components/PackageGeneration/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function saveData(blob, fileName) {
function getAPIURL () {
// Use the default API_URL
console.log("We are calling this function")
let API_URL = "http://localhost:8080"
let API_URL = "http://localhost:8080/"

// If environment variable has been set it will override the default
if (process.env.REACT_APP_API_URL) {
Expand Down Expand Up @@ -72,7 +72,7 @@ class editor extends React.Component {

downloadWarfile() {
var xhr = new XMLHttpRequest();
xhr.open("POST", getAPIURL() + '/package/downloadWarPackage', true);
xhr.open("POST", getAPIURL() + 'package/downloadWarPackage', true);
xhr.responseType = "blob";
xhr.onload = function () {
if(xhr.status == 404) {
Expand All @@ -86,7 +86,7 @@ class editor extends React.Component {

downloadPackagerConfig() {
var xhr = new XMLHttpRequest();
xhr.open("POST", getAPIURL() + '/package/downloadPackageConfiguration', true);
xhr.open("POST", getAPIURL() + 'package/downloadPackageConfiguration', true);
xhr.responseType = "blob";
xhr.onload = function () {
saveData(this.response, 'casc.yml');
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/components/layouts/CardLayout.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ class CardLayout extends React.Component {

async componentDidMount() {
// Use the default API_URL
let API_URL = "http://localhost:8080"
let API_URL = "http://localhost:8080/"

// If environment variable has been set it will override the default
if (process.env.REACT_APP_API_URL) {
console.log("Environment variable has been set")
API_URL = process.env.REACT_APP_API_URL
console.log("Environment variable has been set", API_URL)
}
const response = await fetch(API_URL + '/api/plugin/getPluginList');
const response = await fetch(API_URL + 'api/plugin/getPluginList');
const body = await response.json();
const mainBody = body["plugins"]
this.setState({ plugins: mainBody, isLoading: false});
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ class ModalExample extends React.Component {
this.setState({isLoading:true})

// Use the default API_URL
let API_URL = "http://localhost:8080"
let API_URL = "http://localhost:8080/"

// If environment variable has been set it will override the default
if (process.env.REACT_APP_API_URL) {
console.log("Environment variable has been set")
API_URL = process.env.REACT_APP_API_URL
}

const apiURL = API_URL + "/package/getPackageConfiguration";
const apiURL = API_URL + "package/getPackageConfiguration";
fetch(apiURL, {
method: 'POST',
headers: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.org.jenkins.custom.jenkins.distribution.service;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
class PageController {
// Fall through handler. Anything not otherwise matched, goto index.html
@GetMapping("/**/{path:[^\\.]*}")
public String forwardToIndex(final HttpServletRequest request) {
return "forward:/index.html";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.org.jenkins.custom.jenkins.distribution.service;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;


import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;

@SpringBootTest
@AutoConfigureMockMvc
class PageControllerTest {
@Autowired
private MockMvc mockMvc;

@Test
public void redirectsInternally() throws Exception {
this.mockMvc.perform(get("/pluginList")).andDo(print())
.andExpect(status().is(200))
.andExpect(view().name("forward:/index.html"));
}

}

0 comments on commit 4e44689

Please sign in to comment.