Skip to content

Commit 17081c4

Browse files
committed
refactor: create standalone server library
1 parent 5294d55 commit 17081c4

22 files changed

+169
-127
lines changed

angular.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,39 @@
118118
}
119119
}
120120
},
121+
"server": {
122+
"root": "libs/server",
123+
"sourceRoot": "libs/server/src",
124+
"projectType": "library",
125+
"architect": {
126+
"build": {
127+
"builder": "@nrwl/node:package",
128+
"outputs": ["{options.outputPath}"],
129+
"options": {
130+
"outputPath": "dist/libs/server",
131+
"tsConfig": "libs/server/tsconfig.lib.json",
132+
"packageJson": "libs/server/package.json",
133+
"main": "libs/server/src/index.ts",
134+
"assets": ["libs/server/*.md"]
135+
}
136+
},
137+
"lint": {
138+
"builder": "@nrwl/linter:eslint",
139+
"outputs": ["{options.outputFile}"],
140+
"options": {
141+
"lintFilePatterns": ["libs/server/**/*.ts"]
142+
}
143+
},
144+
"test": {
145+
"builder": "@nrwl/jest:jest",
146+
"outputs": ["coverage/libs/server"],
147+
"options": {
148+
"jestConfig": "libs/server/jest.config.js",
149+
"passWithNoTests": true
150+
}
151+
}
152+
}
153+
},
121154
"web": {
122155
"projectType": "application",
123156
"root": "apps/web",

apps/dev-server/src/main.ts

Lines changed: 12 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -3,109 +3,17 @@
33
* This is only a minimal backend to get started.
44
*/
55

6-
import { Layout, Plot, plot, bootstrap } from 'nodeplotlib';
7-
import { PlotType } from 'plotly.js';
6+
import { NestFactory } from '@nestjs/core';
7+
import { ServerModule } from '@npl/server';
88

9-
(async () => {
10-
await bootstrap(3333, false);
9+
async function bootstrap() {
10+
const app = await NestFactory.create(ServerModule);
11+
app.enableCors();
12+
await app.listen(3333);
13+
console.log(
14+
'Server runnng at',
15+
`http://localhost:${app.getHttpServer().address().port}`
16+
);
17+
}
1118

12-
// bootstrap();
13-
const trace1: Plot = {
14-
x: [
15-
'2017-01-04',
16-
'2017-01-05',
17-
'2017-01-06',
18-
'2017-01-09',
19-
'2017-01-10',
20-
'2017-01-11',
21-
'2017-01-12',
22-
'2017-01-13',
23-
'2017-01-17',
24-
'2017-01-18',
25-
'2017-01-19',
26-
'2017-01-20',
27-
'2017-01-23',
28-
'2017-01-24',
29-
'2017-01-25',
30-
'2017-01-26',
31-
'2017-01-27',
32-
'2017-01-30',
33-
'2017-01-31',
34-
'2017-02-01',
35-
'2017-02-02',
36-
'2017-02-03',
37-
'2017-02-06',
38-
'2017-02-07',
39-
'2017-02-08',
40-
'2017-02-09',
41-
'2017-02-10',
42-
'2017-02-13',
43-
'2017-02-14',
44-
'2017-02-15',
45-
],
46-
close: [
47-
116.019997, 116.610001, 117.910004, 118.989998, 119.110001, 119.75,
48-
119.25, 119.040001, 120, 119.989998, 119.779999, 120, 120.080002,
49-
119.970001, 121.879997, 121.940002, 121.949997, 121.629997, 121.349998,
50-
128.75, 128.529999, 129.080002, 130.289993, 131.529999, 132.039993,
51-
132.419998, 132.119995, 133.289993, 135.020004, 135.509995,
52-
],
53-
decreasing: { line: { color: '#7F7F7F' } },
54-
high: [
55-
116.510002, 116.860001, 118.160004, 119.43, 119.379997, 119.93,
56-
119.300003, 119.620003, 120.239998, 120.5, 120.089996, 120.449997,
57-
120.809998, 120.099998, 122.099998, 122.440002, 122.349998, 121.629997,
58-
121.389999, 130.490005, 129.389999, 129.190002, 130.5, 132.089996,
59-
132.220001, 132.449997, 132.940002, 133.820007, 135.089996, 136.270004,
60-
],
61-
increasing: { line: { color: '#17BECF' } },
62-
line: { color: 'rgba(31,119,180,1)' },
63-
low: [
64-
115.75, 115.809998, 116.470001, 117.940002, 118.300003, 118.599998,
65-
118.209999, 118.809998, 118.220001, 119.709999, 119.370003, 119.730003,
66-
119.769997, 119.5, 120.279999, 121.599998, 121.599998, 120.660004,
67-
120.620003, 127.010002, 127.779999, 128.160004, 128.899994, 130.449997,
68-
131.220001, 131.119995, 132.050003, 132.75, 133.25, 134.619995,
69-
],
70-
open: [
71-
115.849998, 115.919998, 116.779999, 117.949997, 118.769997, 118.739998,
72-
118.900002, 119.110001, 118.339996, 120, 119.400002, 120.449997, 120,
73-
119.550003, 120.419998, 121.669998, 122.139999, 120.93, 121.150002,
74-
127.029999, 127.980003, 128.309998, 129.130005, 130.539993, 131.350006,
75-
131.649994, 132.460007, 133.080002, 133.470001, 135.520004,
76-
],
77-
type: 'candlestick' as PlotType,
78-
xaxis: 'x',
79-
yaxis: 'y',
80-
} as Plot;
81-
82-
const data = [trace1];
83-
84-
const layout = {
85-
dragmode: 'zoom',
86-
margin: {
87-
r: 10,
88-
t: 25,
89-
b: 40,
90-
l: 60,
91-
},
92-
showlegend: false,
93-
xaxis: {
94-
autorange: true,
95-
domain: [0, 1],
96-
range: ['2017-01-03 12:00', '2017-02-15 12:00'],
97-
rangeslider: { range: ['2017-01-03 12:00', '2017-02-15 12:00'] },
98-
title: 'Date',
99-
type: 'date',
100-
},
101-
yaxis: {
102-
autorange: true,
103-
domain: [0, 1],
104-
range: [114.609999778, 137.410004222],
105-
type: 'linear',
106-
},
107-
} as Layout;
108-
109-
plot(data, layout);
110-
plot(data, layout);
111-
})();
19+
bootstrap();

apps/web/src/app/components/app/app.component.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ mat-toolbar {
3030
.info-icons {
3131
display: flex;
3232
gap: 8px;
33-
}
33+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<div #plotContainer></div>
1+
<div #plotContainer></div>
Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { AfterViewInit, ChangeDetectionStrategy, Component, ElementRef, Input, ViewChild } from '@angular/core';
1+
import {
2+
AfterViewInit,
3+
ChangeDetectionStrategy,
4+
Component,
5+
ElementRef,
6+
Input,
7+
ViewChild,
8+
} from '@angular/core';
29
import { PlotData } from '@npl/interfaces';
310

411
// eslint-disable-next-line
@@ -8,18 +15,18 @@ declare const Plotly: any;
815
selector: 'npl-plot',
916
templateUrl: './plot.component.html',
1017
styleUrls: ['./plot.component.css'],
11-
changeDetection: ChangeDetectionStrategy.OnPush
18+
changeDetection: ChangeDetectionStrategy.OnPush,
1219
})
1320
export class PlotComponent implements AfterViewInit {
1421
@Input() plotData!: PlotData;
15-
@ViewChild('plotContainer', {static: false}) plotContainer!: ElementRef;
22+
@ViewChild('plotContainer', { static: false }) plotContainer!: ElementRef;
1623

1724
ngAfterViewInit() {
1825
Plotly.newPlot(
1926
this.plotContainer.nativeElement,
2027
this.plotData.data,
21-
{...(this.plotData.layout ?? {}), autosize: true},
22-
{responsive: true}
23-
)
28+
{ ...(this.plotData.layout ?? {}), autosize: true },
29+
{ responsive: true }
30+
);
2431
}
2532
}

