|
1 | | -from os import environ |
| 1 | +from pathlib import Path |
2 | 2 |
|
3 | 3 | import uvicorn |
4 | | -from fastapi import FastAPI, Request, Response, status |
| 4 | +from fastapi import FastAPI |
5 | 5 | from fastapi.middleware.cors import CORSMiddleware |
6 | | -from pydantic import BaseModel |
7 | | -from redis import Redis |
8 | 6 |
|
9 | | -from .beamline_parameters import ( |
10 | | - BEAMLINE_PARAMETER_PATHS, |
11 | | - GDABeamlineParameters, |
12 | | -) |
13 | | -from .constants import DATABASE_KEYS, ENDPOINTS |
14 | | - |
15 | | -DEV_MODE = bool(int(environ.get("DEV_MODE") or 0)) |
16 | | - |
17 | | -ROOT_PATH = "/api" |
18 | | -print(f"{DEV_MODE=}") |
19 | | -print(f"{ROOT_PATH=}") |
20 | | -if DEV_MODE: |
21 | | - print("Running in dev mode! not setting root path!") |
22 | | - ROOT_PATH = "" |
| 7 | +from daq_config_server.constants import ENDPOINTS |
23 | 8 |
|
24 | 9 | app = FastAPI( |
25 | 10 | title="DAQ config server", |
26 | | - description="""For storing and fetching beamline parameters, etc. which are needed |
27 | | - by more than one applicatioon or service""", |
28 | | - root_path=ROOT_PATH, |
| 11 | + description="""For reading files stored on /dls_sw from another container""", |
29 | 12 | ) |
30 | 13 | origins = ["*"] |
31 | 14 | app.add_middleware( |
|
36 | 19 | allow_headers=["*"], |
37 | 20 | ) |
38 | 21 |
|
39 | | -valkey = Redis(host="localhost", port=6379, decode_responses=True) |
40 | | - |
41 | 22 | __all__ = ["main"] |
42 | 23 |
|
43 | | -BEAMLINE_PARAM_PATH = "" |
44 | | -BEAMLINE_PARAMS: GDABeamlineParameters | None = None |
45 | | - |
46 | | - |
47 | | -@app.get(ENDPOINTS.BL_PARAM + "/{param}") |
48 | | -def get_beamline_parameter(param: str): |
49 | | - """Get a single beamline parameter""" |
50 | | - assert BEAMLINE_PARAMS is not None |
51 | | - return {param: BEAMLINE_PARAMS.params.get(param)} |
52 | | - |
53 | | - |
54 | | -class ParamList(BaseModel): |
55 | | - param_list: list[str] |
56 | | - |
57 | | - |
58 | | -@app.get(ENDPOINTS.BL_PARAM) |
59 | | -def get_all_beamline_parameters(param_list_data: ParamList | None = None): |
60 | | - """Get a dict of all the current beamline parameters.""" |
61 | | - assert BEAMLINE_PARAMS is not None |
62 | | - if param_list_data is None: |
63 | | - return BEAMLINE_PARAMS.params |
64 | | - return {k: BEAMLINE_PARAMS.params.get(k) for k in param_list_data.param_list} |
65 | | - |
66 | | - |
67 | | -@app.get(ENDPOINTS.FEATURE) |
68 | | -def get_feature_flag_list(get_values: bool = False): |
69 | | - """Get a list of all the current feature flags, or a dict of all the current values |
70 | | - if get_values=true is passed""" |
71 | | - flags = valkey.smembers(DATABASE_KEYS.FEATURE_SET) |
72 | | - if not get_values: |
73 | | - return flags |
74 | | - else: |
75 | | - return {flag: bool(int(valkey.get(flag))) for flag in flags} # type: ignore |
76 | | - |
77 | | - |
78 | | -@app.get(ENDPOINTS.FEATURE + "/{flag_name}") |
79 | | -def get_feature_flag(flag_name: str, response: Response): |
80 | | - """Get the value of a feature flag""" |
81 | | - if not valkey.sismember(DATABASE_KEYS.FEATURE_SET, flag_name): |
82 | | - response.status_code = status.HTTP_404_NOT_FOUND |
83 | | - return {"message": f"Feature flag {flag_name} does not exist!"} |
84 | | - else: |
85 | | - ret = int(valkey.get(flag_name)) # type: ignore # We checked if it exists above |
86 | | - return {flag_name: bool(ret)} |
87 | | - |
88 | | - |
89 | | -@app.post(ENDPOINTS.FEATURE + "/{flag_name}", status_code=status.HTTP_201_CREATED) |
90 | | -def create_feature_flag(flag_name: str, response: Response, value: bool = False): |
91 | | - """Sets a feature flag, creating it if it doesn't exist. Default to False.""" |
92 | | - if valkey.sismember(DATABASE_KEYS.FEATURE_SET, flag_name): |
93 | | - response.status_code = status.HTTP_409_CONFLICT |
94 | | - return {"message": f"Feature flag {flag_name} already exists!"} |
95 | | - else: |
96 | | - valkey.sadd(DATABASE_KEYS.FEATURE_SET, flag_name) |
97 | | - return {"success": valkey.set(flag_name, int(value))} |
98 | | - |
99 | | - |
100 | | -@app.put(ENDPOINTS.FEATURE + "/{flag_name}") |
101 | | -def set_feature_flag(flag_name: str, value: bool, response: Response): |
102 | | - """Sets a feature flag, return an error if it doesn't exist.""" |
103 | | - if not valkey.sismember(DATABASE_KEYS.FEATURE_SET, flag_name): |
104 | | - response.status_code = status.HTTP_404_NOT_FOUND |
105 | | - return {"message": f"Feature flag {flag_name} does not exist!"} |
106 | | - else: |
107 | | - return {"success": valkey.set(flag_name, int(value))} |
108 | | - |
109 | | - |
110 | | -@app.delete(ENDPOINTS.FEATURE + "/{flag_name}") |
111 | | -def delete_feature_flag(flag_name: str, response: Response): |
112 | | - """Delete a feature flag.""" |
113 | | - if not valkey.sismember(DATABASE_KEYS.FEATURE_SET, flag_name): |
114 | | - response.status_code = status.HTTP_404_NOT_FOUND |
115 | | - return {"message": f"Feature flag {flag_name} does not exist!"} |
116 | | - else: |
117 | | - valkey.srem(DATABASE_KEYS.FEATURE_SET, flag_name) |
118 | | - return {"success": not valkey.sismember(DATABASE_KEYS.FEATURE_SET, flag_name)} |
119 | | - |
120 | | - |
121 | | -@app.get(ENDPOINTS.INFO) |
122 | | -def get_info(request: Request): |
123 | | - """Get some generic information about the request, mostly for debugging""" |
124 | | - return { |
125 | | - "message": "Welcome to daq-config API.", |
126 | | - "root_path": request.scope.get("root_path"), |
127 | | - "request_headers": request.headers, |
128 | | - } |
129 | | - |
130 | | - |
131 | | -if DEV_MODE: |
132 | | - |
133 | | - @app.api_route("/{full_path:path}") |
134 | | - async def catch_all(request: Request, full_path: str): |
135 | | - return { |
136 | | - "message": "resource not found, supplying info for debug", |
137 | | - "root_path": request.scope.get("root_path"), |
138 | | - "path": full_path, |
139 | | - "request_headers": repr(request.headers), |
140 | | - } |
141 | | - |
142 | | - |
143 | | -def _load_beamline_params(): |
144 | | - global BEAMLINE_PARAMS |
145 | | - BEAMLINE_PARAMS = GDABeamlineParameters.from_file(BEAMLINE_PARAM_PATH) |
146 | 24 |
|
| 25 | +@app.get(ENDPOINTS.CONFIG + "/{file_path:path}") |
| 26 | +def get_configuration(file_path: Path): |
| 27 | + """Read a file and return its contents completely unformatted as a string. After |
| 28 | + https://github.com/DiamondLightSource/daq-config-server/issues/67, this endpoint |
| 29 | + will convert commonly read files to a dictionary format |
| 30 | + """ |
| 31 | + if not file_path.is_file(): |
| 32 | + raise FileNotFoundError(f"File {file_path} cannot be found") |
147 | 33 |
|
148 | | -def _set_beamline_param_path(dev: bool = True): |
149 | | - global BEAMLINE_PARAM_PATH |
150 | | - if dev: |
151 | | - BEAMLINE_PARAM_PATH = "tests/test_data/beamline_parameters.txt" |
152 | | - else: |
153 | | - BEAMLINE_PARAM_PATH = BEAMLINE_PARAMETER_PATHS["i03"] |
| 34 | + with file_path.open("r", encoding="utf-8") as f: |
| 35 | + return f.read() |
154 | 36 |
|
155 | 37 |
|
156 | | -def main(args): |
157 | | - _set_beamline_param_path(args.dev) |
158 | | - _load_beamline_params() |
| 38 | +def main(): |
159 | 39 | uvicorn.run(app="daq_config_server.app:app", host="0.0.0.0", port=8555) |
0 commit comments