introduce
This article introduces how to quickly build a static file download web service through rk-boot .
What is the Static File Download Web UI?
Through the configuration file, you can quickly build a Web service that can download files.
Please visit the following address for the 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 [browsing and downloading] static files on web pages.
Currently, rk-boot supports the following file sources. If users wish to support more file sources, they can do so by implementing the http.FileSystem 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. Verify
访问 http://localhost:8080/rk/v1/static
Read files from pkger (embedded static files)
pkger is a tool that can embed static files into .go files.
In this example, we embed all the files in the current folder into the pkger.go file.
The advantage of this is that when deploying, you don't have to think about copying a bunch of folder structures.
1. Download the pkger command line
go get github.com/markbates/pkger/cmd/pkger
2. Create boot.yaml
pkger will use module to distinguish different packages, so, in sourcePath, we add the prefix of the corresponding module.
---
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
In the code, there are two places to pay attention to.
- pkger.Include("./")
This code does nothing but tell the pkger command line which files to pack.
- _ “github.com/rookie-ninja/rk-demo/internal”
It must be introduced in this way, because we will put the pkger.go file into internal/pkger.go, and define a variable in the pkger.go file. Only in this way can the variable be successfully introduced when compiling 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/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. Verify
访问 http://localhost:8080/rk/v1/static
custom file source
We will use the memFs from the aero package as an example.
Users can implement their own http.FileSystem if they want to read from something like AWS S3.
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.txt file in memFs.
// 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())
}
3. Verify
访问 http://localhost:8080/rk/v1/static