apps/web/src/app/components/stack/stack.component.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import { PlotsService } from '../../services/plots.service';
1111
changeDetection: ChangeDetectionStrategy.OnPush,
1212
})
1313
export class StackComponent {
14-
stackId$: Observable<number|null> = this.activatedRoute.params.pipe(map(params => params.id ? +params.id : null), tap(console.log));
15-
plots$ = combineLatest([
16-
this.stackId$,
17-
this.plotsService.data$
18-
]).pipe(
14+
stackId$: Observable<number | null> = this.activatedRoute.params.pipe(
15+
map((params) => (params.id ? +params.id : null)),
16+
tap(console.log)
17+
);
18+
plots$ = combineLatest([this.stackId$, this.plotsService.data$]).pipe(
1919
tap((data) => console.log(data)),
2020
map(([stackId, { stacks, plots }]) => {
2121
if (stackId === null) {
@@ -26,7 +26,7 @@ export class StackComponent {
2626
return [];
2727
}
2828

29-
return stack.plotIds.map(id => plots.entities[id])
29+
return stack.plotIds.map((id) => plots.entities[id]);
3030
})
3131
);
3232

libs/nodeplotlib/src/lib/nodeplotlib.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import { NestFactory } from '@nestjs/core';
33
import { exec } from 'child_process';
44
import { type } from 'os';
55
import { Layout, Plot } from '@npl/interfaces';
6-
import { NodeplotlibModule } from './nodeplotlib.module';
7-
import { PlotsService } from './plots/plots.service';
6+
import { ServerModule, PlotsService } from '@npl/server';
87
let app: INestApplication | null = null;
98
let plotsService: PlotsService;
109

@@ -20,8 +19,8 @@ export async function plot(data?: Plot[] | null, layout?: Layout) {
2019
plotsService.addPlot({ data, layout });
2120
}
2221

23-
// const address = app.getHttpServer().address();
24-
// openWindow(`http://localhost:${address.port}`);
22+
const address = app.getHttpServer().address();
23+
openWindow(`http://localhost:${address.port}`);
2524
}
2625

2726
/**
@@ -46,14 +45,12 @@ export async function clear() {
4645
}
4746
}
4847

49-
export async function bootstrap(port = 0, production = true) {
48+
export async function bootstrap(port = 0) {
5049
if (app) {
5150
console.log('App is already up and running');
5251
return;
5352
}
54-
app = await NestFactory.create(NodeplotlibModule, {
55-
cors: production ? false : true,
56-
});
53+
app = await NestFactory.create(ServerModule);
5754
plotsService = app.get(PlotsService);
5855
await app.listen(port);
5956
console.log('Server runnng at', app.getHttpServer().address().port);

libs/server/.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": [["@nrwl/web/babel", { "useBuiltIns": "usage" }]]
3+
}

libs/server/.eslintrc.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"extends": ["../../.eslintrc.json"],
3+
"ignorePatterns": ["!**/*"],
4+
"overrides": [
5+
{
6+
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
7+
"rules": {}
8+
},
9+
{
10+
"files": ["*.ts", "*.tsx"],
11+
"rules": {}
12+
},
13+
{
14+
"files": ["*.js", "*.jsx"],
15+
"rules": {}
16+
}
17+
]
18+
}

libs/server/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# server
2+
3+
This library was generated with [Nx](https://nx.dev).
4+
5+
## Running unit tests
6+
7+
Run `nx test server` to execute the unit tests via [Jest](https://jestjs.io).

0 commit comments

Comments
 (0)