Flutter web with Golang Server
Asked Answered
A

3

5

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?

Alonzo answered 14/12, 2019 at 9:52 Comment(0)
P
4

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)
Parthen answered 14/12, 2019 at 11:49 Comment(0)
G
3
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.

Geese answered 2/4, 2020 at 6:49 Comment(0)
C
0

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))
}

Cirsoid answered 9/1 at 12:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.