Gin 框架快速創(chuàng)建靜態(tài)文件下載Web服務(wù)
介紹
本文介紹如何通過 rk-boot 快速搭建靜態(tài)文件下載 Web 服務(wù)。
什么是 靜態(tài)文件下載 Web UI?
通過配置文件,快速搭建可下載文件的 Web 服務(wù)。
請(qǐng)?jiān)L問如下地址獲取完整教程:
rkdocs.netlify.app/cn
安裝
go get github.com/rookie-ninja/rk-boot
快速開始
rk-boot 提供了一個(gè)方便的方法,讓用戶快速實(shí)現(xiàn)網(wǎng)頁【瀏覽和下載】靜態(tài)文件的功能。
目前,rk-boot 支持如下文件源。如果用戶希望支持更多的文件源,可以通過實(shí)現(xiàn) http.FileSystem 接口來實(shí)現(xiàn)。
- 本地文件系統(tǒng)
- pkger
1.創(chuàng)建 boot.yaml
--- gin: - name: greeter# Required port: 8080# Required enabled: true# Required static: enabled: true # Optional, default: false path: "/rk/v1/static" # Optional, default: /rk/v1/static sourceType: local# Required, options: pkger, local sourcePath: "." # Required, full path of source directory
2.創(chuàng)建 main.go
// Copyright (c) 2021 rookie-ninja // // Use of this source code is governed by an Apache-style // license that can be found in the LICENSE file. package main import ( "context" "github.com/rookie-ninja/rk-boot" ) // Application entrance. func main() { // Create a new boot instance. boot := rkboot.NewBoot() // Bootstrap boot.Bootstrap(context.Background()) // Wait for shutdown sig boot.WaitForShutdownSig(context.Background()) }
3.文件夾結(jié)構(gòu)
. ├── boot.yaml ├── go.mod ├── go.sum └── main.go 0 directories, 4 files
4.驗(yàn)證
訪問 http://localhost:8080/rk/v1/static
從 pkger 讀取文件 (嵌入式靜態(tài)文件)
pkger 是一個(gè)可以把靜態(tài)文件,嵌入到 .go 文件的工具。
這個(gè)例子中,我們把當(dāng)前文件夾下的所有文件,都嵌入到 pkger.go 文件中。
這樣做的好處就是,在部署的時(shí)候,可以不用考慮復(fù)制一堆文件夾結(jié)構(gòu)。
1.下載 pkger 命令行
go get github.com/markbates/pkger/cmd/pkger
2.創(chuàng)建 boot.yaml
pkger 會(huì)使用 module 來區(qū)分不同的 package,所以,sourcePath 里,我們添加了相應(yīng) module 的前綴。
--- gin: - name: greeter # Required port: 8080 # Required enabled: true # Required static: enabled: true# Optional, default: false path: "/rk/v1/static" # Optional, default: /rk/v1/static sourceType: pkger # Required, options: pkger, local sourcePath: "github.com/rookie-ninja/rk-demo:/"# Required, full path of source directory
3.創(chuàng)建 main.go
代碼中,有兩個(gè)地方需要注意。
pkger.Include("./")
這段代碼不做任何事情,是告訴 pkger 命令行打包哪些文件。
_ “github.com/rookie-ninja/rk-demo/internal”
一定要這么引入,因?yàn)槲覀儠?huì)把 pkger.go 文件放到 internal/pkger.go 中,pkger.go 文件里定一個(gè)一個(gè) variable,只有這么引入,才可以在編譯 main.go 的時(shí)候,順利引入 variable。
// Copyright (c) 2021 rookie-ninja // // Use of this source code is governed by an Apache-style // license that can be found in the LICENSE file. package main import ( "context" "github.com/markbates/pkger" "github.com/rookie-ninja/rk-boot" // Must be present in order to make pkger load embedded files into memory. _ "github.com/rookie-ninja/rk-demo/internal" ) func init() { // This is used while running pkger CLI pkger.Include("./") } // Application entrance. func main() { // Create a new boot instance. boot := rkboot.NewBoot() // Bootstrap boot.Bootstrap(context.Background()) // Wait for shutdown sig boot.WaitForShutdownSig(context.Background()) }
4.生成 pkger.go
pkger -o internal
5.文件夾結(jié)構(gòu)
. ├── boot.yaml ├── go.mod ├── go.sum ├── internal │└── pkged.go └── main.go 1 directory, 5 files
6.驗(yàn)證
訪問 http://localhost:8080/rk/v1/static
自定義文件源
我們將使用 afero package 里面的 memFs 作為例子。
如果想要從類似 AWS S3 中讀取,用戶可以實(shí)現(xiàn)一個(gè)屬于自己的 http.FileSystem。
rk-boot 會(huì)在后續(xù)的更新中,逐漸實(shí)現(xiàn)這些功能。
1.創(chuàng)建 boot.yaml
--- gin: - name: greeter# Required port: 8080# Required enabled: true# Required
2.創(chuàng)建 main.go
我們?cè)?memFs 中創(chuàng)建了一個(gè) /folder 文件夾和 一個(gè) /file.txt 文件。
// Copyright (c) 2021 rookie-ninja // // Use of this source code is governed by an Apache-style // license that can be found in the LICENSE file. package main import ( "context" "github.com/rookie-ninja/rk-boot" "github.com/rookie-ninja/rk-gin/boot" "github.com/spf13/afero" "os" ) // Application entrance. func main() { // Create a new boot instance. boot := rkboot.NewBoot() // Create a memory fs fs := afero.NewHttpFs(afero.NewMemMapFs()) // Add folder and file.txt into memory fs fs.MkdirAll("/folder", os.ModePerm) f, _ := fs.Create("/file.txt") f.Write([]byte("this is my content!")) f.Close() // Set StaticFileEntry ginEntry := boot.GetGinEntry("greeter") ginEntry.StaticFileEntry = rkgin.NewStaticFileHandlerEntry( rkgin.WithPathStatic("/rk/v1/static"), rkgin.WithFileSystemStatic(fs)) // Bootstrap boot.Bootstrap(context.Background()) // Wait for shutdown sig boot.WaitForShutdownSig(context.Background()) }
3.驗(yàn)證
訪問 http://localhost:8080/rk/v1/static
到此這篇關(guān)于Gin 框架快速創(chuàng)建靜態(tài)文件下載Web服務(wù)的文章就介紹到這了,更多相關(guān)Gin靜態(tài)文件下載內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。