Skip to content

Commit

Permalink
Add configurable port to website, #3. (PR #18)
Browse files Browse the repository at this point in the history
* Add configurable port to website, #3.

* Allow customize service port from website.

* Detect invalid port configuration and provide default port.

Signed-off-by: Pan Li <[email protected]>
  • Loading branch information
Incarnation-p-lee authored Jun 22, 2018
1 parent 0f1fcfc commit 0988cb0
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 54 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
<artifactId>applicationinsights-core</artifactId>
<version>${azure.applicationinsights.version}</version>
</dependency>
</dependencies>
</dependencies>

<dependencyManagement>
<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private void writeKubernetesFile(File dir, ProjectRequest request){
writeText(new File(dir, KUBERNETES_FILE), templateRenderer.process(Paths.get(DOCKER_PATH, KUBERNETES_FILE).toString(), model));
}

private void generateDockerStructure(@NonNull File rootDir, @NonNull String baseDir, Map<String, Boolean> modulesModel, ProjectRequest request) {
private void generateDockerStructure(@NonNull File rootDir, @NonNull String baseDir, Map<String, Object> modulesModel, ProjectRequest request) {
final File dockerDir = Paths.get(rootDir.getPath(),baseDir, "docker").toFile();
final String template = "docker";
final String dockerComposeName = "docker-compose.yml";
Expand All @@ -99,6 +99,8 @@ private void generateDockerStructure(@NonNull File rootDir, @NonNull String base

dockerDir.mkdir();

request.getModules().forEach(m -> modulesModel.put(m.getName() + "-" + "port", m.getPort()));

writeText(new File(dockerDir, dockerComposeName), templateRenderer.process(Paths.get(template, dockerComposeName).toString(), modulesModel));
writeText(new File(dockerDir, runBash), templateRenderer.process(Paths.get(template, runBash).toString(), null));
writeText(new File(dockerDir, runCmd), templateRenderer.process(Paths.get(template, runCmd).toString(), null));
Expand All @@ -116,16 +118,15 @@ public File generateProjectStructure(ProjectRequest request) {
try {
Map<String, Object> model = resolveModel(request);
File rootDir = generateProjectStructure(request, model);
final Map<String, Object> modulesModel = new HashMap<>();

final Map<String, Boolean> modulesModel = new HashMap<>();
for(ProjectRequest subModule: request.getModules()){
generateProjectModuleStructure(subModule, resolveModel(subModule),
new File(rootDir, request.getBaseDir()), request);
final File subModuleDir = new File(rootDir, request.getBaseDir());
generateProjectModuleStructure(subModule, resolveModel(subModule), subModuleDir, request);
modulesModel.put(subModule.getName(), Boolean.TRUE);
}

generateDockerStructure(rootDir, request.getBaseDir(), modulesModel, request);
//publishProjectGeneratedEvent(request);

return rootDir;
}
Expand All @@ -135,15 +136,29 @@ public File generateProjectStructure(ProjectRequest request) {
}
}

private void servicesPortsUpdate(@NonNull ProjectRequest request) {
Assert.isNull(request.getParent(), "should be parent project.");

final Map<String, Integer> servicesPortsMap = request.getServicesPortsMap();
final List<String> services = request.getServices();

final long configuredCount = services.stream().filter(s -> servicesPortsMap.get(s) != null).count();

if (configuredCount < services.size()) {
servicesPortsMap.forEach((key, value) -> servicesPortsMap.put(key, ServiceMetadata.portMap.get(key)));
}

request.getModules().forEach(m -> m.setPort(servicesPortsMap.get(m.getName())));
}

/**
* Generate a project structure for the specified {@link ProjectRequest} and resolved
* model.
* @param request the project request
* @param model the source model
* @return the generated project structure
*/
protected File generateProjectStructure(ProjectRequest request,
Map<String, Object> model) {
protected File generateProjectStructure(ProjectRequest request, Map<String, Object> model) {
File rootDir;
try {
rootDir = File.createTempFile("tmp", "", getTemporaryDirectory());
Expand Down Expand Up @@ -171,6 +186,7 @@ protected File generateProjectStructure(ProjectRequest request,
}

generateGitIgnore(dir, request);
servicesPortsUpdate(request);

return rootDir;
}
Expand All @@ -193,16 +209,17 @@ protected File generateProjectModuleStructure(ProjectRequest request,
writeText(new File(dir, "settings.gradle"), settings);
}
else {
// Write Dockerfile to module
final String dockerFileName = "Dockerfile";
final String exposePortVariable = "EXPOSE_PORT";
final String dockerTemplateDir = "docker";
final Map<String, Integer> portModel = new HashMap<>();
portModel.put(exposePortVariable, ServiceMetadata.portMap.get(request.getName()));
final String path = Paths.get(dockerTemplateDir, dockerFileName).toString();

portModel.put(exposePortVariable, request.getPort());
writeText(new File(dir, dockerFileName), templateRenderer.process(path, portModel));

// Write Dockerfile to module
log.info("Writing Dockerfile to module " + request.getName());
writeText(new File(dir, dockerFileName),
templateRenderer.process(Paths.get(dockerTemplateDir, dockerFileName).toString(), portModel));

// Write pom file
String pom = new String(doGenerateMavenPom(model, "module-pom.xml"));
Expand All @@ -216,13 +233,10 @@ protected File generateProjectModuleStructure(ProjectRequest request,
request.getPackageName().replace(".", "/"));
src.mkdirs();
if(request.getName().equalsIgnoreCase("cloud-hystrix-dashboard")){
write(new File(src, applicationName + "." + language),
"HystrixDashboardApplication." + language, model);
write(new File(src, "MockStreamServlet.java"),
"MockStreamServlet.java", model);
write(new File(src, applicationName + "." + language), "HystrixDashboardApplication." + language, model);
write(new File(src, "MockStreamServlet.java"), "MockStreamServlet.java", model);
} else {
write(new File(src, applicationName + "." + language),
"Application." + language, model);
write(new File(src, applicationName + "." + language), "Application." + language, model);
}

if (request.getDependencies().contains("web")) {
Expand Down Expand Up @@ -299,7 +313,7 @@ private void writePropertiesFile(ProjectRequest request, File resourceDir, Proje
model.put("services", azureServices);
} else if(!ModulePropertiesResolver.isInfraModule(moduleName)){
model.put("applicationName", module.getName());
model.put("port", getPort(module.getName()));
model.put("port", request.getPort());
}

String content = templateRenderer.process(templateFile, model);
Expand All @@ -320,10 +334,6 @@ private void writeBootstrapYaml(ProjectRequest request, File resourceDir) {
writeText(new File(resourceDir, "bootstrap.yml"), content);
}

private int getPort(String serviceName) {
return ServiceMetadata.portMap.get(serviceName);
}

/**
* Create a distribution file for the specified project structure directory and
* extension.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
import com.microsoft.azure.springcloudplayground.util.Version;
import com.microsoft.azure.springcloudplayground.util.VersionProperty;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.lang.NonNull;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;

@NoArgsConstructor
public class ProjectRequest extends BasicProjectRequest {

/**
Expand Down Expand Up @@ -44,6 +44,13 @@ public class ProjectRequest extends BasicProjectRequest {
@Setter
private List<String> facets = new ArrayList<>();

@Getter
private Map<String, Integer> servicesPortsMap;

@Getter
@Setter
private Integer port;

private List<String> services = new ArrayList<>();

private final Map<String, ProjectRequest> subModules = new HashMap<>();
Expand All @@ -52,13 +59,23 @@ public class ProjectRequest extends BasicProjectRequest {
@Setter
private String build;

public ProjectRequest() {
servicesPortsMap = new HashMap<>();
}

public ProjectRequest(ProjectRequest parentProject) {
super(parentProject);
this.resolvedDependencies = parentProject.resolvedDependencies;
this.facets = parentProject.facets;
this.build = parentProject.build;
}

public Integer getServicePort(@NonNull String serviceName) {
Assert.isNull(this.getParent(), "should be parent module of Request");

return this.servicesPortsMap.get(serviceName);
}

public void removeDependency(String id) {
if (this.resolvedDependencies != null) {
Predicate<Dependency> filter = d -> d.getId().equals(id);
Expand Down Expand Up @@ -149,8 +166,7 @@ public void resolve(GeneratorMetadata metadata) {
}

if (!StringUtils.hasText(getApplicationName())) {
setApplicationName(
metadata.getConfiguration().generateApplicationName(getName()));
setApplicationName(metadata.getConfiguration().generateApplicationName(getName()));
}
setPackageName(metadata.getConfiguration().cleanPackageName(getPackageName(),
metadata.getPackageName().getContent()));
Expand Down Expand Up @@ -203,8 +219,7 @@ private void resolveBom(GeneratorMetadata metadata, String bomId,
if (!this.boms.containsKey(bomId)) {
BillOfMaterials bom = metadata.getConfiguration().getEnv().getBoms()
.get(bomId).resolve(requestedVersion);
bom.getAdditionalBoms()
.forEach((id) -> resolveBom(metadata, id, requestedVersion));
bom.getAdditionalBoms().forEach((id) -> resolveBom(metadata, id, requestedVersion));
this.boms.put(bomId, bom);
}
}
Expand Down Expand Up @@ -265,4 +280,24 @@ public Collection<ProjectRequest> getModules() {
public void addModule(ProjectRequest module) {
this.subModules.put(module.getName(), module);
}

public void setCloudConfigServerPort(@NonNull Integer port) {
this.servicesPortsMap.put("cloud-config-server", port);
}

public void setCloudGatewayPort(@NonNull Integer port) {
this.servicesPortsMap.put("cloud-gateway", port);
}

public void setCloudEurekaServerPort(@NonNull Integer port) {
this.servicesPortsMap.put("cloud-eureka-server", port);
}

public void setCloudHystrixDashboardPort(@NonNull Integer port) {
this.servicesPortsMap.put("cloud-hystrix-dashboard", port);
}

public void setAzureServiceBusPort(@NonNull Integer port) {
this.servicesPortsMap.put("azure-service-bus", port);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,53 @@

import com.microsoft.azure.springcloudplayground.metadata.Describable;
import com.microsoft.azure.springcloudplayground.metadata.MetadataElement;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.Setter;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@NoArgsConstructor
public class ServiceModule extends MetadataElement implements Describable {

@Setter
private String description;

@Getter
private String portName;

@Override
public String getDescription() {
return this.description;
}

public ServiceModule(ServiceModule module) {
super(module);
this.description = module.description;
}

public ServiceModule(String id, String name, String description) {
super(id, name);
this.description = description;
this.portName = toPortName(id);
}

private String toPortName(@NonNull String id) {
final List<String> tails = new ArrayList<>(Arrays.asList(id.split("-")));
final String head = tails.remove(0);
final String tail = String.join("", tails.stream().map(StringUtils::capitalize).toArray(String[]::new));

return head + tail + "Port";
}

@Override
public void setId(@NonNull String id) {
this.id = id;
this.portName = toPortName(id);
}

@Override
public String toString() {
return "ServiceModule{" + "id='" + getId() + '\'' + ", name='" + getName()
+ '\'' + ", description='" + this.getDescription() + '\'' + '}';
}

public static ServiceModule withId(String id, String name, String description) {
ServiceModule module = new ServiceModule();

module.setId(id);
module.setName(name);
module.setDescription(description);

return module;
}
}
2 changes: 1 addition & 1 deletion src/main/resources/static/css/customize.css
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
padding-left: 20px;
}

.module-checkbox input {
.module-checkbox input:first-child {
margin-left: -20px;
}

Expand Down
18 changes: 9 additions & 9 deletions src/main/resources/templates/docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,48 @@ services:
config-server:
image: "demo/cloud-config-server"
ports:
- "8888:8888"
- "{{cloud-config-server-port}}:{{cloud-config-server-port}}"
healthcheck:
test: ["CMD", "curl", "-f", "http://config-server:8888/gateway-server.yml"]
test: ["CMD", "curl", "-f", "http://config-server:{{cloud-config-server-port}}/gateway-server.yml"]
interval: 5s
retries: 20
{{/cloud-config-server}}
{{#cloud-eureka-server}}
eureka-server:
image: "demo/cloud-eureka-server"
ports:
- "8761:8761"
- "{{cloud-eureka-server-port}}:{{cloud-eureka-server-port}}"
links:
- config-server
depends_on:
config-server:
condition: service_healthy
healthcheck:
test: ["CMD", "curl", "-f", "http://eureka-server:8761"]
test: ["CMD", "curl", "-f", "http://eureka-server:{{cloud-eureka-server-port}}"]
interval: 5s
retries: 20
{{/cloud-eureka-server}}
{{#azure-service-bus}}
service-bus-server:
image: "demo/azure-service-bus"
ports:
- "8081:8081"
- "{{azure-service-bus-port}}:{{azure-service-bus-port}}"
links:
- eureka-server
- config-server
depends_on:
eureka-server:
condition: service_healthy
healthcheck:
test: ["CMD", "curl", "-f", "http://service-bus-server:8081/azure-service-bus/actuator/health"]
test: ["CMD", "curl", "-f", "http://service-bus-server:{{azure-service-bus-port}}/azure-service-bus/actuator/health"]
interval: 5s
retries: 20
{{/azure-service-bus}}
{{#cloud-hystrix-dashboard}}
hystrix-server:
image: "demo/cloud-hystrix-dashboard"
ports:
- "7979:7979"
- "{{cloud-hystrix-dashboard-port}}:{{cloud-hystrix-dashboard-port}}"
links:
- eureka-server
- config-server
Expand All @@ -60,9 +60,9 @@ services:
- eureka-server
- config-server
ports:
- "9999:9999"
- "{{cloud-gateway-port}}:{{cloud-gateway-port}}"
healthcheck:
test: ["CMD", "curl", "-f", "http://gateway-server:8080/actuator/health"]
test: ["CMD", "curl", "-f", "http://gateway-server:{{cloud-gateway-port}}/actuator/health"]
interval: 5s
retries: 20
{{/cloud-gateway}}
Loading

0 comments on commit 0988cb0

Please sign in to comment.