-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.go
More file actions
59 lines (49 loc) · 1.19 KB
/
server.go
File metadata and controls
59 lines (49 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"github.com/gorilla/mux"
)
type spaHandler struct {
staticPath string
indexPath string
}
func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.URL.Path)
if (r.URL.Path == "/VirtualDOM/VirtualDOM") {
fmt.Println("!!!!!!!!!!!!");
r.URL.Path = "/VirtualDOM/VirtualDOM.js"
}
path, err := filepath.Abs(r.URL.Path)
fmt.Println(path ,err)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
path = filepath.Join(h.staticPath, path)
_, err = os.Stat(path)
if os.IsNotExist(err) {
http.ServeFile(w, r, filepath.Join(h.staticPath, h.indexPath))
return
} else if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.FileServer(http.Dir(h.staticPath)).ServeHTTP(w, r)
}
func main() {
mux := mux.NewRouter()
spa := spaHandler{staticPath: "public", indexPath: "index.html"}
mux.PathPrefix("/").Handler(spa)
srv := &http.Server{
Handler: mux,
Addr: ":80",
WriteTimeout: http.DefaultClient.Timeout,
ReadTimeout: http.DefaultClient.Timeout,
}
// log.Fatal(srv.ListenAndServe())
log.Fatal(srv.ListenAndServe())
}