Add initial project files
This commit is contained in:
Executable
+46
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Environment variables service
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| The `Env.create` method creates an instance of the Env service. The
|
||||
| service validates the environment variables and also cast values
|
||||
| to JavaScript data types.
|
||||
|
|
||||
*/
|
||||
|
||||
import { Env } from '@adonisjs/core/env'
|
||||
|
||||
export default await Env.create(new URL('../', import.meta.url), {
|
||||
NODE_ENV: Env.schema.enum(['development', 'production', 'test'] as const),
|
||||
PORT: Env.schema.number(),
|
||||
APP_KEY: Env.schema.string(),
|
||||
HOST: Env.schema.string({ format: 'host' }),
|
||||
LOG_LEVEL: Env.schema.enum(['fatal', 'error', 'warn', 'info', 'debug', 'trace']),
|
||||
|
||||
/*
|
||||
|----------------------------------------------------------
|
||||
| Variables for configuring database connection
|
||||
|----------------------------------------------------------
|
||||
*/
|
||||
DB_HOST: Env.schema.string({ format: 'host' }),
|
||||
DB_PORT: Env.schema.number(),
|
||||
DB_USER: Env.schema.string(),
|
||||
DB_PASSWORD: Env.schema.string.optional(),
|
||||
DB_DATABASE: Env.schema.string(),
|
||||
/*
|
||||
|----------------------------------------------------------
|
||||
| Variables for public web app cache revalidation
|
||||
|----------------------------------------------------------
|
||||
*/
|
||||
WEB_APP_URL: Env.schema.string.optional({ format: 'url', tld: false }),
|
||||
WEB_REVALIDATE_SECRET: Env.schema.string.optional(),
|
||||
|
||||
DRIVE_DISK: Env.schema.enum(['fs', 's3'] as const),
|
||||
AWS_ACCESS_KEY_ID: Env.schema.string.optional(),
|
||||
AWS_SECRET_ACCESS_KEY: Env.schema.string.optional(),
|
||||
AWS_REGION: Env.schema.string.optional(),
|
||||
S3_BUCKET: Env.schema.string.optional(),
|
||||
S3_ENDPOINT: Env.schema.string.optional(),
|
||||
S3_CDN_URL: Env.schema.string.optional(),
|
||||
})
|
||||
Executable
+47
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| HTTP kernel file
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| The HTTP kernel file is used to register the middleware with the server
|
||||
| or the router.
|
||||
|
|
||||
*/
|
||||
|
||||
import router from '@adonisjs/core/services/router'
|
||||
import server from '@adonisjs/core/services/server'
|
||||
|
||||
/**
|
||||
* The error handler is used to convert an exception
|
||||
* to an HTTP response.
|
||||
*/
|
||||
server.errorHandler(() => import('#exceptions/handler'))
|
||||
|
||||
/**
|
||||
* The server middleware stack runs middleware on all the HTTP
|
||||
* requests, even if there is no route registered for
|
||||
* the request URL.
|
||||
*/
|
||||
server.use([
|
||||
() => import('#middleware/container_bindings_middleware'),
|
||||
() => import('#middleware/force_json_response_middleware'),
|
||||
() => import('@adonisjs/cors/cors_middleware'),
|
||||
])
|
||||
|
||||
/**
|
||||
* The router middleware stack runs middleware on all the HTTP
|
||||
* requests with a registered route.
|
||||
*/
|
||||
router.use([
|
||||
() => import('@adonisjs/core/bodyparser_middleware'),
|
||||
() => import('@adonisjs/auth/initialize_auth_middleware'),
|
||||
() => import('#middleware/initialize_bouncer_middleware'),
|
||||
])
|
||||
|
||||
/**
|
||||
* Named middleware collection must be explicitly assigned to
|
||||
* the routes or the routes group.
|
||||
*/
|
||||
export const middleware = router.named({
|
||||
auth: () => import('#middleware/auth_middleware'),
|
||||
})
|
||||
Executable
+96
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Routes file
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| The routes file is used for defining the HTTP routes.
|
||||
|
|
||||
*/
|
||||
import { existsSync, readdirSync } from 'node:fs'
|
||||
import { join } from 'node:path'
|
||||
import { pathToFileURL } from 'node:url'
|
||||
import router from '@adonisjs/core/services/router'
|
||||
import AutoSwagger from 'adonis-autoswagger'
|
||||
import swagger from '#config/swagger'
|
||||
import { middleware } from './kernel.js'
|
||||
|
||||
async function importIfExists(filePath: string) {
|
||||
if (!existsSync(filePath)) return
|
||||
const fileUrl = pathToFileURL(filePath)
|
||||
await import(fileUrl.href)
|
||||
}
|
||||
|
||||
/**
|
||||
* Load module routes from:
|
||||
* <dir>/<Module>/routes.(ts|js)
|
||||
*/
|
||||
async function loadModuleRoutesFrom(dir: string) {
|
||||
if (!existsSync(dir)) return
|
||||
|
||||
const entries = readdirSync(dir, { withFileTypes: true }).filter((d) => d.isDirectory())
|
||||
|
||||
for (const entry of entries) {
|
||||
const moduleRoot = join(dir, entry.name)
|
||||
const tsRoutes = join(moduleRoot, 'routes.ts')
|
||||
const jsRoutes = join(moduleRoot, 'routes.js')
|
||||
|
||||
await importIfExists(tsRoutes)
|
||||
await importIfExists(jsRoutes)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load API routes from:
|
||||
* <dir>/<Module>/api/routes.(ts|js)
|
||||
*
|
||||
*/
|
||||
async function loadApiRoutesFrom(dir: string) {
|
||||
if (!existsSync(dir)) return
|
||||
|
||||
const entries = readdirSync(dir, { withFileTypes: true }).filter((d) => d.isDirectory())
|
||||
for (const entry of entries) {
|
||||
const apiRoot = join(dir, entry.name, 'api')
|
||||
const tsRoutes = join(apiRoot, 'routes.ts')
|
||||
const jsRoutes = join(apiRoot, 'routes.js')
|
||||
|
||||
await importIfExists(tsRoutes)
|
||||
await importIfExists(jsRoutes)
|
||||
}
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// Load base_modules routes
|
||||
// --------------------------------------------------------------------------
|
||||
await loadModuleRoutesFrom(join(process.cwd(), 'app', 'base_modules'))
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// Load modules routes + modules api routes
|
||||
// --------------------------------------------------------------------------
|
||||
const modulesRoot = join(process.cwd(), 'modules')
|
||||
await loadModuleRoutesFrom(modulesRoot)
|
||||
await loadApiRoutesFrom(modulesRoot)
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// Module generator endpoints
|
||||
// --------------------------------------------------------------------------
|
||||
const ModuleController = () => import('#controllers/module_controller')
|
||||
|
||||
router.get('/makeModule/tables', [ModuleController, 'tables']).use([middleware.auth()])
|
||||
router.get('/makeModule/columns/:table', [ModuleController, 'columns']).use([middleware.auth()])
|
||||
router.post('/makeModule/generate', [ModuleController, 'generate']).use([middleware.auth()])
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// Swagger / Docs
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
// returns swagger in YAML
|
||||
router.get('/swagger', async () => {
|
||||
return AutoSwagger.default.docs(router.toJSON(), swagger)
|
||||
})
|
||||
|
||||
// Renders Swagger-UI and passes YAML-output of /swagger
|
||||
router.get('/docs', async () => {
|
||||
return AutoSwagger.default.ui('/swagger', swagger)
|
||||
// return AutoSwagger.default.scalar("/swagger"); to use Scalar instead.
|
||||
// return AutoSwagger.default.rapidoc("/swagger", "view"); to use RapiDoc instead.
|
||||
})
|
||||
@@ -0,0 +1,12 @@
|
||||
import { anyJsonRule } from '#validators/rules/any_json_rule'
|
||||
import { VineAny } from '@vinejs/vine'
|
||||
|
||||
VineAny.macro('anyJson', function (this: VineAny) {
|
||||
return this.use(anyJsonRule())
|
||||
})
|
||||
|
||||
declare module '@vinejs/vine' {
|
||||
interface VineAny {
|
||||
anyJson(): this
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user