introduce
This article introduces how to quickly set up static file download Web service through RK-boot.
What is static file download Web UI?
This section describes how to quickly build Web services that can be downloaded through configuration files.
Please visit the following address for the full tutorial:
- rkdocs.netlify.app/cn
The installation
go get github.com/rookie-ninja/rk-boot
Copy the code
Quick start
Rk-boot provides a convenient way for users to quickly implement static file browsing and downloading functions on the web.
Currently, RK-boot supports the following file sources. If users want to support more file sources, they can do so by implementing the HTTP.filesystem interface.
- Local file system
- pkger
1. Create the 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
Copy the code
2. Create a 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(a) {
// Create a new boot instance.
boot := rkboot.NewBoot()
// Bootstrap
boot.Bootstrap(context.Background())
// Wait for shutdown sig
boot.WaitForShutdownSig(context.Background())
}
Copy the code
3. Folder structure
.├ ── Go.tempo ├── go.tempo ├─ main. Go 0 directories, 4 filesCopy the code
4. Verify
Go to 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 you don’t have to worry about copying a bunch of folder structures at deployment time.
1. Download the pkger command line
go get github.com/markbates/pkger/cmd/pkger
Copy the code
2. Create the boot. Yaml
Pkger uses modules to distinguish different packages, so in sourcePath we add the corresponding module prefix.
---
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
Copy the code
3. Create a 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”
Go file. Pkger. go file will have one variable in it, so that when we compile main.go, we can 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"
// Must be present in order to make pkger load embedded files into memory.
_ "github.com/rookie-ninja/rk-demo/internal"
)
func init(a) {
// This is used while running pkger CLI
pkger.Include(". /")}// Application entrance.
func main(a) {
// Create a new boot instance.
boot := rkboot.NewBoot()
// Bootstrap
boot.Bootstrap(context.Background())
// Wait for shutdown sig
boot.WaitForShutdownSig(context.Background())
}
Copy the code
4. Generate pkger. Go
pkger -o internal
Copy the code
5. Folder structure
.├ ── ├─ ├─ ├─ ├─ ├─ ├─ ├─ ├─ ├─ ├.go, 2 filesCopy the code
6. Verify
Go to http://localhost:8080/rk/v1/static
Customize file sources
We will use memFs in afero Package as an example.
If you want to read from something like AWS S3, you can implement your own HTTP.filesystem.
Rk-boot will gradually implement these functions in future updates.
1. Create the boot. Yaml
---
gin:
- name: greeter # Required
port: 8080 # Required
enabled: true # Required
Copy the code
2. Create a 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-gin/boot"
"github.com/spf13/afero"
"os"
)
// Application entrance.
func main(a) {
// 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())
}
Copy the code
3. Verify
Go to http://localhost:8080/rk/v1/static