Skip to content

Commit

Permalink
fix client docker file and routes
Browse files Browse the repository at this point in the history
  • Loading branch information
JuribaDev committed Aug 28, 2024
1 parent b9b9ec0 commit 30ba5cd
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 31 deletions.
13 changes: 9 additions & 4 deletions apps/client/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
FROM node:lts-alpine3.20 AS build
FROM node:lts-alpine AS build
WORKDIR /app

RUN apk add --no-cache python3 make g++

COPY package*.json ./

RUN npm ci

COPY . .

RUN npx nx build client --prod

RUN npx nx build client --configuration=production

FROM nginx:stable-alpine

ARG GITHUB_SHA
ARG BUILD_DATE
ARG GITHUB_REF_NAME

LABEL org.opencontainers.image.title="R&D Platform Client"
LABEL org.opencontainers.image.source="https://github.com/JuribaDev/rnd-platform"
LABEL org.opencontainers.image.revision=${GITHUB_SHA}
Expand All @@ -28,6 +27,12 @@ COPY --from=build /app/dist/apps/client/browser /usr/share/nginx/html

COPY apps/client/nginx.conf /etc/nginx/nginx.conf

RUN apk add --no-cache gettext

COPY apps/client/env.sh /docker-entrypoint.d/40-env.sh

RUN chmod +x /docker-entrypoint.d/40-env.sh

RUN chown -R nginx:nginx /usr/share/nginx/html && \
chmod -R 755 /usr/share/nginx/html && \
chown -R nginx:nginx /var/cache/nginx && \
Expand Down
8 changes: 8 additions & 0 deletions apps/client/env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh

# Recreate config file
envsubst < /usr/share/nginx/html/main*.js > /usr/share/nginx/html/main-replaced.js
mv /usr/share/nginx/html/main-replaced.js /usr/share/nginx/html/main*.js

# Replace API_URL in the main.js file
sed -i 's|apiUrl:[^,]*|apiUrl:"'"${API_URL}"'"|g' /usr/share/nginx/html/main*.js
26 changes: 10 additions & 16 deletions apps/client/nginx.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
user nginx;
worker_processes auto;
pid /var/run/nginx.pid;

events {
worker_connections 1024;
}
Expand All @@ -9,14 +10,13 @@ http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

include /etc/nginx/conf.d/*.conf;

gzip on;
gzip_disable "msie6";
log_format debug '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'"$request_filename"';

access_log /var/log/nginx/access.log debug;
error_log /var/log/nginx/error.log debug;

server {
listen 80;
Expand All @@ -29,16 +29,10 @@ http {
try_files $uri $uri/ /index.html;
}

error_page 404 /index.html;

location ~* \.(?:manifest|appcache|html?|xml|json)$ {
expires -1;
add_header Cache-Control "no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0";
location ~* ^(?!/assets/).*$ {
try_files $uri $uri/ /index.html;
}

location ~* \.(?:css|js|woff2?|eot|ttf|otf|svg|png|jpg|jpeg|gif|ico)$ {
expires 1y;
add_header Cache-Control "public, max-age=31536000, immutable";
}
error_page 404 /index.html;
}
}
7 changes: 6 additions & 1 deletion apps/client/src/app/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ import { routes } from './app.routes';
import { ReactiveFormsModule } from '@angular/forms';
import { provideAnimations } from '@angular/platform-browser/animations';

console.log('Configuring Angular application...');

export const appConfig: ApplicationConfig = {
providers: [
provideHttpClient(withInterceptors([authInterceptor])),
provideRouter(routes),
provideHttpClient(withInterceptors([authInterceptor])),
importProvidersFrom(ReactiveFormsModule),
provideAnimations(),
],
};

console.log('Angular application configured.');
console.log('Routes:', routes);
18 changes: 18 additions & 0 deletions apps/client/src/app/app.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,28 @@ import { LoginComponent } from './auth/components/login.component';
import { RegisterComponent } from './auth/components/register.component';
import { ProductListComponent } from './product/components/product-list.component';
import { authGuard } from './auth/guards/auth.guard';
import { inject } from '@angular/core';
import { AuthStore } from './auth/auth.store';
import { Router } from '@angular/router';

export const routes: Routes = [
{ path: '', redirectTo: '/product', pathMatch: 'full' },
{ path: 'login', component: LoginComponent },
{ path: 'register', component: RegisterComponent },
{ path: 'product', component: ProductListComponent, canActivate: [authGuard] },
{
path: '**',
canActivate: [() => {
const authStore = inject(AuthStore);
const router = inject(Router);

if (authStore.isAuthenticated()) {
router.navigate(['/product']);
} else {
router.navigate(['/login']);
}
return false;
}],
component: ProductListComponent
}
];
5 changes: 3 additions & 2 deletions apps/client/src/app/shared/api-client.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
import { TokenService } from './token.service';
import { LoginRequestDto, RegisterRequestDto, AuthResponseDto } from '../auth/dto/auth.dto';
import { PaginatedProductResponseDto, ProductDto } from '../product/dto/product.dto';
import { PaginatedProductResponseDto } from '../product/dto/product.dto';
import { environment } from '../../environments/environment';

@Injectable({
providedIn: 'root'
})
export class ApiClientService {
private baseUrl = 'http://localhost:3000/api/v1';
private baseUrl = environment.apiUrl;

constructor(private http: HttpClient, private tokenService: TokenService) {}

Expand Down
4 changes: 4 additions & 0 deletions apps/client/src/environments/environment.prod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const environment = {
production: true,
apiUrl: '/api/v1'
};
4 changes: 4 additions & 0 deletions apps/client/src/environments/environment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const environment = {
production: false,
apiUrl: 'http://localhost:3000/api/v1'
};
10 changes: 7 additions & 3 deletions apps/client/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { bootstrapApplication } from '@angular/platform-browser';
import { appConfig } from './app/app.config';
import { AppComponent } from './app/app.component';

bootstrapApplication(AppComponent, appConfig).catch((err) =>
console.error(err)
);
console.log('Angular application starting...');

bootstrapApplication(AppComponent, appConfig)
.then(() => console.log('Angular application started successfully.'))
.catch((err) => {
console.error('Angular application failed to start:', err);
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class LoginUseCase {
}

async execute(loginRequest:LoginRequestDto): Promise<AuthResponseDto> {
const user = await this.userRepository.findByEmail(loginRequest.email);
const user = await this.userRepository.findByEmail(loginRequest.email.trim().toLowerCase());
if (!user) throw new NotFoundException('User not found');

const isPasswordValid = await this.passwordHasher.compare(loginRequest.password, user.password);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class RegisterUseCase {

const newUser = new User(
'',
registerDto.email,
registerDto.email.trim().toLowerCase(),
registerDto.name,
hashedPassword,
new Date(),
Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async function bootstrap() {
const globalPrefix = 'api/v1';
app.setGlobalPrefix(globalPrefix);
app.enableCors({
origin: ['http://localhost:4200'],
origin: ['http://localhost'],
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS'],
credentials: true,
});
Expand Down
2 changes: 0 additions & 2 deletions apps/server/src/product/presentation/product.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ export class ProductController {
@Query('limit') limit = 10
): Promise<PaginatedProductResponseDto> {
const result = await this.getUserProductsUseCase.execute(req, page, limit);
await new Promise(resolve => setTimeout(resolve, 3000)); // 2-second delay

return new PaginatedProductResponseDto(result);
}

Expand Down

0 comments on commit 30ba5cd

Please sign in to comment.