Skip to content

Commit 00e5591

Browse files
committed
Start adding nuxt to webapp component
Allow specifying which services you want to load through an env var (kind of hacky, for debugging purposes mainly) Turn all anonymous provider functions into named functions for that purpose Clean up some code, tsconfig, etc This was a little too dreadful... still heavily WIP. Don't merge or use as is.
1 parent c6a06ff commit 00e5591

25 files changed

+7676
-4400
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ resources/game-server-status/style.css
1313
!*.example.json
1414
deploy.sh
1515
markov_data.txt
16-
metaconcord.db
16+
metaconcord.db
17+
.nuxt

app/Container.ts

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,31 @@
1-
import { App } from ".";
2-
import { Service, ServiceMap } from "./services";
3-
4-
type ProviderFactory = { (container: Container): Service | Promise<Service> }[];
5-
6-
export class Container {
7-
readonly app: App;
8-
private providers: ProviderFactory;
9-
private services: ServiceMap = {};
10-
11-
constructor(app: App, providers: ProviderFactory) {
12-
this.app = app;
13-
this.providers = providers;
14-
}
15-
16-
getProviders(): ProviderFactory {
17-
return this.providers;
18-
}
19-
20-
getServices(): ServiceMap {
21-
return this.services;
22-
}
23-
24-
async addService(service: Service | Promise<Service>): Promise<void> {
25-
if (service instanceof Promise) {
26-
service = await service;
27-
}
28-
this.services[service.name] = service;
29-
}
30-
31-
getService<Name extends string>(type: Name): ServiceMap[Name] {
32-
return this.services[type];
33-
}
34-
}
1+
import { App } from ".";
2+
import { Service, ServiceMap } from "./services";
3+
4+
type ProviderFactory = { (container: Container): Service | Promise<Service> }[];
5+
6+
export class Container {
7+
readonly app: App;
8+
private providers: ProviderFactory;
9+
private services: ServiceMap = {};
10+
11+
constructor(app: App, providers: ProviderFactory) {
12+
this.app = app;
13+
this.providers = providers;
14+
}
15+
16+
getProviders(): ProviderFactory {
17+
return this.providers;
18+
}
19+
20+
getServices(): ServiceMap {
21+
return this.services;
22+
}
23+
24+
addService(service: Service): void {
25+
this.services[service.name] = service;
26+
}
27+
28+
getService<Name extends string>(type: Name): ServiceMap[Name] {
29+
return this.services[type];
30+
}
31+
}

app/index.ts

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
1-
import { Container } from "./Container";
2-
import providers from "./services";
3-
4-
export class App {
5-
container: Container;
6-
7-
constructor() {
8-
this.container = new Container(this, providers);
9-
10-
this.init();
11-
}
12-
13-
async init(): Promise<void> {
14-
for (const provider of this.container.getProviders()) {
15-
await this.container.addService(provider(this.container));
16-
}
17-
}
18-
}
1+
import { Container } from "./Container";
2+
import providers from "./services";
3+
4+
export class App {
5+
container: Container;
6+
7+
constructor() {
8+
this.container = new Container(this, providers);
9+
10+
this.init();
11+
}
12+
13+
async init(): Promise<void> {
14+
for (const provider of this.container.getProviders()) {
15+
let providersToLoad;
16+
if (process.env.METACONCORD_LOAD_SERVICES) {
17+
const splitServices = process.env.METACONCORD_LOAD_SERVICES.split(" ");
18+
if (splitServices) {
19+
providersToLoad = Object.fromEntries(
20+
splitServices.filter(x => x).map(provider => [provider.toLowerCase(), true])
21+
);
22+
}
23+
}
24+
25+
const providerName = provider.name.replace(/Provider$/, "").toLowerCase(); // Hack lol
26+
const load = providersToLoad ? providersToLoad[providerName] : true;
27+
if (load) {
28+
const service = await provider(this.container);
29+
this.container.addService(service);
30+
}
31+
}
32+
}
33+
}

app/services/Data.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ export class Data extends Service {
6262
}
6363
}
6464

