搜尋
首頁後端開發Golang使用自訂路由器使用 wails 資產處理程序時出現問題

使用自定义路由器使用 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 id="Index">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。如有侵權,請聯絡admin@php.cn刪除
Golang vs.C:代碼示例和績效分析Golang vs.C:代碼示例和績效分析Apr 15, 2025 am 12:03 AM

Golang適合快速開發和並發編程,而C 更適合需要極致性能和底層控制的項目。 1)Golang的並發模型通過goroutine和channel簡化並發編程。 2)C 的模板編程提供泛型代碼和性能優化。 3)Golang的垃圾回收方便但可能影響性能,C 的內存管理複雜但控制精細。

Golang的影響:速度,效率和簡單性Golang的影響:速度,效率和簡單性Apr 14, 2025 am 12:11 AM

goimpactsdevelopmentpositationality throughspeed,效率和模擬性。 1)速度:gocompilesquicklyandrunseff,IdealforlargeProjects.2)效率:效率:ITScomprehenSevestAndardArdardArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdArdEcceSteral Depentencies,增強的Depleflovelmentimency.3)簡單性。

C和Golang:表演至關重要時C和Golang:表演至關重要時Apr 13, 2025 am 12:11 AM

C 更適合需要直接控制硬件資源和高性能優化的場景,而Golang更適合需要快速開發和高並發處理的場景。 1.C 的優勢在於其接近硬件的特性和高度的優化能力,適合遊戲開發等高性能需求。 2.Golang的優勢在於其簡潔的語法和天然的並發支持,適合高並發服務開發。

Golang行動:現實世界中的示例和應用程序Golang行動:現實世界中的示例和應用程序Apr 12, 2025 am 12:11 AM

Golang在实际应用中表现出色,以简洁、高效和并发性著称。1)通过Goroutines和Channels实现并发编程,2)利用接口和多态编写灵活代码,3)使用net/http包简化网络编程,4)构建高效并发爬虫,5)通过工具和最佳实践进行调试和优化。

Golang:Go編程語言解釋了Golang:Go編程語言解釋了Apr 10, 2025 am 11:18 AM

Go語言的核心特性包括垃圾回收、靜態鏈接和並發支持。 1.Go語言的並發模型通過goroutine和channel實現高效並發編程。 2.接口和多態性通過實現接口方法,使得不同類型可以統一處理。 3.基本用法展示了函數定義和調用的高效性。 4.高級用法中,切片提供了動態調整大小的強大功能。 5.常見錯誤如競態條件可以通過gotest-race檢測並解決。 6.性能優化通過sync.Pool重用對象,減少垃圾回收壓力。

Golang的目的:建立高效且可擴展的系統Golang的目的:建立高效且可擴展的系統Apr 09, 2025 pm 05:17 PM

Go語言在構建高效且可擴展的系統中表現出色,其優勢包括:1.高性能:編譯成機器碼,運行速度快;2.並發編程:通過goroutines和channels簡化多任務處理;3.簡潔性:語法簡潔,降低學習和維護成本;4.跨平台:支持跨平台編譯,方便部署。

SQL排序中ORDER BY語句結果為何有時看似隨機?SQL排序中ORDER BY語句結果為何有時看似隨機?Apr 02, 2025 pm 05:24 PM

關於SQL查詢結果排序的疑惑學習SQL的過程中,常常會遇到一些令人困惑的問題。最近,筆者在閱讀《MICK-SQL基礎�...

技術棧收斂是否僅僅是技術棧選型的過程?技術棧收斂是否僅僅是技術棧選型的過程?Apr 02, 2025 pm 05:21 PM

技術棧收斂與技術選型的關係在軟件開發中,技術棧的選擇和管理是一個非常關鍵的問題。最近,有讀者提出了...

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
4 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
4 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
1 個月前By尊渡假赌尊渡假赌尊渡假赌

熱工具

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具