Skip to content

Commit 9ee588f

Browse files
committed
feat: add option to use virtual empty root directory
1 parent af75ca3 commit 9ee588f

File tree

7 files changed

+21
-5
lines changed

7 files changed

+21
-5
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ server [options]
4848
Root directory of the server.
4949
Defaults to current working directory.
5050
51+
-R|--empty-root
52+
Use virtual empty directory as root directory.
53+
Useful to share alias directories only.
54+
5155
-x|--fallback-proxy <separator><url-path><separator><proxy-target> ...
5256
If local resource under <url-path> not found, reverse proxy to <proxy-target>.
5357
<proxy-target> must be the format of schema://host/path.

src/app/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ func NewApp(params []*param.Param) *App {
6565
}
6666
}
6767

68+
// init vhost
6869
listens := p.Listens
6970
if len(listens) == 0 && len(p.ListensPlain) == 0 && len(p.ListensTLS) == 0 {
7071
if cert == nil {
@@ -74,7 +75,6 @@ func NewApp(params []*param.Param) *App {
7475
}
7576
}
7677

77-
// init vhost
7878
errors = vhSvc.Add(&goVirtualHost.HostInfo{
7979
Listens: listens,
8080
ListensPlain: p.ListensPlain,

src/param/cli.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ func init() {
2121
err = options.AddFlagsValue("root", []string{"-r", "--root"}, "GHFS_ROOT", ".", "root directory of server")
2222
serverErrHandler.CheckFatal(err)
2323

24+
err = options.AddFlags("emptyroot", []string{"-R", "--empty-root"}, "GHFS_EMPTY_ROOT", "use virtual empty root directory")
25+
serverErrHandler.CheckFatal(err)
26+
2427
err = options.AddFlagsValues("fallbackproxies", []string{"-x", "--fallback-proxy"}, "", nil, "reverse proxy to target if local resource not found, <sep><url><sep><target>, e.g. :/doc:http://remote/doc")
2528
serverErrHandler.CheckFatal(err)
2629

@@ -186,6 +189,7 @@ func doParseCli() []*Param {
186189

187190
// normalize option
188191
param.Root, _ = result.GetString("root")
192+
param.EmptyRoot = result.HasKey("emptyroot")
189193
param.IgnoreProxyTargetBadCert = result.HasKey("ignorebadproxycert")
190194
param.GlobalUpload = result.HasKey("globalupload")
191195
param.GlobalArchive = result.HasKey("globalarchive")

src/param/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ type user struct {
88
}
99

1010
type Param struct {
11-
Root string
11+
Root string
12+
EmptyRoot bool
1213

1314
FallbackProxies map[string]string
1415
AlwaysProxies map[string]string

src/serverHandler/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
type handler struct {
1414
root string
15+
emptyRoot bool
1516
urlPrefix string
1617

1718
fallbackProxies proxyHandlers
@@ -131,6 +132,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
131132

132133
func NewHandler(
133134
root string,
135+
emptyRoot bool,
134136
urlPrefix string,
135137
p *param.Param,
136138
users user.Users,
@@ -157,6 +159,7 @@ func NewHandler(
157159

158160
h := &handler{
159161
root: root,
162+
emptyRoot: emptyRoot,
160163
urlPrefix: urlPrefix,
161164

162165
fallbackProxies: fallbackProxies,

src/serverHandler/responseData.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ func (h *handler) getResponseData(r *http.Request, visitFs bool) (data *response
239239
reqFsPath = path.Clean(h.root + reqPath)
240240
}
241241

242-
file, item, _statErr := stat(reqFsPath, visitFs)
242+
file, item, _statErr := stat(reqFsPath, visitFs && !h.emptyRoot)
243243
if _statErr != nil {
244244
errs = append(errs, _statErr)
245245
notFound = os.IsNotExist(_statErr)

src/vhostMux/main.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,17 @@ func NewServeMux(
5858

5959
// register handlers
6060
aliases := p.Aliases
61-
if _, hasRootAlias := aliases["/"]; !hasRootAlias {
61+
_, hasRootAlias := aliases["/"]
62+
emptyRoot := false
63+
if !hasRootAlias {
6264
aliases["/"] = p.Root
65+
emptyRoot = p.EmptyRoot
6366
}
6467

6568
handlers := map[string]http.Handler{}
6669
for urlPath, fsPath := range aliases {
67-
handlers[urlPath] = serverHandler.NewHandler(fsPath, urlPath, p, users, fallbackProxies, alwaysProxies, tplObj, logger, errorHandler)
70+
emptyHandlerRoot := emptyRoot && urlPath == "/"
71+
handlers[urlPath] = serverHandler.NewHandler(fsPath, emptyHandlerRoot, urlPath, p, users, fallbackProxies, alwaysProxies, tplObj, logger, errorHandler)
6872
}
6973

7074
// create ServeMux

0 commit comments

Comments
 (0)