首頁 > 後端開發 > Golang > 主體

使用自訂路由器使用 wails 資產處理程序時出現問題

WBOY
發布: 2024-02-12 16:30:07
轉載
933 人瀏覽過

使用自定义路由器使用 wails 资产处理程序时出现问题

問題內容

我正在嘗試將自訂多工器與wails 資產處理程序一起使用,但是當嘗試獲取任何內容時,我不斷獲取index.html 頁面。我在多工器的 servehttp 函數頂部添加了一條 print 語句,但這僅在程式開始時獲取 favicon.ico 時被調用一次。

我有以下主檔案:

package main

import (
    "embed"
    "fmt"

    "github.com/nigel2392/router/v3"
    "github.com/nigel2392/router/v3/request"
    "github.com/wailsapp/wails/v2"
    "github.com/wailsapp/wails/v2/pkg/options"
    "github.com/wailsapp/wails/v2/pkg/options/assetserver"
)

var router *router.router = router.newrouter(true)

func init() {
    router.get("/about", func(r *request.request) {
        fmt.println("about")
        r.writestring("about")
    })
}

//go:embed all:frontend/dist
var assets embed.fs

func main() {
    // create an instance of the app structure
    app := newapp()

    // create application with options
    err := wails.run(&options.app{
        title:  "new",
        width:  1024,
        height: 768,
        assetserver: &assetserver.options{
            assets:  assets,
            handler: router,
        },
        backgroundcolour: &options.rgba{r: 27, g: 38, b: 54, a: 1},
        onstartup:        app.startup,
        bind: []interface{}{
            app,
        },
    })

    if err != nil {
        println("error:", err.error())
    }
}
登入後複製

我可以在終端機中看到以下輸出:

deb | [externalassethandler] loading 'http://localhost:3000/favicon.ico'
deb | [externalassethandler] loading 'http://localhost:3000/favicon.ico' failed, using assethandler

# print statement at the top of the servehttp function, 
# prints the requested path, and the available router paths.
path:  /favicon.ico
get /about ->

deb | [externalassethandler] loading 'http://localhost:3000/about'


to develop in the browser and call your bound go methods from javascript, navigate to: http://localhost:34115
deb | [externalassethandler] loading 'http://localhost:3000/@vite/client'
deb | [externalassethandler] loading 'http://localhost:3000/node_modules/vite/dist/client/env.mjs'
deb | [externalassethandler] loading 'http://localhost:3000/about'
deb | [externalassethandler] loading 'http://localhost:3000/@vite/client'
deb | [externalassethandler] loading 'http://localhost:3000/node_modules/vite/dist/client/env.mjs'
deb | [externalassethandler] loading 'http://localhost:3000/about'
登入後複製

當嘗試取得有關頁面進行測試時,如 wails assetserver 文件中所述,我會擷取索引頁面:

let resp = await fetch("/about")
undefined
await resp.text()
'<!DOCTYPE html>\n<html lang="en">\n<head>\n  \x3Cscript type="module" src="/@vite/client">\x3C/script>\n\n    <meta charset="UTF-8"/>\n    <meta content="width=device-width, initial-scale=1.0" name="viewport"/>\n    <meta name="wails-options" content="noautoinject" />\n    \x3Cscript src="/wails/ipc.js">\x3C/script>\n    \x3Cscript src="/wails/runtime.js">\x3C/script>\n    <title>new</title>\n</head>\n<body>\n    <h1>Index!</h1>\n    <a href="/about">Go To About!</a>\n</body>\n</html>\n'
登入後複製

為什麼沒有呼叫servehttp函數?

解決方法

日誌顯示使用了 externalassethandler。這意味著使用外部前端開發伺服器。所有資源請求都會先轉送到外部前端開發伺服器。只有當外部前端開發伺服器回應 404405 狀態代碼時,才會使用 assetserver.options 中指定的處理程序。如今,大多數 spa 前端開發伺服器都為非資產請求提供 index.html 服務。這就是您看到這種行為的原因。

解決方法是設定外部前端開發伺服器以繞過該請求。對於vite,修改設定檔新增以下內容:

export default defineConfig({
  server: {
    proxy: {
      '/about': {
        bypass: function () {
          // Return false to produce a 404 error for the request.
          return false;
        },
      },
      // or for all the requests that start with "/api/"
      '/api/': {
        bypass: function () {
          return false;
        },
      },
    },
  },
});
登入後複製

但我建議不要這樣做。因為資產處理程序,顧名思義,是為動態資產提供服務的。 wails 有自己的呼叫綁定 go 方法的方式。你應該嘗試一下。

參考文獻:

#

以上是使用自訂路由器使用 wails 資產處理程序時出現問題的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:stackoverflow.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!