遇到自定义路由器与 wails 资产处理程序的集成问题
来源:stackoverflow
时间:2024-03-17 22:51:31 133浏览 收藏
在 Wails 应用程序中集成自定义路由器和 Wails 资产处理程序时,可能会遇到问题。默认情况下,Wails 使用外部前端开发服务器来处理资源请求,而资产处理程序仅在外部服务器返回 404 或 405 状态代码时才会被调用。这会导致在尝试获取自定义路由时总是返回 index.html 页面。
我正在尝试将自定义多路复用器与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() '\n\n\n \x3Cscript type="module" src="/@vite/client">\x3C/script>\n\n \n \n \n \x3Cscript src="/wails/ipc.js">\x3C/script>\n \x3Cscript src="/wails/runtime.js">\x3C/script>\nnew \n\n\nIndex!
\n Go To About!\n\n\n'
为什么没有调用servehttp函数?
正确答案
日志显示使用了 externalassethandler
。这意味着使用外部前端开发服务器。所有资源请求都会首先转发到外部前端开发服务器。仅当外部前端开发服务器响应 404
或 405
状态代码时,才会使用 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 资产处理程序的集成问题》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习