yankeguo commited on
Commit
a8d486a
·
unverified ·
1 Parent(s): 0c2642d

feat: add secret_key validation

Browse files
Files changed (1) hide show
  1. main.py +12 -1
main.py CHANGED
@@ -1,12 +1,22 @@
1
- from flask import Flask, request, jsonify
2
  import os
3
  import stanza
4
 
 
 
5
  nlp = stanza.Pipeline("en", processors="tokenize", download_method=None)
6
 
7
  app = Flask(__name__)
8
 
9
 
 
 
 
 
 
 
 
 
10
  @app.route("/")
11
  def action_index():
12
  return "Hello, World!"
@@ -14,5 +24,6 @@ def action_index():
14
 
15
  @app.route("/sentence_segmentation", methods=["POST"])
16
  def action_sentences_segment():
 
17
  doc = nlp(request.get_json()["document"])
18
  return jsonify({"sentences": [sentence.text for sentence in doc.sentences]})
 
1
+ from flask import Flask, request, jsonify, abort
2
  import os
3
  import stanza
4
 
5
+ secret_key = os.getenv("SECRET_KEY", "")
6
+
7
  nlp = stanza.Pipeline("en", processors="tokenize", download_method=None)
8
 
9
  app = Flask(__name__)
10
 
11
 
12
+ def guard_secret_key():
13
+ if request.args.get("secret_key", "") == secret_key:
14
+ return
15
+ if request.headers.get("X-Secret-Key", "") == secret_key:
16
+ return
17
+ abort(401)
18
+
19
+
20
  @app.route("/")
21
  def action_index():
22
  return "Hello, World!"
 
24
 
25
  @app.route("/sentence_segmentation", methods=["POST"])
26
  def action_sentences_segment():
27
+ guard_secret_key()
28
  doc = nlp(request.get_json()["document"])
29
  return jsonify({"sentences": [sentence.text for sentence in doc.sentences]})