Skip to content

Commit

Permalink
style: add linter (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
rgomezcasas authored Nov 7, 2023
1 parent c65ab95 commit 64a6d44
Show file tree
Hide file tree
Showing 38 changed files with 853 additions and 763 deletions.
19 changes: 13 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
name: CI

on: [push]
on:
push:
branches:
- main
pull_request:

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v4

- name: Start all the environment
run: docker-compose up -d
- name: 🐳 Start all the environment
run: make start

- name: Wait for the environment to get up
- name: 🔦 Lint
run: make lint

- name: 🦭 Wait for the environment to get up
run: |
while ! make ping-mysql &>/dev/null; do
echo "Waiting for database connection..."
sleep 2
done
- name: Run the tests
- name: Run the tests
run: make test
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
FROM openjdk:21-slim-buster
WORKDIR /app

RUN apt update && apt install -y curl git

ENV NVM_DIR /usr/local/nvm
ENV NODE_VERSION 18.0.0

RUN mkdir -p $NVM_DIR
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

RUN . $NVM_DIR/nvm.sh && nvm install $NODE_VERSION && nvm alias default $NODE_VERSION && nvm use default

ENV NODE_PATH $NVM_DIR/versions/node/v$NODE_VERSION/lib/node_modules
ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH
16 changes: 5 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,35 +1,29 @@
.PHONY: all
all: build

.PHONY: up
up:
@docker-compose up -d
start:
@docker compose up -d

.PHONY: build
build:
@./gradlew build --warning-mode all

.PHONY: run-tests
lint:
@docker exec codelytv-ddd_example-java ./gradlew spotlessCheck

run-tests:
@./gradlew test --warning-mode all

.PHONY: test
test:
@docker exec codelytv-ddd_example-java ./gradlew test --warning-mode all

.PHONY: run
run:
@./gradlew :run

.PHONY: ping-mysql
ping-mysql:
@docker exec codelytv-java_ddd_example-mysql mysqladmin --user=root --password= --host "127.0.0.1" ping --silent

# Start the app
.PHONY: start-mooc_backend
start-mooc_backend:
@./gradlew bootRun --args='mooc_backend server'

.PHONY: start-backoffice_frontend
start-backoffice_frontend:
@./gradlew bootRun --args='backoffice_frontend server'
164 changes: 84 additions & 80 deletions apps/main/tv/codely/apps/Starter.java
Original file line number Diff line number Diff line change
@@ -1,92 +1,96 @@
package tv.codely.apps;

import java.util.Arrays;
import java.util.HashMap;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.context.ConfigurableApplicationContext;

import tv.codely.apps.backoffice.backend.BackofficeBackendApplication;
import tv.codely.apps.backoffice.frontend.BackofficeFrontendApplication;
import tv.codely.apps.mooc.backend.MoocBackendApplication;
import tv.codely.shared.infrastructure.cli.ConsoleCommand;

import java.util.Arrays;
import java.util.HashMap;

public class Starter {
public static void main(String[] args) {
if (args.length < 2) {
throw new RuntimeException("There are not enough arguments");
}

String applicationName = args[0];
String commandName = args[1];
boolean isServerCommand = commandName.equals("server");

ensureApplicationExist(applicationName);
ensureCommandExist(applicationName, commandName);

Class<?> applicationClass = applications().get(applicationName);

SpringApplication app = new SpringApplication(applicationClass);

if (!isServerCommand) {
app.setWebApplicationType(WebApplicationType.NONE);
}

ConfigurableApplicationContext context = app.run(args);

if (!isServerCommand) {
ConsoleCommand command = (ConsoleCommand) context.getBean(
commands().get(applicationName).get(commandName)
);

command.execute(Arrays.copyOfRange(args, 2, args.length));
}
}

private static void ensureApplicationExist(String applicationName) {
if (!applications().containsKey(applicationName)) {
throw new RuntimeException(String.format(
"The application <%s> doesn't exist. Valids:\n- %s",
applicationName,
String.join("\n- ", applications().keySet())
));
}
}

private static void ensureCommandExist(String applicationName, String commandName) {
if (!"server".equals(commandName) && !existCommand(applicationName, commandName)) {
throw new RuntimeException(String.format(
"The command <%s> for application <%s> doesn't exist. Valids (application.command):\n- api\n- %s",
commandName,
applicationName,
String.join("\n- ", commands().get(applicationName).keySet())
));
}
}

private static HashMap<String, Class<?>> applications() {
HashMap<String, Class<?>> applications = new HashMap<>();

applications.put("mooc_backend", MoocBackendApplication.class);
applications.put("backoffice_backend", BackofficeBackendApplication.class);
applications.put("backoffice_frontend", BackofficeFrontendApplication.class);

return applications;
}

private static HashMap<String, HashMap<String, Class<?>>> commands() {
HashMap<String, HashMap<String, Class<?>>> commands = new HashMap<>();

commands.put("mooc_backend", MoocBackendApplication.commands());
commands.put("backoffice_backend", BackofficeBackendApplication.commands());
commands.put("backoffice_frontend", BackofficeFrontendApplication.commands());

return commands;
}

private static Boolean existCommand(String applicationName, String commandName) {
HashMap<String, HashMap<String, Class<?>>> commands = commands();

return commands.containsKey(applicationName) && commands.get(applicationName).containsKey(commandName);
}

public static void main(String[] args) {
if (args.length < 2) {
throw new RuntimeException("There are not enough arguments");
}

String applicationName = args[0];
String commandName = args[1];
boolean isServerCommand = commandName.equals("server");

ensureApplicationExist(applicationName);
ensureCommandExist(applicationName, commandName);

Class<?> applicationClass = applications().get(applicationName);

SpringApplication app = new SpringApplication(applicationClass);

if (!isServerCommand) {
app.setWebApplicationType(WebApplicationType.NONE);
}

ConfigurableApplicationContext context = app.run(args);

if (!isServerCommand) {
ConsoleCommand command = (ConsoleCommand) context.getBean(commands().get(applicationName).get(commandName));

command.execute(Arrays.copyOfRange(args, 2, args.length));
}
}

private static void ensureApplicationExist(String applicationName) {
if (!applications().containsKey(applicationName)) {
throw new RuntimeException(
String.format(
"The application <%s> doesn't exist. Valids:\n- %s",
applicationName,
String.join("\n- ", applications().keySet())
)
);
}
}

private static void ensureCommandExist(String applicationName, String commandName) {
if (!"server".equals(commandName) && !existCommand(applicationName, commandName)) {
throw new RuntimeException(
String.format(
"The command <%s> for application <%s> doesn't exist. Valids (application.command):\n- api\n- %s",
commandName,
applicationName,
String.join("\n- ", commands().get(applicationName).keySet())
)
);
}
}

private static HashMap<String, Class<?>> applications() {
HashMap<String, Class<?>> applications = new HashMap<>();

applications.put("mooc_backend", MoocBackendApplication.class);
applications.put("backoffice_backend", BackofficeBackendApplication.class);
applications.put("backoffice_frontend", BackofficeFrontendApplication.class);

return applications;
}

private static HashMap<String, HashMap<String, Class<?>>> commands() {
HashMap<String, HashMap<String, Class<?>>> commands = new HashMap<>();

commands.put("mooc_backend", MoocBackendApplication.commands());
commands.put("backoffice_backend", BackofficeBackendApplication.commands());
commands.put("backoffice_frontend", BackofficeFrontendApplication.commands());

return commands;
}

private static Boolean existCommand(String applicationName, String commandName) {
HashMap<String, HashMap<String, Class<?>>> commands = commands();

return commands.containsKey(applicationName) && commands.get(applicationName).containsKey(commandName);
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
package tv.codely.apps.backoffice.backend;

import java.util.HashMap;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import tv.codely.shared.domain.Service;

import java.util.HashMap;
import tv.codely.shared.domain.Service;

@SpringBootApplication(exclude = HibernateJpaAutoConfiguration.class)
@ComponentScan(
includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Service.class),
value = {"tv.codely.shared", "tv.codely.backoffice", "tv.codely.apps.backoffice.backend"}
includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Service.class),
value = { "tv.codely.shared", "tv.codely.backoffice", "tv.codely.apps.backoffice.backend" }
)
public class BackofficeBackendApplication {
public static HashMap<String, Class<?>> commands() {
return new HashMap<String, Class<?>>() {{
}};
}

public static HashMap<String, Class<?>> commands() {
return new HashMap<String, Class<?>>() {
{}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import tv.codely.apps.backoffice.backend.middleware.BasicHttpAuthMiddleware;
import tv.codely.shared.domain.bus.command.CommandBus;

@Configuration
public class BackofficeBackendServerConfiguration {
private final CommandBus bus;

public BackofficeBackendServerConfiguration(CommandBus bus) {
this.bus = bus;
}
private final CommandBus bus;

public BackofficeBackendServerConfiguration(CommandBus bus) {
this.bus = bus;
}

@Bean
public FilterRegistrationBean<BasicHttpAuthMiddleware> basicHttpAuthMiddleware() {
FilterRegistrationBean<BasicHttpAuthMiddleware> registrationBean = new FilterRegistrationBean<>();
@Bean
public FilterRegistrationBean<BasicHttpAuthMiddleware> basicHttpAuthMiddleware() {
FilterRegistrationBean<BasicHttpAuthMiddleware> registrationBean = new FilterRegistrationBean<>();

registrationBean.setFilter(new BasicHttpAuthMiddleware(bus));
registrationBean.addUrlPatterns("/health-check");
registrationBean.setFilter(new BasicHttpAuthMiddleware(bus));
registrationBean.addUrlPatterns("/health-check");

return registrationBean;
}
return registrationBean;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,26 @@
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.stereotype.Component;

import tv.codely.shared.infrastructure.config.Parameter;
import tv.codely.shared.infrastructure.config.ParameterNotExist;

@Component
public final class BackofficeBackendServerPortCustomizer implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {
private final Parameter param;
public final class BackofficeBackendServerPortCustomizer
implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {

private final Parameter param;

public BackofficeBackendServerPortCustomizer(Parameter param) {
this.param = param;
}
public BackofficeBackendServerPortCustomizer(Parameter param) {
this.param = param;
}

@Override
public void customize(ConfigurableWebServerFactory factory) {
try {
factory.setPort(param.getInt("BACKOFFICE_BACKEND_SERVER_PORT"));
} catch (ParameterNotExist parameterNotExist) {
parameterNotExist.printStackTrace();
}
}
@Override
public void customize(ConfigurableWebServerFactory factory) {
try {
factory.setPort(param.getInt("BACKOFFICE_BACKEND_SERVER_PORT"));
} catch (ParameterNotExist parameterNotExist) {
parameterNotExist.printStackTrace();
}
}
}
Loading

0 comments on commit 64a6d44

Please sign in to comment.