Skip to content

Commit

Permalink
refactor: merge http_service_provider with app_provider
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Oct 17, 2023
1 parent 819b5bf commit 6c247fc
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 282 deletions.
1 change: 0 additions & 1 deletion factories/core/ignitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export class IgnitorFactory {
return [
'@adonisjs/core/providers/app_provider',
'@adonisjs/core/providers/hash_provider',
'@adonisjs/core/providers/http_provider',
'@adonisjs/core/providers/repl_provider',
].concat(providers || [])
}
Expand Down
42 changes: 42 additions & 0 deletions providers/app_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import { Config } from '../modules/config.js'
import { Logger } from '../modules/logger.js'
import { Encryption } from '../modules/encryption.js'
import { Router, Server } from '../modules/http/main.js'
import type { ApplicationService, LoggerService } from '../src/types.js'
import BodyParserMiddleware from '../modules/bodyparser/bodyparser_middleware.js'

/**
* The Application Service provider registers all the baseline
Expand Down Expand Up @@ -96,6 +98,43 @@ export default class AppServiceProvider {
this.app.container.alias('encryption', Encryption)
}

/**
* Registers the HTTP server with the container as a singleton
*/
protected registerServer() {
this.app.container.singleton(Server, async (resolver) => {
const encryption = await resolver.make('encryption')
const emitter = await resolver.make('emitter')
const logger = await resolver.make('logger')
const config = this.app.config.get<any>('app.http')
return new Server(this.app, encryption, emitter, logger, config)
})

this.app.container.alias('server', Server)
}

/**
* Registers router with the container as a singleton
*/
protected registerRouter() {
this.app.container.singleton(Router, async (resolver) => {
const server = await resolver.make('server')
return server.getRouter()
})
this.app.container.alias('router', Router)
}

/**
* Self construct bodyparser middleware class, since it needs
* config that cannot be resolved by the container
*/
protected registerBodyParserMiddleware() {
this.app.container.bind(BodyParserMiddleware, () => {
const config = this.app.config.get<any>('bodyparser')
return new BodyParserMiddleware(config)
})
}