65-
export default async (container: Container): Promise<Service> => {
65+
export default async function DataProvider(container: Container): Promise<Service> {
6666
const data = new Data(container);
6767
await data.init();
6868
await data.load();
6969
return data;
70-
};
70+
}

app/services/IRC.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,6 @@ export class IRC extends Service {
9595
}
9696
}
9797

98-
export default (container: Container): Service => {
98+
export default function IRCProvider(container: Container): Service {
9999
return new IRC(container);
100-
};
100+
}

app/services/Markov.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,6 @@ export class MarkovService extends Service {
253253
// }
254254
}
255255

256-
export default (container: Container): Service => {
256+
export default function MarkovProvider(container: Container): Service {
257257
return new MarkovService(container);
258-
};
258+
}

app/services/SQL.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ export class SQL extends Service {
2929
}
3030
}
3131

32-
export default (container: Container): Service => {
32+
export default function SQLProvider(container: Container): Service {
3333
return new SQL(container);
34-
};
34+
}

app/services/Service.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Container } from "@/app/Container";
2+
3+
export default class Service {
4+
readonly name: string;
5+
container: Container;
6+
7+
constructor(container: Container) {
8+
this.container = container;
9+
}
10+
}

app/services/Starboard.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Container } from "../Container";
22
import { MessageReaction } from "discord.js";
3-
import { Service } from ".";
43
import { SQL } from "./SQL";
4+
import { Service } from ".";
55
import { TextChannel } from "discord.js";
66
import Discord from "discord.js";
77
import config from "@/config/starboard.json";
@@ -90,6 +90,6 @@ export class Starboard extends Service {
9090
}
9191
}
9292

93-
export default (container: Container): Service => {
93+
export default function StarboardProvider(container: Container): Service {
9494
return new Starboard(container);
95-
};
95+
}

app/services/Steam.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,6 @@ export class Steam extends Service {
7979
}
8080
}
8181

82-
export default (container: Container): Service => {
82+
export default function SteamProvider(container: Container): Service {
8383
return new Steam(container);
84-
};
84+
}

app/services/Twitter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ApiResponseError, TweetExtendedEntitiesV1, TweetV1, TwitterApi } from "twitter-api-v2";
1+
import { ApiResponseError, TweetV1, TwitterApi } from "twitter-api-v2";
22
import { Container } from "@/app/Container";
33
import { Service } from ".";
44
import Filter from "bad-words";
@@ -188,6 +188,6 @@ export class Twitter extends Service {
188188
}
189189
}
190190

191-
export default (container: Container): Service => {
191+
export default function TwitterProvider(container: Container): Service {
192192
return new Twitter(container);
193-
};
193+
}

app/services/discord/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,6 @@ export class DiscordBot extends Service {
133133
}
134134
}
135135

136-
export default (container: Container): Service => {
136+
export default function DiscordBotProvider(container: Container): Service {
137137
return new DiscordBot(container);
138-
};
138+
}

app/services/gamebridge/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ export { DiscordClient, GameServer, GameBridge, GameServerConfig, Player };
55

66
import { Container } from "@/app/Container";
77
import { Service } from "@/app/services";
8-
export default (container: Container): Service => {
8+
export default function GameBridgeProvider(container: Container): Service {
99
return new GameBridge(container);
10-
};
10+
}

app/services/index.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
1-
import { Container } from "@/app/Container";
2-
3-
export class Service {
4-
readonly name: string;
5-
container: Container;
6-
7-
constructor(container: Container) {
8-
this.container = container;
9-
}
10-
}
1+
import Service from "./Service";
2+
export { Service };
113

124
import DataProvider, { Data } from "./Data";
135
import DiscordBotProvider, { DiscordBot } from "./discord";
@@ -34,7 +26,7 @@ export default [
3426
StarboardProvider,
3527
IRCProvider,
3628
]; // The order is important
37-
export { SQLProvider, Data, DiscordBot, GameBridge, Steam, WebApp, Motd, Twitter, IRC };
29+
export { SQL, Data, DiscordBot, GameBridge, Steam, WebApp, Motd, Twitter, IRC };
3830
export type ServiceMap = {
3931
[key: string]: Service | undefined;
4032
Data?: Data;

app/services/motd/index.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Container } from "@/app/Container";
2-
import { Service } from "@/app/services";
3-
import Motd from "./Motd";
4-
5-
export { Motd };
6-
export default (container: Container): Service => {
7-
return new Motd(container);
8-
};
1+
import { Container } from "@/app/Container";
2+
import { Service } from "@/app/services";
3+
import Motd from "./Motd";
4+
5+
export { Motd };
6+
export default function MotdProvider(container: Container): Service {
7+
return new Motd(container);
8+
}

