Springboot for JS
// src/MainApp.ts
import { Service, BootApplication, Configuration, Bean, Qualifier, Component, Primary, RestController } from "./annotations/di";
import { Get, PathParam } from "./annotations/http";
import { run } from "./core/run";
import { Log } from "./logging/log";
import { Logger } from "./logging/logger";
@Configuration()
export class AppConfig {
@Primary()
@Bean()
greeting(): string {
return "Hi";
}
@Bean()
hallo(): string {
return "Hallo";
}
@Bean({name: "greetingWithName"})
greetingWithName(greeting: string): string {
return greeting + " Henry!";
}
}
@Component()
export class HelloWorldService {
constructor(@Qualifier("greetingWithName")private greetingWithName: string) { }
greet() {
return `${this.greetingWithName} from HelloWorldService!`;
}
}
@Log()
@Service()
export class GreetingService {
private log!: Logger;
constructor(@Qualifier("greetingWithName")private greetingWithName: string) { }
greet() {
this.log.info("GreetingService.greet() called");
return `Hey ${this.greetingWithName}! from GreetingService!`;
}
}
@Log()
@RestController('/hello')
export class HelloWorldController {
private log!: Logger;
constructor(private greetingService: GreetingService, private worldService: HelloWorldService) { }
@Get('/greet/:name')
greet(@PathParam("name", "Henry") name: string): any {
this.log.info("HelloWorldController.greet() called");
return {
message: `Hello ${name}!`,
};
}
@Get('/', {produces: 'text/html'})
greetHtml() {
this.log.info("HelloWorldController.greetHtml() called");
return (<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>Hello World!</h1>
<p>This is a simple HTML response.</p>
</body>
</html>);
}
@Get('/world')
world() : string {
this.log.info("HelloWorldController.world() called");
return this.worldService.greet();
}
}
@Log()
@BootApplication()
export class MainApp {
private log!: Logger;
constructor(private greetingService: GreetingService, private worldService: HelloWorldService) { }
start() {
this.log.info(this.greetingService.greet());
this.log.info(this.worldService.greet());
this.log.info("MainApp started");
}
}
run<MainApp>(MainApp).then(app => {app.start()});