Skip to content

Commit 8918aa0

Browse files
author
Alain BOUDARD
committed
fix: first providers to serve manifests
1 parent cbd99d5 commit 8918aa0

21 files changed

+411
-4
lines changed

application/ng-shell/src/app/app.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ export class AppComponent implements OnInit {
2424
}
2525

2626
async ngOnInit() {
27-
await loadManifest("http://localhost:8080/ng-shell/api/manifest")
27+
await loadManifest("http://localhost:8080/ng-shell/me/manifests/all");
28+
// await loadManifest("http://localhost:8080/ng-shell/api/manifests");
2829
const httpManifest = getManifest<CustomManifest>();
2930
const routes = buildRoutes(httpManifest);
3031
this.router.resetConfig(routes);

application/src/main/java/com/cit/application/controllers/HelloController.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.cit.application.controllers;
22

3+
import java.util.Map;
4+
35
import org.springframework.beans.factory.annotation.Value;
46
import org.springframework.web.bind.annotation.GetMapping;
57
import org.springframework.web.bind.annotation.RestController;
@@ -41,6 +43,35 @@ public MicroFrontend microfontend() {
4143
return new MicroFrontend(mfe1);
4244
}
4345

46+
@GetMapping("/api/manifests")
47+
public MicroFrontend[] microfontends() {
48+
Manifest mfe1 = new Manifest(remoteEntry, exposedModule, displayName, routePath, ngModuleName, type);
49+
Manifest mfe2 = new Manifest(remoteEntry, "./Plans", "Plans", "/plans", "PlansModule", "module");
50+
return new MicroFrontend[] {new MicroFrontend(mfe1), new MicroFrontend(mfe2)};
51+
}
52+
53+
@GetMapping("/api/manifests2")
54+
public Map<String,Object> mfe() {
55+
return Map.of(
56+
"orders",
57+
Map.of(
58+
"remoteEntry", remoteEntry,
59+
"exposedModule", exposedModule,
60+
"displayName", displayName,
61+
"routePath", routePath,
62+
"ngModuleName", ngModuleName,
63+
"type", type),
64+
"plans",
65+
Map.of(
66+
"remoteEntry", remoteEntry,
67+
"exposedModule", "./plans",
68+
"displayName", "Plans",
69+
"routePath", "plans",
70+
"ngModuleName", "PlansModule",
71+
"type", "module")
72+
);
73+
}
74+
4475
public static class MicroFrontend {
4576
private Manifest mfe1;
4677
public MicroFrontend(Manifest mfe1) {

core/pom.xml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>org.springframework</groupId>
8+
<artifactId>module-federation</artifactId>
9+
<version>0.0.1-SNAPSHOT</version>
10+
</parent>
11+
12+
<groupId>com.cit</groupId>
13+
<artifactId>core</artifactId>
14+
15+
<properties>
16+
<maven.compiler.source>11</maven.compiler.source>
17+
<maven.compiler.target>11</maven.compiler.target>
18+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19+
</properties>
20+
<dependencies>
21+
<dependency>
22+
<groupId>org.springframework</groupId>
23+
<artifactId>spring-context</artifactId>
24+
<version>5.3.30</version>
25+
<scope>compile</scope>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.springframework</groupId>
29+
<artifactId>spring-web</artifactId>
30+
<version>5.3.30</version>
31+
<scope>compile</scope>
32+
</dependency>
33+
</dependencies>
34+
35+
</project>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.cit.core;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
import org.springframework.stereotype.Component;
7+
8+
@Component
9+
public class CoreManifestProvider implements ManifestProvider {
10+
11+
@Override
12+
public String getName() {
13+
return "core";
14+
}
15+
16+
@Override
17+
public List<String> getNames() {
18+
return List.of("users", "orders");
19+
}
20+
21+
// fake data
22+
@Override
23+
public Map<String, Manifest> getManifests() {
24+
// empty
25+
return Map.of();
26+
/*return Map.of(
27+
"users",
28+
new Manifest(
29+
"http://localhost:8080/ng-shell/core/remoteEntry.js",
30+
"./users",
31+
"Users",
32+
"users",
33+
"UsersModule",
34+
"module"
35+
)
36+
);*/
37+
}
38+
39+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.cit.core;
2+
3+
public class Manifest {
4+
private String remoteEntry;
5+
private String exposedModule;
6+
private String displayName;
7+
private String routePath;
8+
private String ngModuleName; // TODO find a more generic name for next versions with routes
9+
private String type;
10+
11+
public Manifest(
12+
String remoteEntry,
13+
String exposedModule,
14+
String displayName,
15+
String routePath,
16+
String ngModuleName,
17+
String type) {
18+
this.remoteEntry = remoteEntry;
19+
this.exposedModule = exposedModule;
20+
this.displayName = displayName;
21+
this.routePath = routePath;
22+
this.ngModuleName = ngModuleName;
23+
this.type = type;
24+
}
25+
26+
public String getRemoteEntry() {
27+
return remoteEntry;
28+
}
29+
30+
public void setRemoteEntry(String remoteEntry) {
31+
this.remoteEntry = remoteEntry;
32+
}
33+
34+
public String getExposedModule() {
35+
return exposedModule;
36+
}
37+
38+
public void setExposedModule(String exposedModule) {
39+
this.exposedModule = exposedModule;
40+
}
41+
42+
public String getDisplayName() {
43+
return displayName;
44+
}
45+
46+
public void setDisplayName(String displayName) {
47+
this.displayName = displayName;
48+
}
49+
50+
public String getRoutePath() {
51+
return routePath;
52+
}
53+
54+
public void setRoutePath(String routePath) {
55+
this.routePath = routePath;
56+
}
57+
58+
public String getNgModuleName() {
59+
return ngModuleName;
60+
}
61+
62+
public void setNgModuleName(String ngModuleName) {
63+
this.ngModuleName = ngModuleName;
64+
}
65+
66+
public String getType() {
67+
return type;
68+
}
69+
70+
public void setType(String type) {
71+
this.type = type;
72+
}
73+
74+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.cit.core;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
@RestController
12+
@RequestMapping("/me/manifests")
13+
public class ManifestController {
14+
15+
@Autowired
16+
private ManifestControllerHelper manifestControllerHelper;
17+
18+
@GetMapping
19+
public List<String> getCurrentUserManifests() {
20+
return List.of(manifestControllerHelper.getManifests());
21+
}
22+
23+
@GetMapping("/list")
24+
public List<String> getCurrentUserManifestsList() {
25+
return List.of(manifestControllerHelper.getManifestsList());
26+
}
27+
28+
@GetMapping("/all")
29+
public Map<String, Manifest> getAllManifests() {
30+
return manifestControllerHelper.getAllManifests();
31+
}
32+
33+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.cit.core;
2+
3+
import java.util.Collections;
4+
import java.util.List;
5+
import java.util.Map;
6+
import java.util.Set;
7+
import java.util.function.Function;
8+
import java.util.stream.Collectors;
9+
import java.util.stream.Stream;
10+
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.stereotype.Component;
13+
14+
@Component
15+
public class ManifestControllerHelper {
16+
17+
private Map<String, ManifestProvider> manifestProviderMap = Collections.emptyMap();
18+
private Map<List<String>, ManifestProvider> manifestProviderListMap = Collections.emptyMap();
19+
private Map<Map<String, Manifest>, ManifestProvider> manifestProvidersListMap = Collections.emptyMap();
20+
21+
22+
@Autowired
23+
void initializeProviders(List<ManifestProvider> manifestProviderList) {
24+
manifestProvidersListMap = manifestProviderList
25+
.stream()
26+
.collect(Collectors.toMap(ManifestProvider::getManifests, Function.identity()));
27+
}
28+
29+
@Autowired
30+
void initializeMap(List<ManifestProvider> manifestProviderList) {
31+
manifestProviderMap = manifestProviderList
32+
.stream()
33+
.collect(Collectors.toMap(ManifestProvider::getName, Function.identity()));
34+
}
35+
36+
@Autowired
37+
void initializeListMap(List<ManifestProvider> manifestProviderList) {
38+
// concatenate the getNames() of all ManifestProviders
39+
manifestProviderListMap = manifestProviderList
40+
.stream()
41+
.collect(Collectors.toMap(ManifestProvider::getNames, Function.identity()));
42+
}
43+
44+
public Map<String, Manifest> getAllManifests() {
45+
// Set<Map<String, Manifest>> tmp = manifestProvidersListMap.keySet();
46+
// Stream<Map.Entry<String, Manifest>> tmp2 = manifestProvidersListMap.keySet().stream().flatMap(map -> map.entrySet().stream());
47+
return manifestProvidersListMap.keySet().stream().flatMap(map -> map.entrySet().stream())
48+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
49+
}
50+
51+
public String[] getManifests() {
52+
return manifestProviderMap.keySet().toArray(new String[0]);
53+
}
54+
55+
public String[] getManifestsList() {
56+
return manifestProviderListMap.keySet().stream().flatMap(List::stream).toArray(String[]::new);
57+
}
58+
59+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.cit.core;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
public interface ManifestProvider {
7+
String getName();
8+
List<String> getNames();
9+
Map<String, Manifest> getManifests();
10+
}

library/mfe1/src/app/app-routing.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { RouterModule, Routes } from '@angular/router';
33

44
const routes: Routes = [
55
{
6-
path: 'orders', loadChildren: () => import('./orders/orders.module').then(m => m.OrdersModule) }
6+
path: 'orders', loadChildren: () => import('./orders/orders.module').then(m => m.OrdersModule) },
7+
{ path: 'plans', loadChildren: () => import('./plans/plans.module').then(m => m.PlansModule) }
78
];
89

910
@NgModule({

library/mfe1/src/app/app.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ <h1>{{title}}</h1>
22
<ul>
33
<li>Home</li>
44
<li><a routerLink="orders">Orders</a></li>
5+
<li><a routerLink="plans">Plans</a></li>
56
</ul>
67
<router-outlet></router-outlet>

0 commit comments

Comments
 (0)