Added version display
- Added error route handling
This commit is contained in:
@ -0,0 +1,19 @@
|
||||
package de.w665.sharepulse.rest;
|
||||
|
||||
import org.springframework.boot.web.servlet.error.ErrorController;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* This class is used to redirect the user to the home page if an endpoint is not found.
|
||||
*/
|
||||
@RestController
|
||||
public class ErrorRestController implements ErrorController {
|
||||
@RequestMapping("/error")
|
||||
public String handleError() {
|
||||
return "<script>" +
|
||||
"console.log('Page not found. Redirecting to /home...');" +
|
||||
"window.location.href = window.location.origin + '/home';" +
|
||||
"</script>";
|
||||
}
|
||||
}
|
37
src/main/java/de/w665/sharepulse/rest/mappings/Version.java
Normal file
37
src/main/java/de/w665/sharepulse/rest/mappings/Version.java
Normal file
@ -0,0 +1,37 @@
|
||||
package de.w665.sharepulse.rest.mappings;
|
||||
|
||||
import com.rethinkdb.net.Response;
|
||||
import de.w665.sharepulse.rest.ApiRestController;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.jar.Attributes;
|
||||
import java.util.jar.Manifest;
|
||||
|
||||
@RestController
|
||||
public class Version extends ApiRestController {
|
||||
|
||||
private final ResourceLoader resourceLoader;
|
||||
|
||||
@Autowired
|
||||
public Version(ResourceLoader resourceLoader) {
|
||||
this.resourceLoader = resourceLoader;
|
||||
}
|
||||
|
||||
@GetMapping("/version")
|
||||
public ResponseEntity<Object> getVersion() throws Exception {
|
||||
Resource resource = resourceLoader.getResource("classpath:META-INF/MANIFEST.MF");
|
||||
Manifest manifest = new Manifest(resource.getInputStream());
|
||||
Attributes attr = manifest.getMainAttributes();
|
||||
String version = attr.getValue("Application-Version");
|
||||
String response = version != null ? version : "Version is only available in production builds";
|
||||
return new ResponseEntity<>(response, HttpStatus.OK);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user