app/services/webapp/api/index.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1+
import addCI from "./ci";
2+
import addChangeGamemode from "./gamemode";
13
import addEmojiAPI from "./emojis";
24
import addGameServerStatusAPI from "./game-server-status";
35
import addMapThumbnails from "./map-thumbnails";
4-
import changeGamemode from "./gamemode";
5-
import ci from "./ci";
6+
import addNuxt from "./nuxt";
67

7-
export default [addEmojiAPI, addGameServerStatusAPI, addMapThumbnails, changeGamemode, ci];
8+
export default [
9+
addEmojiAPI,
10+
addGameServerStatusAPI,
11+
addMapThumbnails,
12+
addChangeGamemode,
13+
addCI,
14+
addNuxt,
15+
];

app/services/webapp/api/nuxt.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { WebApp } from "..";
2+
3+
export default async (webApp: WebApp): Promise<void> => {
4+
const { buildNuxt, loadNuxt } = await import("@nuxt/kit");
5+
6+
// TODO: Find a way to live build but disallow reloading?
7+
// Maybe you have to build before?
8+
9+
// Check if we need to run Nuxt in development mode
10+
const isDev = true; // process.env.NODE_ENV !== "production";
11+
12+
// Get a ready to use Nuxt instance
13+
const nuxt = await loadNuxt({ dev: isDev, ready: false });
14+
await nuxt.ready();
15+
16+
// Enable live build & reloading
17+
if (isDev) {
18+
buildNuxt(nuxt);
19+
}
20+
21+
// Probably need to do more stuff here, look at nuxi dev / nuxi build
22+
23+
webApp.app.use(nuxt.server.app);
24+
};

app/services/webapp/index.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,18 @@ export class WebApp extends Service {
1414
constructor(container: Container) {
1515
super(container);
1616

17-
for (const addAPI of APIs) {
18-
addAPI(this);
19-
}
17+
(async () => {
18+
for (const addAPI of APIs) {
19+
await addAPI(this);
20+
}
2021

21-
this.http = this.app.listen(this.config.port, "0.0.0.0", () => {
22-
console.log(`HTTP server listening on ${this.config.port}`);
23-
});
22+
this.http = this.app.listen(this.config.port, "0.0.0.0", () => {
23+
console.log(`HTTP server listening on ${this.config.port}`);
24+
});
25+
})();
2426
}
2527
}
2628

27-
export default (container: Container): Service => {
29+
export default function WebAppProvider(container: Container): Service {
2830
return new WebApp(container);
29-
};
31+
}

index.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
// Load environment variables
2-
import * as dotenv from "dotenv";
3-
dotenv.config();
4-
5-
// Load module extensions
6-
import "@/extensions";
7-
8-
// Main
9-
import { App } from "./app";
10-
const MetaConcord = new App();
11-
(global as any).MetaConcord = MetaConcord;
12-
13-
export default MetaConcord;
1+
// Load environment variables
2+
import * as dotenv from "dotenv";
3+
dotenv.config();
4+
5+
// Load module extensions
6+
import "@/extensions";
7+
8+
// Main
9+
import { App } from "./app";
10+
const MetaConcord = new App();
11+
globalThis.MetaConcord = MetaConcord;
12+
13+
export default MetaConcord;

nuxt.config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { defineNuxtConfig } from "nuxt";
2+
3+
// https://v3.nuxtjs.org/api/configuration/nuxt.config
4+
export default defineNuxtConfig({
5+
srcDir: "nuxt/",
6+
});

nuxt/pages/index.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template>
2+
Hello World
3+
</template>

nuxt/pages/test.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template>
2+
Test
3+
</template>

0 commit comments

Comments
 (0)