1- import express from "express" ;
1+ import express , { RequestHandler } from "express" ;
22import cors from "cors" ;
33import { PrismaClient } from "@prisma/client" ;
44
5+ const { WEB_ORIGIN , API_PASSWORD } = process . env ;
6+
57const app = express ( ) ;
68const client = new PrismaClient ( ) ;
79
8- app . use ( cors ( { origin : process . env [ " WEB_ORIGIN" ] } ) ) ;
10+ app . use ( cors ( { origin : WEB_ORIGIN } ) ) ;
911app . use ( express . json ( ) ) ;
1012
13+ const requirePassword : RequestHandler = ( request , response , next ) => {
14+ if ( request . headers . authorization === API_PASSWORD ) next ( ) ;
15+ else response . sendStatus ( 401 ) . send ( ) ;
16+ } ;
17+
1118// 通信テスト
1219
1320app . get ( "/" , ( _ , res ) => {
@@ -28,7 +35,7 @@ type UserResponse = {
2835 rank : number | undefined ;
2936} ;
3037
31- app . post ( "/user" , async ( request , response ) => {
38+ app . post ( "/user" , requirePassword , async ( request , response ) => {
3239 const requestBody : PostUserRequest = request . body ;
3340 try {
3441 await client . user . create ( {
@@ -95,7 +102,7 @@ type PutUserRequest = {
95102 name : string ;
96103} ;
97104
98- app . put ( "/user/:userId([0-9]+)" , async ( request , response ) => {
105+ app . put ( "/user/:userId([0-9]+)" , requirePassword , async ( request , response ) => {
99106 const requestParams : PutUserParams = {
100107 userId : Number ( request . params [ "userId" ] ) ,
101108 } ;
@@ -148,16 +155,22 @@ type PutProgramRequest = {
148155 program : string ;
149156} ;
150157
151- app . put ( "/program" , async ( request , response ) => {
158+ app . put ( "/program" , requirePassword , async ( request , response ) => {
152159 const requestBody : PutProgramRequest = request . body ;
153160 const participantNumber = await client . userBattleIdentity . count ( ) ;
154- await client . userBattleIdentity . create ( {
155- data : {
161+ await client . userBattleIdentity . upsert ( {
162+ create : {
156163 userId : requestBody . userId ,
157164 program : requestBody . program ,
158165 league : Math . floor ( ( participantNumber + 5 ) / 4 ) , // 作成された順にリーグ番号が割り振られる
159166 rank : requestBody . userId , // ひとまずidと同じ番号を挿入
160167 } ,
168+ update : {
169+ program : requestBody . program ,
170+ } ,
171+ where : {
172+ userId : requestBody . userId ,
173+ } ,
161174 } ) ;
162175 response . send ( ) ;
163176} ) ;
@@ -169,7 +182,7 @@ type PostSwapRankRequest = {
169182 userId2 : number ;
170183} ;
171184
172- app . post ( "/swap-rank" , async ( request , response ) => {
185+ app . post ( "/swap-rank" , requirePassword , async ( request , response ) => {
173186 const requestBody : PostSwapRankRequest = request . body ;
174187 const user1 = await client . userBattleIdentity . findUnique ( {
175188 where : { userId : requestBody . userId1 } ,
@@ -207,4 +220,14 @@ app.post("/swap-rank", async (request, response) => {
207220 response . send ( ) ;
208221} ) ;
209222
223+ type PostCheckPasswordRequest = {
224+ password : string ;
225+ } ;
226+
227+ app . post ( "/check-password" , ( request , response ) => {
228+ const requestBody : PostCheckPasswordRequest = request . body ;
229+ if ( requestBody . password === API_PASSWORD ) response . sendStatus ( 200 ) ;
230+ else response . sendStatus ( 401 ) ;
231+ } ) ;
232+
210233app . listen ( 8081 ) ;
0 commit comments