網站首頁 編程語言 正文
介紹
本文介紹如何通過 rk-boot 快速搭建靜態文件下載 Web 服務。
什么是 靜態文件下載 Web UI?
通過配置文件,快速搭建可下載文件的 Web 服務。
請訪問如下地址獲取完整教程:
安裝
go get github.com/rookie-ninja/rk-boot
快速開始
rk-boot 提供了一個方便的方法,讓用戶快速實現網頁【瀏覽和下載】靜態文件的功能。
目前,rk-boot 支持如下文件源。如果用戶希望支持更多的文件源,可以通過實現 http.FileSystem 接口來實現。
- 本地文件系統
- pkger
1.創建 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.創建 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.文件夾結構
. ├── boot.yaml ├── go.mod ├── go.sum └── main.go 0 directories, 4 files
4.驗證
訪問 http://localhost:8080/rk/v1/static
從 pkger 讀取文件 (嵌入式靜態文件)
pkger 是一個可以把靜態文件,嵌入到 .go 文件的工具。
這個例子中,我們把當前文件夾下的所有文件,都嵌入到 pkger.go 文件中。
這樣做的好處就是,在部署的時候,可以不用考慮復制一堆文件夾結構。
1.下載 pkger 命令行
go get github.com/markbates/pkger/cmd/pkger
2.創建 boot.yaml
pkger 會使用 module 來區分不同的 package,所以,sourcePath 里,我們添加了相應 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.創建 main.go
代碼中,有兩個地方需要注意。
pkger.Include("./")
這段代碼不做任何事情,是告訴 pkger 命令行打包哪些文件。
_ “github.com/rookie-ninja/rk-demo/internal”
一定要這么引入,因為我們會把 pkger.go 文件放到 internal/pkger.go 中,pkger.go 文件里定一個一個 variable,只有這么引入,才可以在編譯 main.go 的時候,順利引入 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.文件夾結構
. ├── boot.yaml ├── go.mod ├── go.sum ├── internal │ └── pkged.go └── main.go 1 directory, 5 files
6.驗證
訪問 http://localhost:8080/rk/v1/static
自定義文件源
我們將使用 afero package 里面的 memFs 作為例子。
如果想要從類似 AWS S3 中讀取,用戶可以實現一個屬于自己的 http.FileSystem。
rk-boot 會在后續的更新中,逐漸實現這些功能。
1.創建 boot.yaml
--- gin: - name: greeter # Required port: 8080 # Required enabled: true # Required
2.創建 main.go
我們在 memFs 中創建了一個 /folder 文件夾和 一個 /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.驗證
訪問 http://localhost:8080/rk/v1/static
原文鏈接:https://juejin.cn/post/7039594150022348837
相關推薦
- 2021-12-12 linux縮減XFS分區格式的根目錄_Linux
- 2022-04-04 webpack-loader: url-loader
- 2022-07-06 C#數據適配器DataAdapter_C#教程
- 2022-05-31 如何使用yolov5輸出檢測到的目標坐標信息_python
- 2022-08-31 SQL注入報錯注入函數圖文詳解_數據庫其它
- 2022-05-29 python3中的類繼承你真的了解嗎_python
- 2022-11-11 Android利用Canvas類繪制圖形_Android
- 2021-12-07 基于Kubernetes實現前后端應用的金絲雀發布(兩種方案)_C#教程
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支