From c058a5ad73ef6fae0552886cac15d8b588874f8b Mon Sep 17 00:00:00 2001 From: leeduckgo Date: Sun, 26 Nov 2023 18:03:46 +0800 Subject: [PATCH] update with auth --- config/config.exs | 3 +- lib/components/movespace_db.ex | 3 +- .../controllers/function_runner_controller.ex | 31 +++++++++++++++---- lib/utils/constants.ex | 4 +++ 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/config/config.exs b/config/config.exs index 6638079..e6503bd 100644 --- a/config/config.exs +++ b/config/config.exs @@ -27,7 +27,8 @@ config :tai_shang_micro_faas_system, github_token: System.get_env("GITHUB_TOKEN"), embedbase_key: System.get_env("EMBEDBASE_KEY"), did_mainnet: "0x61b96051f553d767d7e6dfcc04b04c28d793c8af3d07d3a43b4e2f8f4ca04c9f", - did_testnet: "0xc71124a51e0d63cfc6eb04e690c39a4ea36774ed4df77c00f7cbcbc9d0505b2c" + did_testnet: "0xc71124a51e0d63cfc6eb04e690c39a4ea36774ed4df77c00f7cbcbc9d0505b2c", + api_key: System.get_env("API_KEY") config :cors_plug, max_age: 2592000, diff --git a/lib/components/movespace_db.ex b/lib/components/movespace_db.ex index af30f3d..5e775cd 100644 --- a/lib/components/movespace_db.ex +++ b/lib/components/movespace_db.ex @@ -14,8 +14,7 @@ defmodule Components.MovespaceDB do end def insert_vector(vector_db_name, id_in_embedbase, raw_data, meta_data, embedding) do - IO.puts "tttttt" - IO.puts raw_data + IO.puts "insert_vector" sql_cmd = """ INSERT INTO #{vector_db_name} (id_in_embedbase, raw_data, meta_data, embedding) VALUES ( '#{id_in_embedbase}', diff --git a/lib/tai_shang_micro_faas_system_web/controllers/function_runner_controller.ex b/lib/tai_shang_micro_faas_system_web/controllers/function_runner_controller.ex index bdb9e13..9f576ec 100644 --- a/lib/tai_shang_micro_faas_system_web/controllers/function_runner_controller.ex +++ b/lib/tai_shang_micro_faas_system_web/controllers/function_runner_controller.ex @@ -50,14 +50,33 @@ defmodule TaiShangMicroFaasSystemWeb.FunctionRunnerController do end def run(conn, payload) do - result = - payload - |> ExStructTranslator.to_atom_struct() - |> do_run() - |> handle_param() - json(conn, %{result: result}) + # TODO: optimize here. + if is_nil(Constants.get_api_key()) do + result = + payload + |> ExStructTranslator.to_atom_struct() + |> do_run() + |> handle_param() + json(conn, %{result: result}) + else + atomed_payload = + %{api_key: api_key} = + ExStructTranslator.to_atom_struct(payload) + + result = + if guide(api_key) do + atomed_payload + |> do_run() + |> handle_param() + else + handle_param({:error, "wrong api key!"}) + end + json(conn, %{result: result}) + end end + def guide(api_key), do: api_key == Constants.get_api_key() + def do_run(%{ tx_id: tx_id, func_name: func_name, diff --git a/lib/utils/constants.ex b/lib/utils/constants.ex index 152b081..9e5ca38 100644 --- a/lib/utils/constants.ex +++ b/lib/utils/constants.ex @@ -53,4 +53,8 @@ defmodule Constants do Application.fetch_env!(:tai_shang_micro_faas_system, :did_testnet) end + def get_api_key() do + Application.fetch_env!(:tai_shang_micro_faas_system, :api_key) + end + end