| 617 | } |
| 618 | |
| 619 | func (fs serveCommandFileSystem) Open(requestName string) (http.File, error) { |
| 620 | requestName = strings.TrimPrefix(requestName, `/`) // Strip leading '/' |
| 621 | name := path.Join(fs.serveRoot, requestName) |
| 622 | log.WithField(`request`, requestName). |
| 623 | Print(`Request`) |
| 624 | |
| 625 | // Check if the file is reachable from the serve root. |
| 626 | if f, err := http.Dir(fs.serveRoot).Open(requestName); err == nil { |
| 627 | log.WithField(`request`, requestName). |
| 628 | Print(`Found in serve root`) |
| 629 | return f, nil |
| 630 | } |
| 631 | |
| 632 | if strings.HasPrefix(requestName, `.well-known/`) { |
| 633 | // Ignore ".well-known/" requests since they are probes. |
| 634 | // See https://en.wikipedia.org/wiki/Well-known_URI |
| 635 | return nil, os.ErrNotExist |
| 636 | } |
| 637 | |
| 638 | dir, file := path.Split(name) |
| 639 | dir = strings.TrimSuffix(dir, `/`) // Strip tailing '/' |
| 640 | base := path.Base(dir) // base is parent folder name, which becomes the output file name. |
| 641 | |
| 642 | isPkg := file == base+".js" |
| 643 | isMap := file == base+".js.map" |
| 644 | isIndex := file == "index.html" |
| 645 | |
| 646 | // Create a new session to pick up changes to source code on disk. |
| 647 | // TODO(dmitshur): might be possible to get a single session to detect changes to source code on disk |
| 648 | s, err := gbuild.NewSession(fs.options) |
| 649 | if err != nil { |
| 650 | log.WithField(`request`, requestName).WithError(err). |
| 651 | Error(`Failed to creates a session`) |
| 652 | return nil, err |
| 653 | } |
| 654 | |
| 655 | // Check if the file is reachable from the Go path. |
| 656 | if f, err := http.Dir(path.Join(s.XContext().Env().GOPATH, `src`)).Open(requestName); err == nil { |
| 657 | log.WithField(`request`, requestName). |
| 658 | Print(`Found in Go path`) |
| 659 | return f, nil |
| 660 | } |
| 661 | |
| 662 | // Check if the file is reachable from the Go root. |
| 663 | if f, err := http.Dir(path.Join(s.XContext().Env().GOROOT, `src`)).Open(requestName); err == nil { |
| 664 | log.WithField(`request`, requestName). |
| 665 | Print(`Found in Go root`) |
| 666 | return f, nil |
| 667 | } |
| 668 | |
| 669 | // Check if the request's dir is an import path. |
| 670 | if pkg, err := s.XContext().Import(dir, fs.serveRoot, build.FindOnly); err == nil { |
| 671 | f, err := http.Dir(pkg.Dir).Open(file) |
| 672 | if err == nil { |
| 673 | log.WithField(`request`, requestName). |
| 674 | Print(`Found in import path`) |
| 675 | return f, nil |
| 676 | } |