-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Api cleanups, added ability to create api keys and send emails.
- Loading branch information
1 parent
5c6f74c
commit 534d8d4
Showing
3 changed files
with
130 additions
and
15 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 |
---|---|---|
|
@@ -16,8 +16,11 @@ import { | |
DPLADocList, | ||
FourHundredResponse, | ||
FiveHundredResponse, | ||
EmailSent, | ||
} from "./aggregation/responses"; | ||
import ApiKeyRepository from "./aggregation/api_key_repository"; | ||
import { SESClient } from "@aws-sdk/client-ses"; | ||
import { Emailer } from "./aggregation/Emailer"; | ||
|
||
const mustFork = | ||
process.env.MUST_FORK === "true" || process.env.NODE_ENV === "production"; | ||
|
@@ -56,6 +59,7 @@ function worker() { | |
const elasticsearchUrl = | ||
process.env.ELASTIC_URL || "http://search.internal.dp.la:9200/"; | ||
const elasticsearchIndex = process.env.ELASTIC_INDEX || "dpla_alias"; | ||
const emailFrom = process.env.EMAIL_FROM || "[email protected]"; | ||
|
||
const app: Application = express(); | ||
app.use(morgan("tiny")); //http request logger | ||
|
@@ -65,6 +69,7 @@ function worker() { | |
|
||
let s3 = new S3Client(awsOptions); | ||
let sqs = new SQSClient(awsOptions); | ||
let ses = new SESClient(awsOptions); | ||
|
||
if (xray) { | ||
const XRayExpress = AWSXRay.express; | ||
|
@@ -74,6 +79,7 @@ function worker() { | |
AWSXRay.captureHTTPsGlobal(https, true); | ||
sqs = AWSXRay.captureAWSClient(sqs); | ||
s3 = AWSXRay.captureAWSClient(s3); | ||
ses = AWSXRay.captureAWSClient(ses); | ||
} | ||
|
||
const dbPool: Pool = new Pool({ | ||
|
@@ -85,6 +91,8 @@ function worker() { | |
query_timeout: dbQueryTimeout, | ||
}); | ||
|
||
const emailer = new Emailer(ses, emailFrom); | ||
|
||
const apiKeyRepository = new ApiKeyRepository(dbPool); | ||
|
||
const authMiddleware = async ( | ||
|
@@ -109,7 +117,7 @@ function worker() { | |
if (!apiKeyRepository.isApiKeyValid(apiKey)) { | ||
return res.status(401).json({ message: "Unauthorized" }); | ||
} | ||
const user = await apiKeyRepository.findUserByApiKey(apiKey, dbPool); | ||
const user = await apiKeyRepository.findUserByApiKey(apiKey); | ||
|
||
if (!user) { | ||
return res.status(401).json({ message: "Unauthorized" }); | ||
|
@@ -162,9 +170,17 @@ function worker() { | |
}); | ||
|
||
//SEARCH | ||
const searchController = new SearchController(esClient); | ||
const searchController = new SearchController( | ||
esClient, | ||
apiKeyRepository, | ||
emailer, | ||
); | ||
const handleJsonResponses = ( | ||
response: DPLADocList | FourHundredResponse | FiveHundredResponse, | ||
response: | ||
| DPLADocList | ||
| EmailSent | ||
| FourHundredResponse | ||
| FiveHundredResponse, | ||
res: express.Response, | ||
) => { | ||
for (const [header, value] of Object.entries(securityHeaders)) { | ||
|
@@ -181,7 +197,6 @@ function worker() { | |
}; | ||
|
||
app.get(["/v2/items/:id", "/items/:id"], authMiddleware, async (req, res) => { | ||
console.log("IN: /v2/items/:id"); | ||
const response = await searchController.getItem( | ||
req.params.id, | ||
queryParams(req), | ||
|
@@ -200,6 +215,12 @@ function worker() { | |
handleJsonResponses(response, res); | ||
}); | ||
|
||
app.post(["/v2/api_key/:email", "/api_key:email"], async (req, res) => { | ||
const email = req.params.email; | ||
const response = await searchController.createApiKey(email); | ||
handleJsonResponses(response, res); | ||
}); | ||
|
||
app.listen(PORT, () => { | ||
console.log("Server is running on port", PORT); | ||
}); | ||
|