Gin 框架快速创建静态文件下载Web服务

 更新时间:2021年12月10日 10:17:11   作者:尹东勋  
本文主要介绍了Gin 框架快速创建静态文件下载Web服务,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

介绍

本文介绍如何通过 rk-boot 快速搭建静态文件下载 Web 服务。

什么是 静态文件下载 Web UI?

通过配置文件,快速搭建可下载文件的 Web 服务。

请访问如下地址获取完整教程:

rkdocs.netlify.app/cn

安装

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

到此这篇关于Gin 框架快速创建静态文件下载Web服务的文章就介绍到这了,更多相关Gin静态文件下载内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • golang实现微信支付v3版本的方法

    golang实现微信支付v3版本的方法

    这篇文章主要介绍了golang实现微信支付v3版本的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-03-03
  • Golang 实现超大文件读取的两种方法

    Golang 实现超大文件读取的两种方法

    这篇文章主要介绍了Golang 实现超大文件读取的两种方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-04-04
  • golang中http请求的context传递到异步任务的坑及解决

    golang中http请求的context传递到异步任务的坑及解决

    这篇文章主要介绍了golang中http请求的context传递到异步任务的坑及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-03-03
  • 使用Go语言进行安卓开发的详细教程

    使用Go语言进行安卓开发的详细教程

    本文将介绍如何使用Go语言进行安卓开发,我们将探讨使用Go语言进行安卓开发的优点、准备工作、基本概念和示例代码,通过本文的学习,你将了解如何使用Go语言构建高效的安卓应用程序,需要的朋友可以参考下
    2023-11-11
  • Goland 的安装及激活教程(window、linux下安装)

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

    这篇文章主要介绍了Golang Goland 的安装及激活详细教程,包括window下安装goland和linux下安装goland,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-10-10
  • golang1.21新特性全面讲解

    golang1.21新特性全面讲解

    经过了半年左右的开发,golang 1.21 最近正式发布了,这个版本中有不少重要的新特性和变更,尤其是在泛型相关的代码上,下面小编就来和大家好好唠唠吧
    2023-08-08
  • Go单例模式与Once源码实现

    Go单例模式与Once源码实现

    这篇文章主要介绍了Go单例模式与Once源码实现,本文结合示例代码给大家讲解的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-12-12
  • Golang中的闭包(Closures)详解

    Golang中的闭包(Closures)详解

    在 Golang 中,闭包是一个引用了作用域之外的变量的函数,Golang 中的匿名函数也被称为闭包,闭包可以被认为是一种特殊类型的匿名函数,所以本文就给大家详细的介绍一下Golang的闭包到底是什么,感兴趣的小伙伴跟着小编一起来看看吧
    2023-07-07
  • GScript 编写标准库示例详解

    GScript 编写标准库示例详解

    这篇文章主要为大家介绍了GScript 编写标准库示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • Go语言常用的打log方式详解

    Go语言常用的打log方式详解

    Golang的log包短小精悍,可以非常轻松的实现日志打印转存功能,下面这篇文章主要给大家介绍了关于Go语言常用的打log方式的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-10-10

最新评论