Skip to content

Commit

Permalink
Merge pull request zeabur#157 from CorrectRoadH/feat/sanic
Browse files Browse the repository at this point in the history
feat(planner/python): add sanic support
  • Loading branch information
yuaanlin authored Oct 16, 2023
2 parents 750f728 + 8ba3092 commit a0a552e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
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)

0 comments on commit a0a552e

Please sign in to comment.