Is it possible to run flutter web build using golang server? Golang has facility to serve html file and flutter web gives output as index.html and js files. if it is possible then how golang code should look like?
Flutter web with Golang Server
Asked Answered
as the friendly doc mentions it, i believe you got to build your app.
https://flutter.dev/docs/get-started/web#build
Run the following command to generate a release build:
flutter build web
This populates a build/web directory with built files, including an assets directory, which need to be served together.
how golang code should look like?
like any other regular HTTP golang server.
http.Handle("/build/web/", http.StripPrefix("/build/web/", http.FileServer(http.Dir("build/web"))))
http.ListenAndServe(":8080", nil)
package main
import (
"flag"
"log"
"net/http"
)
func main() {
port := flag.String("p", "8181", "port to serve on")
directory := flag.String("d", "web", "the directory of static file to host")
flag.Parse()
http.Handle("/", http.FileServer(http.Dir(*directory)))
log.Printf("Serving %s on HTTP port: %s\n", *directory, *port)
log.Fatal(http.ListenAndServe(":"+*port, nil))
}
Just copy web folder to your's go app.
Server will be hosted at http://localhost:8080/
and make sure you don't add slash/
in file path
package main
import (
"log"
"net/http"
)
func main() {
// Serve Flutter app files from the build/web directory
http.Handle("/", http.FileServer(http.Dir("client/build/web")))
// Start the server on port 8080
log.Fatal(http.ListenAndServe(":8080", nil))
}
© 2022 - 2024 — McMap. All rights reserved.