-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(server): update environment configuration and enhance API st…
…ructure - Refactored environment configuration by introducing a new ConfigProviderLayer for improved management of environment variables. - Updated API structure by renaming the SuperTokens class to SupertokensService for better clarity. - Enhanced API middleware by implementing a custom authentication middleware for improved user context handling. - Cleaned up unused code and comments to streamline the codebase. - Improved logging and server address handling in the HTTP server setup.
- Loading branch information
1 parent
5917cb3
commit 1178d43
Showing
5 changed files
with
78 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,25 @@ | ||
import { Config, Effect, Layer } from 'effect' | ||
import { Config, ConfigProvider, Effect, Layer } from 'effect' | ||
|
||
// Environment configuration layer | ||
export class Env { | ||
static readonly Live = Layer.mergeAll( | ||
Layer.setConfigProvider( | ||
Effect.succeed({ | ||
load: (path: string) => Effect.succeed(process.env[path]), | ||
}), | ||
), | ||
) | ||
export const ConfigProviderLayer = Layer.setConfigProvider( | ||
ConfigProvider.fromJson(process.env), | ||
) | ||
|
||
static readonly load = Effect.all({ | ||
NODE_ENV: Config.string('NODE_ENV'), | ||
SERVER_PORT: Config.number('SERVER_PORT'), | ||
SUPERTOKENS_API_KEY: Config.string('SUPERTOKENS_API_KEY'), | ||
SUPERTOKENS_URL: Config.string('SUPERTOKENS_URL'), | ||
SERVER_URL: Config.string('SERVER_URL'), | ||
WEBSITE_URL: Config.string('WEBSITE_URL'), | ||
}) | ||
} | ||
// // Environment configuration layer | ||
// export class Env { | ||
// static readonly Live = Layer.mergeAll( | ||
// Layer.setConfigProvider( | ||
// Effect.succeed({ | ||
// load: (path: string) => Effect.succeed(process.env[path]), | ||
// }), | ||
// ), | ||
// ) | ||
|
||
// static readonly load = Effect.all({ | ||
// NODE_ENV: Config.string('NODE_ENV'), | ||
// SERVER_PORT: Config.number('SERVER_PORT'), | ||
// SUPERTOKENS_API_KEY: Config.string('SUPERTOKENS_API_KEY'), | ||
// SUPERTOKENS_URL: Config.string('SUPERTOKENS_URL'), | ||
// SERVER_URL: Config.string('SERVER_URL'), | ||
// WEBSITE_URL: Config.string('WEBSITE_URL'), | ||
// }) | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,38 @@ | ||
import { createServer } from 'node:http' | ||
import { HttpApiBuilder, HttpApiSwagger } from '@effect/platform' | ||
import { | ||
HttpApiBuilder, | ||
HttpApiSwagger, | ||
HttpMiddleware, | ||
HttpServer, | ||
} from '@effect/platform' | ||
import { NodeHttpServer, NodeRuntime } from '@effect/platform-node' | ||
import { Effect, Layer } from 'effect' | ||
import { Config, Layer } from 'effect' | ||
|
||
import { ApiLive } from './api/apis.js' | ||
import { withCorsMiddleware } from './api/cors.js' | ||
import { Env } from './env.js' | ||
import { SuperTokens } from './supertokens.js' | ||
import { MiddlewareCorsLive } from './api/cors.js' | ||
import { SupertokensService } from './supertokens.js' | ||
// import { Env } from './env.js' | ||
|
||
// TODO: init SuperTokens | ||
// const ServerConfig = Config.all({ | ||
// SERVER_PORT: Config.number('SERVER_PORT'), | ||
// }) | ||
|
||
const program = Effect.gen(function* () { | ||
const config = yield* Env.load | ||
// HTTP server implementation | ||
const ServerLive = NodeHttpServer.layer(() => createServer(), { | ||
port: 4040, | ||
}) | ||
|
||
const ServerLive = NodeHttpServer.layer(() => createServer(), { | ||
port: config.SERVER_PORT, | ||
}) | ||
|
||
const HttpLive = HttpApiBuilder.serve(withCorsMiddleware).pipe( | ||
Layer.provide(HttpApiSwagger.layer({ path: '/docs' })), | ||
// Register API with HTTP server | ||
const HttpLive = HttpApiBuilder.serve(HttpMiddleware.logger) | ||
.pipe( | ||
Layer.provide(MiddlewareCorsLive), | ||
Layer.provide(SupertokensService.layer), | ||
Layer.provide(ApiLive), | ||
Layer.provide(ServerLive), | ||
HttpServer.withLogAddress, // Log server address | ||
Layer.provide(HttpApiSwagger.layer({ path: '/docs' })), | ||
) | ||
.pipe(Layer.provide(ServerLive)) | ||
|
||
return yield* Layer.launch(HttpLive) | ||
}).pipe(Effect.provide(Env.Live)) | ||
|
||
NodeRuntime.runMain(program) | ||
// Run server | ||
NodeRuntime.runMain(Layer.launch(HttpLive)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters