-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
135 lines (114 loc) · 3.18 KB
/
main.go
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"path"
"strings"
inertia "github.com/romsar/gonertia"
)
func main() {
i := initInertia()
mux := http.NewServeMux()
mux.Handle("/home", i.Middleware(homeHandler(i)))
mux.Handle("/build/", http.StripPrefix("/build/", http.FileServer(http.Dir("./public/build"))))
http.ListenAndServe(":8000", mux)
}
func initInertia() *inertia.Inertia {
viteHotFile := "./public/hot"
rootViewFile := "resources/views/root.html"
// check if laravel-vite-plugin is running in dev mode (it puts a "hot" file in the public folder)
_, err := os.Stat(viteHotFile)
if err == nil {
i, err := inertia.NewFromFile(
rootViewFile,
inertia.WithSSR(),
)
if err != nil {
log.Fatal(err)
}
i.ShareTemplateFunc("vite", func(entry string) (string, error) {
content, err := os.ReadFile(viteHotFile)
if err != nil {
return "", err
}
url := strings.TrimSpace(string(content))
if strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://") {
url = url[strings.Index(url, ":")+1:]
} else {
url = "//localhost:8080"
}
if entry != "" && !strings.HasPrefix(entry, "/") {
entry = "/" + entry
}
return url + entry, nil
})
i.ShareTemplateData("hmr", true)
return i
}
// laravel-vite-plugin not running in dev mode, use build manifest file
manifestPath := "./public/build/manifest.json"
// check if the manifest file exists, if not, rename it
if _, err := os.Stat(manifestPath); os.IsNotExist(err) {
// move the manifest from ./public/build/.vite/manifest.json to ./public/build/manifest.json
// so that the vite function can find it
err := os.Rename("./public/build/.vite/manifest.json", "./public/build/manifest.json")
if err != nil {
return nil
}
}
i, err := inertia.NewFromFile(
rootViewFile,
inertia.WithVersionFromFile(manifestPath),
inertia.WithSSR(),
)
if err != nil {
log.Fatal(err)
}
i.ShareTemplateFunc("vite", vite(manifestPath, "/build/"))
return i
}
func vite(manifestPath, buildDir string) func(path string) (string, error) {
f, err := os.Open(manifestPath)
if err != nil {
log.Fatalf("cannot open provided vite manifest file: %s", err)
}
defer f.Close()
viteAssets := make(map[string]*struct {
File string `json:"file"`
Source string `json:"src"`
})
err = json.NewDecoder(f).Decode(&viteAssets)
// print content of viteAssets
for k, v := range viteAssets {
log.Printf("%s: %s\n", k, v.File)
}
if err != nil {
log.Fatalf("cannot unmarshal vite manifest file to json: %s", err)
}
return func(p string) (string, error) {
if val, ok := viteAssets[p]; ok {
return path.Join("/", buildDir, val.File), nil
}
return "", fmt.Errorf("asset %q not found", p)
}
}
func homeHandler(i *inertia.Inertia) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
err := i.Render(w, r, "Home/Index", inertia.Props{
"text": "Inertia.js with React and Go! 💚",
})
if err != nil {
handleServerErr(w, err)
return
}
}
return http.HandlerFunc(fn)
}
func handleServerErr(w http.ResponseWriter, err error) {
log.Printf("http error: %s\n", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("server error"))
}