Repository navigation
feat: 支持通过 APP_BASE_PATH 配置子路径部署 #4433
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
Open
Adcbda
wants to merge
3
commits into
QuantumNous:main
Choose a base branch
from
Adcbda:feat-sub-path
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,121
−176
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| package common | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "path" | ||
| "strings" | ||
| ) | ||
|
|
||
| var AppBasePath = "" | ||
|
|
||
| func NormalizeBasePath(raw string) (string, error) { | ||
| trimmed := strings.TrimSpace(raw) | ||
| if trimmed == "" || trimmed == "/" { | ||
| return "", nil | ||
| } | ||
| if !strings.HasPrefix(trimmed, "/") { | ||
| return "", fmt.Errorf("APP_BASE_PATH must start with '/'") | ||
| } | ||
| if strings.ContainsAny(trimmed, "?#") { | ||
| return "", fmt.Errorf("APP_BASE_PATH must not contain query or fragment") | ||
| } | ||
|
|
||
| normalized := strings.TrimRight(trimmed, "/") | ||
| if normalized == "" { | ||
| return "", nil | ||
| } | ||
| cleaned := path.Clean(normalized) | ||
| if cleaned != normalized { | ||
| return "", fmt.Errorf("APP_BASE_PATH contains invalid path segments") | ||
| } | ||
| return normalized, nil | ||
| } | ||
|
|
||
| func SessionCookiePath() string { | ||
| if AppBasePath == "" { | ||
| return "/" | ||
| } | ||
| return AppBasePath | ||
| } | ||
|
|
||
| func WithAppBasePath(routePath string) string { | ||
| if AppBasePath == "" { | ||
| if routePath == "" { | ||
| return "/" | ||
| } | ||
| return routePath | ||
| } | ||
|
|
||
| if routePath == "" || routePath == "/" { | ||
| return AppBasePath | ||
| } | ||
|
|
||
| normalized := routePath | ||
| if !strings.HasPrefix(normalized, "/") { | ||
| normalized = "/" + normalized | ||
| } | ||
| if normalized == AppBasePath || strings.HasPrefix(normalized, AppBasePath+"/") { | ||
| return normalized | ||
| } | ||
| return AppBasePath + normalized | ||
| } | ||
|
|
||
| func StripAppBasePath(requestPath string) (string, bool) { | ||
| if AppBasePath == "" { | ||
| if requestPath == "" { | ||
| return "/", true | ||
| } | ||
| return requestPath, true | ||
| } | ||
|
|
||
| if requestPath == AppBasePath { | ||
| return "/", true | ||
| } | ||
| if strings.HasPrefix(requestPath, AppBasePath+"/") { | ||
| stripped := strings.TrimPrefix(requestPath, AppBasePath) | ||
| if stripped == "" { | ||
| return "/", true | ||
| } | ||
| return stripped, true | ||
| } | ||
| return "", false | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package common | ||
|
|
||
| import "testing" | ||
|
|
||
| func TestNormalizeBasePath(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| input string | ||
| want string | ||
| wantErr bool | ||
| }{ | ||
| {name: "empty", input: "", want: ""}, | ||
| {name: "root", input: "/", want: ""}, | ||
| {name: "single segment", input: "/new-api", want: "/new-api"}, | ||
| {name: "strip trailing slash", input: "/new-api/", want: "/new-api"}, | ||
| {name: "nested path", input: "/foo/bar", want: "/foo/bar"}, | ||
| {name: "missing leading slash", input: "new-api", wantErr: true}, | ||
| {name: "double slash", input: "/foo//bar", wantErr: true}, | ||
| {name: "dot segment", input: "/foo/./bar", wantErr: true}, | ||
| {name: "dot dot segment", input: "/foo/../bar", wantErr: true}, | ||
| {name: "query fragment", input: "/foo?bar", wantErr: true}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got, err := NormalizeBasePath(tt.input) | ||
| if tt.wantErr { | ||
| if err == nil { | ||
| t.Fatalf("expected error, got nil and %q", got) | ||
| } | ||
| return | ||
| } | ||
| if err != nil { | ||
| t.Fatalf("NormalizeBasePath() error = %v", err) | ||
| } | ||
| if got != tt.want { | ||
| t.Fatalf("NormalizeBasePath() = %q, want %q", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestWithAppBasePath(t *testing.T) { | ||
| original := AppBasePath | ||
| t.Cleanup(func() { | ||
| AppBasePath = original | ||
| }) | ||
|
|
||
| AppBasePath = "/new-api" | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| path string | ||
| want string | ||
| }{ | ||
| {name: "root", path: "/", want: "/new-api"}, | ||
| {name: "console", path: "/console", want: "/new-api/console"}, | ||
| {name: "already prefixed", path: "/new-api/console", want: "/new-api/console"}, | ||
| {name: "empty", path: "", want: "/new-api"}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| if got := WithAppBasePath(tt.path); got != tt.want { | ||
| t.Fatalf("WithAppBasePath() = %q, want %q", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package common | ||
|
|
||
| import "testing" | ||
|
|
||
| func TestStartupURL(t *testing.T) { | ||
| original := AppBasePath | ||
| t.Cleanup(func() { | ||
| AppBasePath = original | ||
| }) | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| basePath string | ||
| want string | ||
| }{ | ||
| { | ||
| name: "without base path", | ||
| basePath: "", | ||
| want: "http://localhost:3000/", | ||
| }, | ||
| { | ||
| name: "with base path", | ||
| basePath: "/app", | ||
| want: "http://localhost:3000/app/", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| AppBasePath = tt.basePath | ||
|
|
||
| if got := startupURL("localhost", "3000"); got != tt.want { | ||
| t.Fatalf("startupURL() = %q, want %q", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Healthcheck URL construction is sensitive to APP_BASE_PATH formatting.
The expansion
http://localhost:3000${APP_BASE_PATH}/api/statusassumesAPP_BASE_PATHhas no trailing slash. If a user setsAPP_BASE_PATH=/new-api/, the resulting URL becomeshttp://localhost:3000/new-api//api/status, which won't match the Gin routes (backend normalizesAppBasePathby trimming trailing slashes, so the registered route is/new-api/api/status). Consider documenting this constraint in the adjacent comment on line 37, or using a shell normalization in the healthcheck command.🤖 Prompt for AI Agents