You've already forked LibreTranslate
							
							
				mirror of
				https://github.com/LibreTranslate/LibreTranslate.git
				synced 2025-10-30 23:37:48 +02:00 
			
		
		
		
	wsgi support
This commit is contained in:
		
							
								
								
									
										15
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										15
									
								
								README.md
									
									
									
									
									
								
							| @@ -118,6 +118,21 @@ docker-compose up -d --build | ||||
| | --require-api-key-origin | Require use of an API key for programmatic access to the API, unless the request origin matches this domain | `No restrictions on domain origin` | | ||||
| | --load-only   | Set available languages    | `all from argostranslate`    | | ||||
|  | ||||
| ## Run with Gunicorn | ||||
|  | ||||
| ``` | ||||
| pip install gunicorn | ||||
| gunicorn --bind 0.0.0.0:5000 'wsgi:app' | ||||
| ``` | ||||
|  | ||||
| You can pass application arguments directly to Gunicorn via: | ||||
|  | ||||
|  | ||||
| ``` | ||||
| gunicorn --bind 0.0.0.0:5000 'wsgi:app(api_keys=True)' | ||||
| ``` | ||||
|  | ||||
|  | ||||
| ## Manage API Keys | ||||
|  | ||||
| LibreTranslate supports per-user limit quotas, e.g. you can issue API keys to users so that they can enjoy higher requests limits per minute (if you also set `--req-limit`). By default all users are rate-limited based on `--req-limit`, but passing an optional `api_key` parameter to the REST endpoints allows a user to enjoy higher request limits. | ||||
|   | ||||
							
								
								
									
										22
									
								
								app/main.py
									
									
									
									
									
								
							
							
						
						
									
										22
									
								
								app/main.py
									
									
									
									
									
								
							| @@ -1,4 +1,5 @@ | ||||
| import argparse | ||||
| import sys | ||||
| import operator | ||||
|  | ||||
| from app.app import create_app | ||||
| @@ -103,17 +104,20 @@ def main(): | ||||
|     args = parser.parse_args() | ||||
|     app = create_app(args) | ||||
|  | ||||
|     if args.debug: | ||||
|         app.run(host=args.host, port=args.port) | ||||
|     if sys.argv[0] == '--wsgi': | ||||
|         return app | ||||
|     else: | ||||
|         from waitress import serve | ||||
|         if args.debug: | ||||
|             app.run(host=args.host, port=args.port) | ||||
|         else: | ||||
|             from waitress import serve | ||||
|  | ||||
|         serve( | ||||
|             app, | ||||
|             host=args.host, | ||||
|             port=args.port, | ||||
|             url_scheme="https" if args.ssl else "http", | ||||
|         ) | ||||
|             serve( | ||||
|                 app, | ||||
|                 host=args.host, | ||||
|                 port=args.port, | ||||
|                 url_scheme="https" if args.ssl else "http", | ||||
|             ) | ||||
|  | ||||
|  | ||||
| if __name__ == "__main__": | ||||
|   | ||||
							
								
								
									
										19
									
								
								wsgi.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								wsgi.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,19 @@ | ||||
| from app import main | ||||
|  | ||||
| def app(*args, **kwargs): | ||||
|     import sys | ||||
|     sys.argv = ['--wsgi'] | ||||
|     for k in kwargs: | ||||
|         ck = k.replace("_", "-") | ||||
|         if isinstance(kwargs[k], bool) and kwargs[k]: | ||||
|             sys.argv.append("--" + ck) | ||||
|         else: | ||||
|             sys.argv.append("--" + ck) | ||||
|             sys.argv.append(kwargs[k]) | ||||
|  | ||||
|     instance = main() | ||||
|  | ||||
|     if len(kwargs) == 0: | ||||
|         return instance(*args, **kwargs) | ||||
|     else: | ||||
|         return instance | ||||
		Reference in New Issue
	
	Block a user