/**
* Registers bindings
*/
Expand All @@ -108,5 +147,8 @@ export default class AppServiceProvider {
this.registerEmitter()
this.registerEncryption()
this.registerTestUtils()
this.registerServer()
this.registerRouter()
this.registerBodyParserMiddleware()
}
}
2 changes: 1 addition & 1 deletion providers/hash_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default class HashServiceProvider {
const config = await configProvider.resolve<any>(this.app, hashConfigProvider)
if (!config) {
throw new RuntimeException(
'Invalid "config/hash.ts" file. Make sure you using the "defineConfig" method'
'Invalid "config/hash.ts" file. Make sure you are using the "defineConfig" method'
)
}

Expand Down
66 changes: 0 additions & 66 deletions providers/http_provider.ts

This file was deleted.

3 changes: 1 addition & 2 deletions tests/bindings/edge.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ test.group('Bindings | Edge', () => {
providers: [
'../providers/app_provider.js',
'../providers/hash_provider.js',
'../providers/http_provider.js',
'../providers/edge_provider.js',
],
},
Expand Down Expand Up @@ -60,7 +59,7 @@ test.group('Bindings | Edge', () => {
providers: [
'../providers/app_provider.js',
'../providers/hash_provider.js',
'../providers/http_provider.js',

'../providers/edge_provider.js',
],
},
Expand Down
6 changes: 1 addition & 5 deletions tests/bindings/repl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ test.group('Bindings | Repl', () => {
const ignitor = new IgnitorFactory()
.merge({
rcFileContents: {
providers: [
'../providers/app_provider.js',
'../providers/hash_provider.js',
'../providers/http_provider.js',
],
providers: ['../providers/app_provider.js', '../providers/hash_provider.js'],
},
})
.withCoreConfig()
Expand Down
66 changes: 11 additions & 55 deletions tests/cli_formatters/routes_list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,7 @@ test.group('Formatters | List routes | toJSON', () => {
.withCoreConfig()
.merge({
rcFileContents: {
providers: [
'./providers/app_provider.js',
'./providers/hash_provider.js',
'./providers/http_provider.js',
],
providers: ['./providers/app_provider.js', './providers/hash_provider.js'],
},
})
.create(fs.baseUrl, {
Expand Down Expand Up @@ -250,11 +246,7 @@ test.group('Formatters | List routes | toJSON', () => {
.withCoreConfig()
.merge({
rcFileContents: {
providers: [
'./providers/app_provider.js',
'./providers/hash_provider.js',
'./providers/http_provider.js',
],
providers: ['./providers/app_provider.js', './providers/hash_provider.js'],
},
})
.create(fs.baseUrl, {
Expand Down Expand Up @@ -402,11 +394,7 @@ test.group('Formatters | List routes | toJSON', () => {
.withCoreConfig()
.merge({
rcFileContents: {
providers: [
'./providers/app_provider.js',
'./providers/hash_provider.js',
'./providers/http_provider.js',
],
providers: ['./providers/app_provider.js', './providers/hash_provider.js'],
},
})
.create(fs.baseUrl, {
Expand Down Expand Up @@ -465,11 +453,7 @@ test.group('Formatters | List routes | toJSON', () => {
.withCoreConfig()
.merge({
rcFileContents: {
providers: [
'./providers/app_provider.js',
'./providers/hash_provider.js',
'./providers/http_provider.js',
],
providers: ['./providers/app_provider.js', './providers/hash_provider.js'],
},
})
.create(fs.baseUrl, {
Expand Down Expand Up @@ -546,11 +530,7 @@ test.group('Formatters | List routes | filters', () => {
.withCoreConfig()
.merge({
rcFileContents: {
providers: [
'./providers/app_provider.js',
'./providers/hash_provider.js',
'./providers/http_provider.js',
],
providers: ['./providers/app_provider.js', './providers/hash_provider.js'],
},
})
.create(fs.baseUrl, {
Expand Down Expand Up @@ -625,11 +605,7 @@ test.group('Formatters | List routes | filters', () => {
.withCoreConfig()
.merge({
rcFileContents: {
providers: [
'./providers/app_provider.js',
'./providers/hash_provider.js',
'./providers/http_provider.js',
],
providers: ['./providers/app_provider.js', './providers/hash_provider.js'],
},
})
.create(fs.baseUrl, {
Expand Down Expand Up @@ -747,11 +723,7 @@ test.group('Formatters | List routes | filters', () => {
.withCoreConfig()
.merge({
rcFileContents: {
providers: [
'./providers/app_provider.js',
'./providers/hash_provider.js',
'./providers/http_provider.js',
],
providers: ['./providers/app_provider.js', './providers/hash_provider.js'],
},
})
.create(fs.baseUrl, {
Expand Down Expand Up @@ -815,11 +787,7 @@ test.group('Formatters | List routes | filters', () => {
.withCoreConfig()
.merge({
rcFileContents: {
providers: [
'./providers/app_provider.js',
'./providers/hash_provider.js',
'./providers/http_provider.js',
],
providers: ['./providers/app_provider.js', './providers/hash_provider.js'],
},
})
.create(fs.baseUrl, {
Expand Down Expand Up @@ -873,11 +841,7 @@ test.group('Formatters | List routes | filters', () => {
.withCoreConfig()
.merge({
rcFileContents: {
providers: [
'./providers/app_provider.js',
'./providers/hash_provider.js',
'./providers/http_provider.js',
],
providers: ['./providers/app_provider.js', './providers/hash_provider.js'],
},
})
.create(fs.baseUrl, {
Expand Down Expand Up @@ -931,11 +895,7 @@ test.group('Formatters | List routes | filters', () => {
.withCoreConfig()
.merge({
rcFileContents: {
providers: [
'./providers/app_provider.js',
'./providers/hash_provider.js',
'./providers/http_provider.js',
],
providers: ['./providers/app_provider.js', './providers/hash_provider.js'],
},
})
.create(fs.baseUrl, {
Expand Down Expand Up @@ -1000,11 +960,7 @@ test.group('Formatters | List routes | filters', () => {
.withCoreConfig()
.merge({
rcFileContents: {
providers: [
'./providers/app_provider.js',
'./providers/hash_provider.js',
'./providers/http_provider.js',
],
providers: ['./providers/app_provider.js', './providers/hash_provider.js'],
},
})
.create(fs.baseUrl, {
Expand Down
6 changes: 1 addition & 5 deletions tests/hash.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,7 @@ test.group('Hash | provider', () => {
const ignitor = new IgnitorFactory()
.merge({
rcFileContents: {
providers: [
'../providers/app_provider.js',
'../providers/hash_provider.js',
'../providers/http_provider.js',
],
providers: ['../providers/app_provider.js', '../providers/hash_provider.js'],
},
})
.merge({
Expand Down
Loading

0 comments on commit 6c247fc

Please sign in to comment.