In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly analyzes the relevant knowledge points of how to understand the go-zero file service in the Golang micro-service framework, the content is detailed and easy to understand, the operation details are reasonable, and has a certain reference value. If you are interested, you might as well follow the editor to take a look, and follow the editor to learn more about "how to understand go-zero file services in the Golang micro-service framework".
Golang micro-service framework go-zero file service
Go-zero itself supports file services, but we need to write related handler files. The purpose of this article is to
Do not write any handler related to the file
If you have a new file, just template the file to a specific directory and don't touch any go code
The need is here. Let's do it.
Before you start the code, you may need to read
Golang microservice framework go-zero series-1: use XormV2 golang microservice framework in go-zero go-zero series-2: use jwt-token authentication in go-zero practice golang microservice framework go-zero series-3: extend go-zero to support automation of html template parsing
Be careful
Micro services pay attention to the separation of resources, and try to use professional file servers or third-party storage platforms such as OSS in the actual production process.
The thought of file Service implementation
There is a special static file service encapsulation in gin, which is not currently provided by go-zero. At present, go-zero provides very strict path matching. For example, access / asset/l1.jpg will be mapped to / asset/:1 handlerlv1 / asset/l1/l2.jpg will be mapped to / asset/:1/:2 corresponding handlerlv2. There are two cases
Map the specified path to a single file
For example, we need to access favourite.ico, and the system points to the. / www/favourite.ico file. The code is as follows
/ / processing function, pass in the file address func filehandler (filepath string) http.HandlerFunc {return func (w http.ResponseWriter, req * http.Request) {http.ServeFile (w, req, filepath)}}
Directly call the AddRoute method in router to add a single route
Func RegisterHandlers (engine * rest.Server, serverCtx * svc.ServiceContext) {/ / add a single engine.AddRoute directly here (rest.Route {Method: http.MethodGet, Path: "/ favourite.ico" Handler: filehandler (". / www/favourite.ico"),})} maps the specified directory and provides services
In the actual process, we need to expose a certain directory, such as / assets/ directory, in which some resource files such as css,js,img are stored.
Tree / f+---assets | +-css | +-fonts | +-images | +-js | |\-plugins | +-- font-awesome | | +-css | |\-fonts | +-fontawesome | | +-| -css | |\-fonts | +-ionicons | | +-css | |\-fonts | +-jquery.contextmenu | |\-images | +-jquery.pin | | +-css | |\-images | +-jqueryui-1.12.1 | | | +-external |\-jquery | |\-images |\-swiper-4.5.3 | +-css |\-js |
If you use a single file to implement, it must not be reasonable, because the router will be very large, how to solve this problem? We can use the following methods to implement the folder service
/ func dirhandler (patern, filedir string) http.HandlerFunc {return func (w http.ResponseWriter, req * http.Request) {handler: = http.StripPrefix (patern, http.FileServer (http.Dir (filedir) handler.ServeHTTP (w, req)}}
The core of the above function is the http.StripPrefix (patern, http.FileServer (http.Dir (filedir) function. The core function of this function is to map the mapping patern format to a directory filedir.
Patern: request path format / assets/:1,/assets/:1/:2
Filedir: map the corresponding folder. / assets/
Then we only need to build a mapping between the multi-level file access format and dirhandler.
Func RegisterHandlers (engine * rest.Server, serverCtx * svc.ServiceContext) {/ / register here dirlevel: = [] string {": 1", ": 2", ": 3", ": 4", ": 5", ": 6", ": 7" ": 8"} patern: = "/ asset/" dirpath: = ". / assets/" for I: = 1 I
< len(dirlevel); i++ { path := prefix + strings.Join(dirlevel[:i], "/") //最后生成 /asset engine.AddRoute( rest.Route{ Method: http.MethodGet, Path: path, Handler: dirhandler(patern,dirpath), }) logx.Infof("register dir %s %s", path,dirpath) }}404 404可以在main函数中配置 rt := router.NewPatRouter() rt.SetNotFoundHandler(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { //这里内容可以定制 w.Write([]byte("服务器开小差了,这里可定制")) })) server := rest.MustNewServer(c.RestConf, rest.WithRouter(rt)) 此时请求http://127.0.0.1:8888/hello,系统响应 服务器开小差了,这里可定制 测试 启动系统后运行 E:\workspace@go\gozero\file>Go run file.go2020/09/05 20:18:24 {"@ timestamp": "2020-09-05T20:18:24.682+08", "level": "info", "content": "{{file-api {console logs info false 0 100} pro {0}} 0.0.0.0 8081 false 10000 {false 0s []}} [/ asset/=./assets]}"} {"@ timestamp": "2020-09-05T20:18:24.682+08", "level": "info" "content": "register dir / asset/:1. / assets"} {"@ timestamp": "2020-09-05T20:18:24.683+08", "level": "info", "content": "register dir / asset/:1/:2. / assets"} {"@ timestamp": "2020-09-05T20:18:24.683+08", "level": "info" "content": "register dir / asset/:1/:2/:3. / assets"} {"@ timestamp": "2020-09-05T20:18:24.683+08", "level": "info", "content": "register dir / asset/:1/:2/:3/:4. / assets"} {"@ timestamp": "2020-09-05T20:18:24.697+08", "level": "info" "content": "register dir / asset/:1/:2/:3/:4/:5. / assets"} {"@ timestamp": "2020-09-05T20:18:24.697+08", "level": "info", "content": "register dir / asset/:1/:2/:3/:4/:5/:6. / assets"} {"@ timestamp": "2020-09-05T20:18:24.698+08", "level": "info" "content": "register dir / asset/:1/:2/:3/:4/:5/:6/:7. / assets"}
Access to the system can respond normally.
Http://127.0.0.1:8888/asset/images/avatar.jpg http://127.0.0.1:8888/asset/js/test.js http://127.0.0.1:8888/asset/js/lv2/test.js
Note that the request is / asset/**, not / assets/**
Think about it.
We can implement file service according to req.URL.path in NotFoundHandler, how to implement it?
This is the end of the introduction on "how to understand the go-zero file service in the Golang micro-service framework". More related content can be searched for previous articles, hoping to help you answer questions and questions, please support the website!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.