Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(planner/python): add sanic support #157

Merged
merged 7 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion internal/python/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand All @@ -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()
Expand Down Expand Up @@ -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.TrimSuffix(entryFile, ".py")
*wa = optional.Some(entryWithoutExt + ":" + match[1])
return wa.Unwrap()
}
return ""
}

return ""
}

Expand Down Expand Up @@ -535,6 +558,8 @@ 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 {
commandSegment = append(commandSegment, "sanic", wsgi, "--host 0.0.0.0", "--port "+wsgilistenedPort)
} else {
commandSegment = append(commandSegment, "gunicorn", "--bind :"+wsgilistenedPort, wsgi)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/types/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const (
PythonFrameworkFlask PythonFramework = "flask"
PythonFrameworkDjango PythonFramework = "django"
PythonFrameworkFastapi PythonFramework = "fastapi"
PythonFrameworkSanic PythonFramework = "sanic"
PythonFrameworkNone PythonFramework = "none"
)

Expand Down
1 change: 1 addition & 0 deletions tests/python-sanic/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sanic
12 changes: 12 additions & 0 deletions tests/python-sanic/server.py
Original file line number Diff line number Diff line change
@@ -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)
Loading