Feat: Initial Setup through env vars (#1736)
* initial support for initial setup * improve setup * improve mobile view * move base admin route * admin panel mobile view * set initial host and port * add docs * properly setup everything, use for dev env * change userconfig and interface port on setup, note users afterwards
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { eq, sql } from 'drizzle-orm';
|
||||
import { userConfig } from './schema';
|
||||
import type { UserConfigUpdateType } from './types';
|
||||
import { wgInterface } from '#db/schema';
|
||||
import type { DBType } from '#db/sqlite';
|
||||
|
||||
function createPreparedStatement(db: DBType) {
|
||||
@@ -8,14 +9,6 @@ function createPreparedStatement(db: DBType) {
|
||||
get: db.query.userConfig
|
||||
.findFirst({ where: eq(userConfig.id, sql.placeholder('interface')) })
|
||||
.prepare(),
|
||||
updateHostPort: db
|
||||
.update(userConfig)
|
||||
.set({
|
||||
host: sql.placeholder('host') as never as string,
|
||||
port: sql.placeholder('port') as never as number,
|
||||
})
|
||||
.where(eq(userConfig.id, sql.placeholder('interface')))
|
||||
.prepare(),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -38,11 +31,26 @@ export class UserConfigService {
|
||||
return userConfig;
|
||||
}
|
||||
|
||||
// TODO: wrap ipv6 host in square brackets
|
||||
|
||||
/**
|
||||
* sets host of user config
|
||||
*
|
||||
* sets port of user config and interface
|
||||
*/
|
||||
updateHostPort(host: string, port: number) {
|
||||
return this.#statements.updateHostPort.execute({
|
||||
interface: 'wg0',
|
||||
host,
|
||||
port,
|
||||
return this.#db.transaction(async (tx) => {
|
||||
await tx
|
||||
.update(userConfig)
|
||||
.set({ host, port })
|
||||
.where(eq(userConfig.id, 'wg0'))
|
||||
.execute();
|
||||
|
||||
await tx
|
||||
.update(wgInterface)
|
||||
.set({ port })
|
||||
.where(eq(wgInterface.name, 'wg0'))
|
||||
.execute();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,13 @@ const db = drizzle({ client, schema });
|
||||
|
||||
export async function connect() {
|
||||
await migrate();
|
||||
return new DBService(db);
|
||||
const dbService = new DBService(db);
|
||||
|
||||
if (WG_INITIAL_ENV.ENABLED) {
|
||||
await initialSetup(dbService);
|
||||
}
|
||||
|
||||
return dbService;
|
||||
}
|
||||
|
||||
class DBService {
|
||||
@@ -58,3 +64,47 @@ async function migrate() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function initialSetup(db: DBServiceType) {
|
||||
const setup = await db.general.getSetupStep();
|
||||
|
||||
if (setup.done) {
|
||||
DB_DEBUG('Setup already done. Skiping initial setup.');
|
||||
return;
|
||||
}
|
||||
|
||||
if (WG_INITIAL_ENV.IPV4_CIDR && WG_INITIAL_ENV.IPV6_CIDR) {
|
||||
DB_DEBUG('Setting initial CIDR...');
|
||||
await db.interfaces.updateCidr({
|
||||
ipv4Cidr: WG_INITIAL_ENV.IPV4_CIDR,
|
||||
ipv6Cidr: WG_INITIAL_ENV.IPV6_CIDR,
|
||||
});
|
||||
}
|
||||
|
||||
if (WG_INITIAL_ENV.DNS) {
|
||||
DB_DEBUG('Setting initial DNS...');
|
||||
const userConfig = await db.userConfigs.get();
|
||||
await db.userConfigs.update({
|
||||
...userConfig,
|
||||
defaultDns: WG_INITIAL_ENV.DNS,
|
||||
});
|
||||
}
|
||||
|
||||
if (
|
||||
WG_INITIAL_ENV.USERNAME &&
|
||||
WG_INITIAL_ENV.PASSWORD &&
|
||||
WG_INITIAL_ENV.HOST &&
|
||||
WG_INITIAL_ENV.PORT
|
||||
) {
|
||||
DB_DEBUG('Creating initial user...');
|
||||
await db.users.create(WG_INITIAL_ENV.USERNAME, WG_INITIAL_ENV.PASSWORD);
|
||||
|
||||
DB_DEBUG('Setting initial host and port...');
|
||||
await db.userConfigs.updateHostPort(
|
||||
WG_INITIAL_ENV.HOST,
|
||||
WG_INITIAL_ENV.PORT
|
||||
);
|
||||
|
||||
await db.general.setSetupStep(0);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user