| 44 | } |
| 45 | |
| 46 | func newLocalStaticFS(dir string) (staticSourceFS, error) { |
| 47 | absDir, err := filepath.Abs(dir) |
| 48 | if err != nil { |
| 49 | return staticSourceFS{}, fmt.Errorf("%s resolve %q: %w", webDistDirEnvVar, dir, err) |
| 50 | } |
| 51 | info, err := os.Stat(absDir) |
| 52 | if err != nil { |
| 53 | return staticSourceFS{}, fmt.Errorf("%s stat %q: %w", webDistDirEnvVar, absDir, err) |
| 54 | } |
| 55 | if !info.IsDir() { |
| 56 | return staticSourceFS{}, fmt.Errorf("%s %q is not a directory", webDistDirEnvVar, absDir) |
| 57 | } |
| 58 | indexPath := filepath.Join(absDir, "index.html") |
| 59 | indexInfo, err := os.Stat(indexPath) |
| 60 | if err != nil { |
| 61 | return staticSourceFS{}, fmt.Errorf( |
| 62 | "%s missing readable index.html at %q: %w", |
| 63 | webDistDirEnvVar, |
| 64 | indexPath, |
| 65 | err, |
| 66 | ) |
| 67 | } |
| 68 | if indexInfo.IsDir() { |
| 69 | return staticSourceFS{}, fmt.Errorf("%s index.html at %q is a directory", webDistDirEnvVar, indexPath) |
| 70 | } |
| 71 | file, err := os.Open(indexPath) |
| 72 | if err != nil { |
| 73 | return staticSourceFS{}, fmt.Errorf("%s open index.html at %q: %w", webDistDirEnvVar, indexPath, err) |
| 74 | } |
| 75 | if err := file.Close(); err != nil { |
| 76 | return staticSourceFS{}, fmt.Errorf("%s close index.html at %q: %w", webDistDirEnvVar, indexPath, err) |
| 77 | } |
| 78 | return staticSourceFS{ |
| 79 | fs: os.DirFS(absDir), |
| 80 | source: webDistDirEnvVar + "=" + absDir, |
| 81 | }, nil |
| 82 | } |
| 83 | |
| 84 | func (h *Handlers) serveStaticRoute(c *gin.Context) { |
| 85 | if c == nil { |