jonra1993 / fastapi-alembic-sqlmodel-async

This is a project template which uses FastAPI, Pydantic 2.0, Alembic and async SQLModel as ORM. It shows a complete async CRUD using authentication and role base access control.
MIT License
964 stars 150 forks source link

Not sure where to connect to the docker images after deploying to server #60

Closed jymchng closed 1 year ago

jymchng commented 1 year ago

Hi, using backend/docker-compose.yml I have deployed the docker containers to my private server. However, I am unable to curl or connect to it using my browser. Can you advise how I should do that?

Thank you.

jonra1993 commented 1 year ago

Hello @jymchng you make sure that you set your custom domain in environment variables. or use you public IP

image

EXT_ENDPOINT1=mycustomwebsite.com so that I can get access with both mycustomwebsite.com or fastapi.mycustomwebsite.com EXT_ENDPOINT1=172.10.10.10 so that I can get access to 172.10.10.10. Subdomains do not work with IP addresses, so I recommend you get a custom domain on servers.

jymchng commented 1 year ago

@jonra1993 Thank you for your reply. Just another follow up question, how can I get from HTTP to HTTPS? Thank you.

jonra1993 commented 1 year ago

Hello @jymchng this can be helpful. The project uses Caddy as reverse proxy which automatically creates SSL certificates https://github.com/jonra1993/fastapi-alembic-sqlmodel-async/issues/59#issuecomment-1493015271

Another option could be using cloudflare and providing a certificate using its free https proxy

jymchng commented 1 year ago

Hello @jonra1993, thank you for your helpful reply!

I have another question.

Let's say I have a Project table, shown below:

from sqlmodel import SQLModel

class Project(SQLModel, table=True):
    featured: bool = False

If I have an API that sets the Project.featured = True but I want it to be set to False 3 days later. What suggestions do you have to achieve this?

Thank you.

jonra1993 commented 1 year ago

Hello @jymchng I think that is a crontab task you can use celery beats and create a celery task after someone changes featured to True. Something like this https://github.com/jonra1993/fastapi-alembic-sqlmodel-async/blob/main/backend/app/app/api/v1/endpoints/periodic_tasks.py

on crontab you calculate 3 days after right now

periodic_task.crontab = CrontabSchedule(
        hour=22, minute=14, day_of_month=29, month_of_year=3, timezone="UTC"
    )
jymchng commented 1 year ago

Hi @jonra1993, thank you for this awesome template.

After I tried to deploy with your Caddyfile, attempting to enable HTTPS, this is the error I got:

caddy_reverse_proxy  | {"level":"error","ts":1680712202.3448455,"logger":"http.acme_client","msg":"validating authorization","identifier":"static.mycustomdomain.com","problem":{"type":"urn:ietf:params:acme:error:dns","title":"","detail":"DNS problem: NXDOMAIN looking up A for static.mycustomdomain.com - check that a DNS record exists for this domain; DNS problem: NXDOMAIN looking up AAAA for static.mycustomdomain.com - check that a DNS record 
exists for this domain","instance":"","subproblems":[]},"order":"https://acme-staging-v02.api.letsencrypt.org/acme/order/96894614/8124165414","attempt":2,"max_attempts":3}

I am running docker-compose-dev.yml for testing on my local machine. What should I do?

jonra1993 commented 1 year ago

Hello @jymchng you can not use SSL perfectly locally because you will require a public static IP also your local router should allow public connections and a DNS server which points A record to your PC public IP.

The error says that mycustomdomain.com is not in your local namespace and Cady can not generate a SSL certificate.

For development, I recommend you use HTTP. If you want to use SSL which is for production better use a remote server and connect a custom domain using a DNS server like Cloudflare which points the custom domain to your remote server. In such a situation, it is going to work perfectly. something like this

I hope it can help.

jymchng commented 1 year ago

Thank you for your response @jonra1993, I wish I can give you more stars than what you have now!