一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術(shù)及教程分享平臺(tái)!
分類導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Golang - Gin 框架快速創(chuàng)建靜態(tài)文件下載Web服務(wù)

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

2022-01-22 16:21尹東勛 Golang

本文主要介紹了Gin 框架快速創(chuàng)建靜態(tài)文件下載Web服務(wù),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文介紹如何通過 rk-boot 快速搭建靜態(tài)文件下載 Web 服務(wù)。什么是 靜態(tài)文件下載 Web UI?通過配置文件,快速搭建可下載文件的 Web 服務(wù)。

Gin 框架快速創(chuàng)建靜態(tài)文件下載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

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

 

從 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

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

 

自定義文件源

我們將使用 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

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

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

原文鏈接:https://juejin.cn/post/7039594150022348837

延伸 · 閱讀

精彩推薦
  • Golanggolang json.Marshal 特殊html字符被轉(zhuǎn)義的解決方法

    golang json.Marshal 特殊html字符被轉(zhuǎn)義的解決方法

    今天小編就為大家分享一篇golang json.Marshal 特殊html字符被轉(zhuǎn)義的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧 ...

    李浩的life12792020-05-27
  • GolangGolang通脈之?dāng)?shù)據(jù)類型詳情

    Golang通脈之?dāng)?shù)據(jù)類型詳情

    這篇文章主要介紹了Golang通脈之?dāng)?shù)據(jù)類型,在編程語言中標(biāo)識(shí)符就是定義的具有某種意義的詞,比如變量名、常量名、函數(shù)名等等,Go語言中標(biāo)識(shí)符允許由...

    4272021-11-24
  • Golanggolang如何使用struct的tag屬性的詳細(xì)介紹

    golang如何使用struct的tag屬性的詳細(xì)介紹

    這篇文章主要介紹了golang如何使用struct的tag屬性的詳細(xì)介紹,從例子說起,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看...

    Go語言中文網(wǎng)11352020-05-21
  • GolangGolang中Bit數(shù)組的實(shí)現(xiàn)方式

    Golang中Bit數(shù)組的實(shí)現(xiàn)方式

    這篇文章主要介紹了Golang中Bit數(shù)組的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧...

    天易獨(dú)尊11682021-06-09
  • Golanggolang的httpserver優(yōu)雅重啟方法詳解

    golang的httpserver優(yōu)雅重啟方法詳解

    這篇文章主要給大家介紹了關(guān)于golang的httpserver優(yōu)雅重啟的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,...

    helight2992020-05-14
  • Golanggolang 通過ssh代理連接mysql的操作

    golang 通過ssh代理連接mysql的操作

    這篇文章主要介紹了golang 通過ssh代理連接mysql的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧...

    a165861639710342021-03-08
  • Golanggo日志系統(tǒng)logrus顯示文件和行號(hào)的操作

    go日志系統(tǒng)logrus顯示文件和行號(hào)的操作

    這篇文章主要介紹了go日志系統(tǒng)logrus顯示文件和行號(hào)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧...

    SmallQinYan12302021-02-02
  • Golanggo語言制作端口掃描器

    go語言制作端口掃描器

    本文給大家分享的是使用go語言編寫的TCP端口掃描器,可以選擇IP范圍,掃描的端口,以及多線程,有需要的小伙伴可以參考下。 ...

    腳本之家3642020-04-25
主站蜘蛛池模板: 国产永久免费视频m3u8 | 大逼美女 | 果冻传媒在线播放1 | 大陆国产vs国产对白 | 菠萝视频污 | 99 久久99久久精品免观看 | 久久人妻无码毛片A片麻豆 久久热这里只有 精品 | 东方影库四虎 | 含羞草传媒每天免费一次破解 | 毛茸茸的大逼 | 奇米影视先锋 | 久久久久久88色偷偷 | 天堂色 | 毛片亚洲毛片亚洲毛片 | 国产精品资源在线观看网站 | 国产精品福利在线观看入口 | 香蕉精品国产高清自在自线 | 为什么丈夫插我我却喜欢被打着插 | 国产自在线观看 | 欧美日韩精彩视频 | 久久精品WWW人人爽人人 | 国产性片在线观看 | ts人妖国产一区 | 扒开女人下面 | 韩国一大片a毛片女同 | 欧美一区高清 | 9420高清完整版在线观看国语 | 日本乱子 | bl动漫在线观看 | 波多野结衣女老师 | 洗濯屋し在线观看 | 99re这里只有精品视频 | 国产亚洲精品第一综合另类 | 国产草草视频 | 国产精品视频一区二区三区 | 久久re这里精品在线视频7 | 4tube欧美高清 | 久久视频在线视频 | 秋葵丝瓜茄子草莓榴莲樱桃 | 日韩精品一二三区 | 日日操日日|