From cdaffd45f78ccafdab0a9ed0d677c54f9582968c Mon Sep 17 00:00:00 2001 From: CorrectRoadH Date: Mon, 16 Oct 2023 12:44:51 +0800 Subject: [PATCH 1/7] feat: add sanic support --- internal/python/plan.go | 25 ++++++++++++++++++++++++- pkg/types/plan.go | 1 + 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/internal/python/plan.go b/internal/python/plan.go index 7db83e0f..cabe2820 100644 --- a/internal/python/plan.go +++ b/internal/python/plan.go @@ -53,6 +53,11 @@ func DetermineFramework(ctx *pythonPlanContext) types.PythonFramework { return fw.Unwrap() } + if HasDependencyWithFile(ctx, "sanic") { + *fw = optional.Some(types.PythonFrameworkSanic) + return fw.Unwrap() + } + *fw = optional.Some(types.PythonFrameworkNone) return fw.Unwrap() } @@ -66,7 +71,7 @@ func DetermineEntry(ctx *pythonPlanContext) string { return entry } - for _, file := range []string{"main.py", "app.py", "manage.py"} { + for _, file := range []string{"main.py", "app.py", "manage.py", "server.py"} { if utils.HasFile(src, file) { *et = optional.Some(file) return et.Unwrap() @@ -261,6 +266,24 @@ func DetermineWsgi(ctx *pythonPlanContext) string { return "" } + if framework == types.PythonFrameworkSanic { + entryFile := DetermineEntry(ctx) + + re := regexp.MustCompile(`(\w+)\s*=\s*Sanic\([^)]*\)`) + content, err := afero.ReadFile(src, entryFile) + if err != nil { + return "" + } + + match := re.FindStringSubmatch(string(content)) + if len(match) > 1 { + entryWithoutExt := strings.Replace(entryFile, ".py", "", 1) + *wa = optional.Some(entryWithoutExt + ":" + match[1]) + return wa.Unwrap() + } + return "" + } + return "" } diff --git a/pkg/types/plan.go b/pkg/types/plan.go index 9eec0a6f..2d7a5936 100644 --- a/pkg/types/plan.go +++ b/pkg/types/plan.go @@ -80,6 +80,7 @@ const ( PythonFrameworkFlask PythonFramework = "flask" PythonFrameworkDjango PythonFramework = "django" PythonFrameworkFastapi PythonFramework = "fastapi" + PythonFrameworkSanic PythonFramework = "sanic" PythonFrameworkNone PythonFramework = "none" ) From 9de1c8731ec73f359493b53de9a2de07115540a8 Mon Sep 17 00:00:00 2001 From: CorrectRoadH Date: Mon, 16 Oct 2023 18:13:31 +0800 Subject: [PATCH 2/7] feat: change start cmd --- internal/python/plan.go | 4 ++++ .../__pycache__/server.cpython-38.pyc | Bin 0 -> 559 bytes tests/python-sanic/requirements.txt | 1 + tests/python-sanic/server.py | 12 ++++++++++++ 4 files changed, 17 insertions(+) create mode 100644 tests/python-sanic/__pycache__/server.cpython-38.pyc create mode 100644 tests/python-sanic/requirements.txt create mode 100644 tests/python-sanic/server.py diff --git a/internal/python/plan.go b/internal/python/plan.go index cabe2820..a624fe40 100644 --- a/internal/python/plan.go +++ b/internal/python/plan.go @@ -558,6 +558,10 @@ func determineStartCmd(ctx *pythonPlanContext) string { if framework == types.PythonFrameworkFastapi { commandSegment = append(commandSegment, "uvicorn", wsgi, "--host 0.0.0.0", "--port "+wsgilistenedPort) + } + if framework == types.PythonFrameworkSanic { + commandSegment = append(commandSegment, "sanic", wsgi, "--host 0.0.0.0", "--port "+wsgilistenedPort) + } else { commandSegment = append(commandSegment, "gunicorn", "--bind :"+wsgilistenedPort, wsgi) } diff --git a/tests/python-sanic/__pycache__/server.cpython-38.pyc b/tests/python-sanic/__pycache__/server.cpython-38.pyc new file mode 100644 index 0000000000000000000000000000000000000000..f54f0bdd7cda3cbf8af7e156b05e1b3e4ba87b03 GIT binary patch literal 559 zcmYjN&2G~`5Z<-dvE%%dHz-$25K|3nw4g=q)*vSpwq}^qk7n{^z zmrmPjvzE<5~Ib0iYUgu@QdDlOB{dV#S-%)vdNXWe_ zpC-(*wbaJ4Rw`q|?^10L+{SrPfY!W{tFvTagFTvZgY9_lPZa1@Hi*w$($Cx0_6pg2 zDOdK{>G}NQhxy6ugg+d~`YSMxe44xl&aJPu&3@x2#?iOYyIP8E1@GMz^%%{N4!wv* IsIec>|4U$vJ^%m! literal 0 HcmV?d00001 diff --git a/tests/python-sanic/requirements.txt b/tests/python-sanic/requirements.txt new file mode 100644 index 00000000..fe6e6292 --- /dev/null +++ b/tests/python-sanic/requirements.txt @@ -0,0 +1 @@ +sanic diff --git a/tests/python-sanic/server.py b/tests/python-sanic/server.py new file mode 100644 index 00000000..5b73dcde --- /dev/null +++ b/tests/python-sanic/server.py @@ -0,0 +1,12 @@ +from sanic import Sanic +from sanic.response import text + +app = Sanic("proxied_example") +app.config.FORWARDED_SECRET = "YOUR SECRET" + +@app.get("/zeabur") +def index(request): + return text("zeabur") + +if __name__ == "__main__": + app.run(host="127.0.0.1", port=8000, workers=8, access_log=False) From 0bd239f44eaa2db4ed701816ff7bb08c802e21ff Mon Sep 17 00:00:00 2001 From: CorrectRoadH Date: Mon, 16 Oct 2023 18:15:41 +0800 Subject: [PATCH 3/7] chore: delete unused line --- internal/python/plan.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/python/plan.go b/internal/python/plan.go index a624fe40..ceb273e5 100644 --- a/internal/python/plan.go +++ b/internal/python/plan.go @@ -561,7 +561,6 @@ func determineStartCmd(ctx *pythonPlanContext) string { } if framework == types.PythonFrameworkSanic { commandSegment = append(commandSegment, "sanic", wsgi, "--host 0.0.0.0", "--port "+wsgilistenedPort) - } else { commandSegment = append(commandSegment, "gunicorn", "--bind :"+wsgilistenedPort, wsgi) } From 92696dd0cf36c90b19f71ba15e1c84b17a861cc1 Mon Sep 17 00:00:00 2001 From: CorrectRoadH Date: Mon, 16 Oct 2023 05:32:28 -0500 Subject: [PATCH 4/7] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20plan.go?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: pan93412 --- internal/python/plan.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/python/plan.go b/internal/python/plan.go index ceb273e5..cfca0877 100644 --- a/internal/python/plan.go +++ b/internal/python/plan.go @@ -559,7 +559,7 @@ func determineStartCmd(ctx *pythonPlanContext) string { if framework == types.PythonFrameworkFastapi { commandSegment = append(commandSegment, "uvicorn", wsgi, "--host 0.0.0.0", "--port "+wsgilistenedPort) } - if framework == types.PythonFrameworkSanic { + else if framework == types.PythonFrameworkSanic { commandSegment = append(commandSegment, "sanic", wsgi, "--host 0.0.0.0", "--port "+wsgilistenedPort) } else { commandSegment = append(commandSegment, "gunicorn", "--bind :"+wsgilistenedPort, wsgi) From fbc9fb0c16f874342a33b08fa925619b9c15b941 Mon Sep 17 00:00:00 2001 From: CorrectRoadH Date: Mon, 16 Oct 2023 05:32:33 -0500 Subject: [PATCH 5/7] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20plan.go?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: pan93412 --- internal/python/plan.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/python/plan.go b/internal/python/plan.go index cfca0877..557f88d6 100644 --- a/internal/python/plan.go +++ b/internal/python/plan.go @@ -277,7 +277,7 @@ func DetermineWsgi(ctx *pythonPlanContext) string { match := re.FindStringSubmatch(string(content)) if len(match) > 1 { - entryWithoutExt := strings.Replace(entryFile, ".py", "", 1) + entryWithoutExt := strings.TrimSuffix(entryFile, ".py") *wa = optional.Some(entryWithoutExt + ":" + match[1]) return wa.Unwrap() } From 216d095548c36b009c46f782c435dd90d95ea9e2 Mon Sep 17 00:00:00 2001 From: CorrectRoadH Date: Mon, 16 Oct 2023 19:25:52 +0800 Subject: [PATCH 6/7] chore: delete unused file --- .../__pycache__/server.cpython-38.pyc | Bin 559 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 tests/python-sanic/__pycache__/server.cpython-38.pyc diff --git a/tests/python-sanic/__pycache__/server.cpython-38.pyc b/tests/python-sanic/__pycache__/server.cpython-38.pyc deleted file mode 100644 index f54f0bdd7cda3cbf8af7e156b05e1b3e4ba87b03..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 559 zcmYjN&2G~`5Z<-dvE%%dHz-$25K|3nw4g=q)*vSpwq}^qk7n{^z zmrmPjvzE<5~Ib0iYUgu@QdDlOB{dV#S-%)vdNXWe_ zpC-(*wbaJ4Rw`q|?^10L+{SrPfY!W{tFvTagFTvZgY9_lPZa1@Hi*w$($Cx0_6pg2 zDOdK{>G}NQhxy6ugg+d~`YSMxe44xl&aJPu&3@x2#?iOYyIP8E1@GMz^%%{N4!wv* IsIec>|4U$vJ^%m! From cd522e05082767d7e822e231f5e72bfe2f9115e6 Mon Sep 17 00:00:00 2001 From: CorrectRoadH Date: Mon, 16 Oct 2023 21:10:05 +0800 Subject: [PATCH 7/7] chore: fix lint --- internal/python/plan.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/internal/python/plan.go b/internal/python/plan.go index 557f88d6..cb81648b 100644 --- a/internal/python/plan.go +++ b/internal/python/plan.go @@ -558,8 +558,7 @@ func determineStartCmd(ctx *pythonPlanContext) string { if framework == types.PythonFrameworkFastapi { commandSegment = append(commandSegment, "uvicorn", wsgi, "--host 0.0.0.0", "--port "+wsgilistenedPort) - } - else if framework == types.PythonFrameworkSanic { + } else if framework == types.PythonFrameworkSanic { commandSegment = append(commandSegment, "sanic", wsgi, "--host 0.0.0.0", "--port "+wsgilistenedPort) } else { commandSegment = append(commandSegment, "gunicorn", "--bind :"+wsgilistenedPort, wsgi)