Moge-Row commited on
Commit
c1788dc
·
1 Parent(s): 7a15fa1

fix index path multiple fallbacks

Browse files
Files changed (1) hide show
  1. internal/handler/index.go +12 -2
internal/handler/index.go CHANGED
@@ -3,6 +3,8 @@ package handler
3
  import (
4
  "net/http"
5
  "os"
 
 
6
  )
7
 
8
  func HandleIndex(w http.ResponseWriter, r *http.Request) {
@@ -10,11 +12,19 @@ if r.URL.Path != "/" {
10
  http.NotFound(w, r)
11
  return
12
  }
13
- content, err := os.ReadFile("/app/static/index.html")
 
 
14
  if err != nil {
15
- http.Error(w, "Page not found", 404)
 
 
 
 
16
  return
17
  }
 
 
18
  w.Header().Set("Content-Type", "text/html; charset=utf-8")
19
  w.Write(content)
20
  }
 
3
  import (
4
  "net/http"
5
  "os"
6
+ "path/filepath"
7
+ "runtime"
8
  )
9
 
10
  func HandleIndex(w http.ResponseWriter, r *http.Request) {
 
12
  http.NotFound(w, r)
13
  return
14
  }
15
+ _, filename, _, _ := runtime.Caller(0)
16
+ dir := filepath.Join(filepath.Dir(filename), "..", "..", "static", "index.html")
17
+ content, err := os.ReadFile(dir)
18
  if err != nil {
19
+ content, err = os.ReadFile("static/index.html")
20
+ if err != nil {
21
+ content, err = os.ReadFile("/app/static/index.html")
22
+ if err != nil {
23
+ http.Error(w, "Page not found: "+err.Error(), 404)
24
  return
25
  }
26
+ }
27
+ }
28
  w.Header().Set("Content-Type", "text/html; charset=utf-8")
29
  w.Write(content)
30
  }