Setup Golang server with Vue3 using InertiaJS
This commit is contained in:
34
server.go
Normal file
34
server.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
inertia "github.com/romsar/gonertia"
|
||||
"log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func main() {
|
||||
i, err := inertia.NewFromFile("./root.html")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.Handle("/", i.Middleware(homeHandler(i)))
|
||||
|
||||
mux.Handle("/build/", http.StripPrefix("/build/", http.FileServer(http.Dir("./serve/build"))))
|
||||
|
||||
http.ListenAndServe(":8080", mux)
|
||||
}
|
||||
|
||||
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": "From Golang",
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return http.HandlerFunc(fn)
|
||||
}
|
||||
Reference in New Issue
Block a user