Skip to content

Commit

Permalink
Configure website-service correctly serving angular website
Browse files Browse the repository at this point in the history
  • Loading branch information
MichiBaum committed Oct 17, 2024
1 parent 43543af commit 9a78f0a
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 12 deletions.
2 changes: 1 addition & 1 deletion website-service/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ target/
!**/src/test/**/target/

# That you can not accidentally commit this folder
src/main/resources/angular
src/main/resources/static
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.michibaum.websiteservice;

import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.resource.PathResourceResolver;

import java.io.IOException;

@Configuration
public class AngularConfig implements WebMvcConfigurer {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {

registry.addResourceHandler("/**")
.addResourceLocations("classpath:/static/")
.resourceChain(true)
.addResolver(new PathResourceResolver() {
@Override
protected Resource getResource(String resourcePath, Resource location) throws IOException {
Resource requestedResource = location.createRelative(resourcePath);
return requestedResource.exists() && requestedResource.isReadable() ? requestedResource
: new ClassPathResource("/static/index.html");
}
});

}
}
4 changes: 0 additions & 4 deletions website-service/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ spring:
include-description: true
main:
allow-bean-definition-overriding: true
web:
resources:
static-locations: classpath:/angular/browser


eureka:
instance:
Expand Down
5 changes: 4 additions & 1 deletion website/angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
"build": {
"builder": "@angular-devkit/build-angular:application",
"options": {
"outputPath": "../website-service/src/main/resources/angular",
"outputPath": {
"base": "../website-service/src/main/resources/static",
"browser": ""
},
"index": "src/index.html",
"browser": "src/main.ts",
"polyfills": [
Expand Down
5 changes: 3 additions & 2 deletions website/src/app/core/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import {ActivatedRouteSnapshot, Router} from "@angular/router";
import {Authentication, AuthenticationResponse} from "../models/authentication.model";
import {environment} from "../../../environments/environment";
import {Sides} from "../config/sides";
import {RouternavigationService} from "./router-navigation.service";

@Injectable({providedIn: 'root'})
export class AuthService {

constructor(private http: HttpClient, private router:Router) {
constructor(private http: HttpClient, private router:Router, private routernavigationService: RouternavigationService) {
}

login(username:string, password:string ) {
Expand Down Expand Up @@ -39,7 +40,7 @@ export class AuthService {
let jwtIsPresent = this.jwtIsPresent();

if (!jwtIsPresent) {
this.router.navigate(['/login']);
this.routernavigationService.login()
return false;
}

Expand Down
12 changes: 8 additions & 4 deletions website/src/app/core/services/router-navigation.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,30 @@ export class RouternavigationService {
}

home() {
this.router.navigate(['/home'], {skipLocationChange: true});
this.router.navigate(['/home']);
}

github() {
open('https://github.com/MichiBaum');
}

imprint() {
this.router.navigate(['/imprint'], { skipLocationChange: true });
this.router.navigate(['/imprint']);
}

chess() {
this.router.navigate(['/chess'], { skipLocationChange: true });
this.router.navigate(['/chess']);
}

open(url: string) {
window.open(url, '_blank');
}

microservices() {
this.router.navigate(['/microservices'], { skipLocationChange: true })
this.router.navigate(['/microservices'])
}

login() {
this.router.navigate(['/login']);
}
}

0 comments on commit 9a78f0a

Please sign in to comment.