introduce
This article describes how to rk-boot Quickly build a static file download Web service.
What is the static file download Web UI?
Quickly build Web services that can download files through configuration files.
Please visit the following address for a complete tutorial:
install
go get github.com/rookie-ninja/rk-boot/gf
Quick start
Rk boot provides a convenient method for users to quickly realize the function of web page [browsing and downloading] static files.
Currently, rk boot supports the following file sources. If users want to support more file sources, they can implement http File system interface.
- Local file system
- pkger
1. Create boot yaml
--- gf: - 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. Create 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" _ "github.com/rookie-ninja/rk-boot/gf" ) // 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. Folder structure
. ├── boot.yaml ├── go.mod ├── go.sum └── main.go 0 directories, 4 files
4. Verification
visit http://localhost:8080/rk/v1/static
Read file from pkger (embedded static file)
pkger Is a static file that can be embedded into go file tool.
In this example, we embed all files in the current folder into pkger Go file.
The advantage of this is that you don't have to consider copying a pile of folder structures during deployment.
1. Download pkger command line
go get github.com/markbates/pkger/cmd/pkger
2. Create boot yaml
pkger will use modules to distinguish different package s. Therefore, in sourcePath, we added the prefix of corresponding modules.
--- gf: - 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. Create main go
There are two things to note in the code.
- pkger.Include("./")
This code does nothing but tell the pkger command line which files to package.
- _ "github.com/rookie-ninja/rk-demo/internal"
It must be introduced in this way, because we will put pkger Put the go file in internal / pkger Go, pkger Set variables in the go file. Only by introducing them in this way can you compile main Go and smoothly introduce 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" _ "github.com/rookie-ninja/rk-boot/gf" // 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. Generate pkger go
pkger -o internal
5. Folder structure
. ├── boot.yaml ├── go.mod ├── go.sum ├── internal │ └── pkged.go └── main.go 1 directory, 5 files
6. Verification
visit http://localhost:8080/rk/v1/static
Custom file source
We will use memFs in afero package as an example.
If you want to read from AWS S3, you can implement your own http FileSystem.
Rk boot will gradually implement these functions in subsequent updates.
1. Create boot yaml
--- gf: - name: greeter # Required port: 8080 # Required enabled: true # Required
2. Create main go
We created a / folder folder and a / file in memFs Txt file.
// 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-boot/gf" "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 gfEntry := rkbootgf.GetGfEntry("greeter") gfEntry.StaticFileEntry = rkentry.RegisterStaticFileHandlerEntry( rkentry.WithPathStatic("/rk/v1/static"), rkentry.WithFileSystemStatic(fs)) // Bootstrap boot.Bootstrap(context.Background()) // Wait for shutdown sig boot.WaitForShutdownSig(context.Background()) }