Skip to content

Commit 7987c48

Browse files
Add initial documentation around CORS usage
1 parent 8aaafcc commit 7987c48

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

FAQ.md

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ For more examples, check out Hug's [documentation](https://github.com/timothycro
66

77
Q: *Can I use Hug with a web framework -- Django for example?*
88

9-
A: You can use Hug alongside Django or the web framework of your choice, but it does have drawbacks. You would need to run hug on a separate, hug-exclusive server. You can also [mount Hug as a WSGI app](https://pythonhosted.org/django-wsgi/embedded-apps.html), embedded within your normal Django app.
9+
A: You can use Hug alongside Django or the web framework of your choice, but it does have drawbacks. You would need to run hug on a separate, hug-exclusive server. You can also [mount Hug as a WSGI app](https://pythonhosted.org/django-wsgi/embedded-apps.html), embedded within your normal Django app.
1010

1111
Q: *Is Hug compatabile with Python 2?*
1212

13-
A: Python 2 is not supported by Hug. However, if you need to account for backwards compatability, there are workarounds. For example, you can wrap the decorators:
13+
A: Python 2 is not supported by Hug. However, if you need to account for backwards compatability, there are workarounds. For example, you can wrap the decorators:
1414

15-
```Python
15+
```Python
1616
def my_get_fn(func, *args, **kwargs):
1717
if 'hug' in globals():
1818
return hug.get(func, *args, **kwargs)
@@ -29,7 +29,7 @@ Q: *How can I serve static files from a directory using Hug?*
2929

3030
A: For a static HTML page, you can just set the proper output format as: `output=hug.output_format.html`. To see other examples, check out the [html_serve](https://github.com/timothycrosley/hug/blob/develop/examples/html_serve.py) example, the [image_serve](https://github.com/timothycrosley/hug/blob/develop/examples/image_serve.py) example, and the more general [static_serve](https://github.com/timothycrosley/hug/blob/develop/examples/static_serve.py) example within `hug/examples`.
3131

32-
Most basic examples will use a format that looks something like this:
32+
Most basic examples will use a format that looks something like this:
3333

3434
```Python
3535
@hug.static('/static')
@@ -50,15 +50,15 @@ A: You can access a list of your routes by using the routes object on the HTTP A
5050

5151
`__hug_wsgi__.http.routes`
5252

53-
It will return to you a structure of "base_url -> url -> HTTP method -> Version -> Python Handler". Therefore, for example, if you have no base_url set and you want to see the list of all URLS, you could run:
53+
It will return to you a structure of "base_url -> url -> HTTP method -> Version -> Python Handler". Therefore, for example, if you have no base_url set and you want to see the list of all URLS, you could run:
5454

5555
`__hug_wsgi__.http.routes[''].keys()`
5656

5757
Q: *How can I configure a unique 404 route?*
5858

5959
A: By default, Hug will call `documentation_404()` if no HTTP route is found. However, if you want to configure other options (such as routing to a directiory, or routing everything else to a landing page) you can use the `@hug.sink('/')` decorator to create a "catch-all" route:
6060

61-
```Python
61+
```Python
6262
import hug
6363

6464
@hug.sink('/all')
@@ -67,3 +67,23 @@ def my_sink(request):
6767
```
6868

6969
For more information, check out the ROUTING.md file within the `hug/documentation` directory.
70+
71+
Q: *How can I enable CORS*
72+
73+
A: There are many solutions depending on the specifics of your application.
74+
For most applications, you can use the included cors middleware:
75+
76+
```
77+
import hug
78+
79+
api = hug.API(__name__)
80+
api.http.add_middleware(hug.middleware.CORSMiddleware(api, max_age=10))
81+
82+
83+
@hug.get("/demo")
84+
def get_demo():
85+
return {"result": "Hello World"}
86+
```
87+
For cases that are more complex then the middleware handles
88+
89+
[This comment]([https://github.com/hugapi/hug/issues/114#issuecomment-342493165]) (and the discussion around it) give a good starting off point.

0 commit comments

Comments
 (0)