最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Gin 框架快速創(chuàng)建靜態(tài)文件下載Web服務(wù)

 更新時間:2021年12月10日 10:17:11   作者:尹東勛  
本文主要介紹了Gin 框架快速創(chuàng)建靜態(tài)文件下載Web服務(wù),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

介紹

本文介紹如何通過 rk-boot 快速搭建靜態(tài)文件下載 Web 服務(wù)。

什么是 靜態(tài)文件下載 Web UI?

通過配置文件,快速搭建可下載文件的 Web 服務(wù)。

請訪問如下地址獲取完整教程:

rkdocs.netlify.app/cn

安裝

go get github.com/rookie-ninja/rk-boot

快速開始

rk-boot 提供了一個方便的方法,讓用戶快速實現(xiàn)網(wǎng)頁【瀏覽和下載】靜態(tài)文件的功能。

目前,rk-boot 支持如下文件源。如果用戶希望支持更多的文件源,可以通過實現(xiàn) http.FileSystem 接口來實現(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.驗證

訪問 http://localhost:8080/rk/v1/static

從 pkger 讀取文件 (嵌入式靜態(tài)文件)

pkger 是一個可以把靜態(tài)文件,嵌入到 .go 文件的工具。

這個例子中,我們把當(dāng)前文件夾下的所有文件,都嵌入到 pkger.go 文件中。

這樣做的好處就是,在部署的時候,可以不用考慮復(fù)制一堆文件夾結(jié)構(gòu)。

1.下載 pkger 命令行

go get github.com/markbates/pkger/cmd/pkger

2.創(chuàng)建 boot.yaml

pkger 會使用 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

代碼中,有兩個地方需要注意。

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.文件夾結(jié)構(gòu)

.
├── 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 中讀取,用戶可以實現(xiàn)一個屬于自己的 http.FileSystem。

rk-boot 會在后續(xù)的更新中,逐漸實現(xiàn)這些功能。

1.創(chuàng)建 boot.yaml

---
gin:
  - name: greeter                     # Required
    port: 8080                        # Required
    enabled: true                     # Required

2.創(chuàng)建 main.go

我們在 memFs 中創(chuàng)建了一個 /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

到此這篇關(guān)于Gin 框架快速創(chuàng)建靜態(tài)文件下載Web服務(wù)的文章就介紹到這了,更多相關(guān)Gin靜態(tài)文件下載內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • golang實現(xiàn)微信支付v3版本的方法

    golang實現(xiàn)微信支付v3版本的方法

    這篇文章主要介紹了golang實現(xiàn)微信支付v3版本的方法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03
  • Golang 實現(xiàn)超大文件讀取的兩種方法

    Golang 實現(xiàn)超大文件讀取的兩種方法

    這篇文章主要介紹了Golang 實現(xiàn)超大文件讀取的兩種方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • golang中http請求的context傳遞到異步任務(wù)的坑及解決

    golang中http請求的context傳遞到異步任務(wù)的坑及解決

    這篇文章主要介紹了golang中http請求的context傳遞到異步任務(wù)的坑及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • 使用Go語言進行安卓開發(fā)的詳細教程

    使用Go語言進行安卓開發(fā)的詳細教程

    本文將介紹如何使用Go語言進行安卓開發(fā),我們將探討使用Go語言進行安卓開發(fā)的優(yōu)點、準(zhǔn)備工作、基本概念和示例代碼,通過本文的學(xué)習(xí),你將了解如何使用Go語言構(gòu)建高效的安卓應(yīng)用程序,需要的朋友可以參考下
    2023-11-11
  • Goland 的安裝及激活教程(window、linux下安裝)

    Goland 的安裝及激活教程(window、linux下安裝)

    這篇文章主要介紹了Golang Goland 的安裝及激活詳細教程,包括window下安裝goland和linux下安裝goland,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-10-10
  • golang1.21新特性全面講解

    golang1.21新特性全面講解

    經(jīng)過了半年左右的開發(fā),golang?1.21?最近正式發(fā)布了,這個版本中有不少重要的新特性和變更,尤其是在泛型相關(guān)的代碼上,下面小編就來和大家好好嘮嘮吧
    2023-08-08
  • Go單例模式與Once源碼實現(xiàn)

    Go單例模式與Once源碼實現(xiàn)

    這篇文章主要介紹了Go單例模式與Once源碼實現(xiàn),本文結(jié)合示例代碼給大家講解的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-12-12
  • Golang中的閉包(Closures)詳解

    Golang中的閉包(Closures)詳解

    在?Golang?中,閉包是一個引用了作用域之外的變量的函數(shù),Golang?中的匿名函數(shù)也被稱為閉包,閉包可以被認為是一種特殊類型的匿名函數(shù),所以本文就給大家詳細的介紹一下Golang的閉包到底是什么,感興趣的小伙伴跟著小編一起來看看吧
    2023-07-07
  • GScript?編寫標(biāo)準(zhǔn)庫示例詳解

    GScript?編寫標(biāo)準(zhǔn)庫示例詳解

    這篇文章主要為大家介紹了GScript?編寫標(biāo)準(zhǔn)庫示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-10-10
  • Go語言常用的打log方式詳解

    Go語言常用的打log方式詳解

    Golang的log包短小精悍,可以非常輕松的實現(xiàn)日志打印轉(zhuǎn)存功能,下面這篇文章主要給大家介紹了關(guān)于Go語言常用的打log方式的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-10-10

最新評論

丹江口市| 志丹县| 贵南县| 永定县| 扶风县| 白朗县| 安平县| 瑞昌市| 洛阳市| 景东| 达日县| 鲁甸县| 恩施市| 沁源县| 会东县| 滕州市| 广平县| 团风县| 靖宇县| 淳化县| 望奎县| 桂平市| 德昌县| 绩溪县| 景东| 仪征市| 手游| 江源县| 甘肃省| 珠海市| 察隅县| 晋州市| 瓮安县| 宝丰县| 太谷县| 宣城市| 抚顺市| 怀来县| 兴城市| 松阳县| 东兰县|