Skip to content

Commit

Permalink
Merge pull request #40 from dongxuny/master
Browse files Browse the repository at this point in the history
Implementation of static file handler
  • Loading branch information
dongxuny authored Dec 5, 2021
2 parents a2d3f2f + e198768 commit 6ddac68
Show file tree
Hide file tree
Showing 16 changed files with 651 additions and 8 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Interceptor & bootstrapper designed for grpc. Currently, supports bellow functio
| Swagger Service | Swagger UI with same port as gRPC Gateway. |
| Common Service | List of common API available on gRPC, gRPC Gateway and swagger. |
| TV Service | A Web UI shows application and environment information. |
| Static file handler | A Web UI shows files could be downloaded from server, currently support source of local and pkger. |
| Metrics interceptor | Collect RPC metrics and export as prometheus client with same port of gRPC gateway. |
| Log interceptor | Log every RPC requests as event with rk-query. |
| Trace interceptor | Collect RPC trace and export it to stdout, file or jaeger. |
Expand Down Expand Up @@ -51,6 +52,7 @@ Interceptor & bootstrapper designed for grpc. Currently, supports bellow functio
- [Prom Client](#prom-client)
- [TV Service](#tv-service-1)
- [Swagger Service](#swagger-service-1)
- [Static file handler Service](#static-file-handler-service)
- [Interceptors](#interceptors)
- [Log](#log)
- [Metrics](#metrics-1)
Expand Down Expand Up @@ -374,6 +376,20 @@ http:
| grpc.sw.jsonPath | Where the swagger.json files are stored locally | string | "" |
| grpc.sw.headers | Headers would be sent to caller as scheme of [key:value] | []string | [] |
### Static file handler Service
| name | description | type | default value |
| ------ | ------ | ------ | ------ |
| grpc.static.enabled | Optional, Enable static file handler | boolean | false |
| grpc.static.path | Optional, path of static file handler | string | /rk/v1/static |
| grpc.static.sourceType | Required, local and pkger supported | string | "" |
| grpc.static.sourcePath | Required, full path of source directory | string | "" |
- About [pkger](https://github.com/markbates/pkger)
User can use pkger command line tool to embed static files into .go files.
Please use sourcePath like: github.com/rookie-ninja/rk-grpc:/boot/assets
### Interceptors
#### Log
| name | description | type | default value |
Expand Down
Binary file added boot/assets/static/icons/audio.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added boot/assets/static/icons/back.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added boot/assets/static/icons/doc.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added boot/assets/static/icons/file.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added boot/assets/static/icons/folder.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added boot/assets/static/icons/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added boot/assets/static/icons/pdf.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added boot/assets/static/icons/pkg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added boot/assets/static/icons/sec.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions boot/assets/static/index.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{{define "index"}}
<html>
<head>
<title>Index of {{ .Path }}</title>
<style>
* {
font-family: sans-serif;
}
</style>
</head>
<body>
<h1>Index of {{ .Path }}</h1>
<hr>
<table width="100%" border="0" cellspacing="10">
<thead>
<tr>
<th align="left">FileName</th>
<th align="left">Last modified</th>
<th align="left">Size</th>
</tr>
<thead>
<tbody>
<tr>
<td><img style="margin-right: 5px; max-width: 20px" src="data:image/png;base64,{{ .PrevIcon }}"/><a href="{{ .PrevPath }}">..</a></td>
<td></td>
<td></td>
</tr>
{{ range .Files }}
<tr>
<td><img style="margin-right: 5px; max-width: 20px" src="data:image/png;base64,{{ .Icon }}"/><a href="{{ .FileUrl }}">{{ .FileName }}</a></td>
<td>{{ .ModTime }}</td>
<td>{{ .Size }} bytes</td>
</tr>
{{ end }}
</tbody>
</table>
</body>
</html>
{{end}}
63 changes: 56 additions & 7 deletions boot/grpc_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"fmt"
"github.com/ghodss/yaml"
gwruntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/markbates/pkger"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rookie-ninja/rk-common/common"
Expand Down Expand Up @@ -48,6 +49,7 @@ import (
"net/http"
"os"
"path"
"path/filepath"
"reflect"
"runtime"
"strconv"
Expand Down Expand Up @@ -154,6 +156,7 @@ type BootConfigGrpc struct {
Sw BootConfigSw `yaml:"sw" json:"sw"`
Tv BootConfigTv `yaml:"tv" json:"tv"`
Prom BootConfigProm `yaml:"prom" json:"prom"`
Static BootConfigStaticHandler `yaml:"static" json:"static"`
Proxy BootConfigProxy `yaml:"proxy" json:"proxy"`
EnableRkGwOption bool `yaml:"enableRkGwOption" json:"enableRkGwOption"`
GwOption *gwOption `yaml:"gwOption" json:"gwOption"`
Expand Down Expand Up @@ -329,12 +332,13 @@ type GrpcEntry struct {
gwSecureOptions []rkgrpcsec.Option `json:"-" yaml:"-"`
gwCsrfOptions []rkgrpccsrf.Option `json:"-" yaml:"-"`
// Utility related
SwEntry *SwEntry `json:"swEntry" yaml:"swEntry"`
TvEntry *TvEntry `json:"tvEntry" yaml:"tvEntry"`
ProxyEntry *ProxyEntry `json:"proxyEntry" yaml:"proxyEntry"`
PromEntry *PromEntry `json:"promEntry" yaml:"promEntry"`
CommonServiceEntry *CommonServiceEntry `json:"commonServiceEntry" yaml:"commonServiceEntry"`
CertEntry *rkentry.CertEntry `json:"certEntry" yaml:"certEntry"`
SwEntry *SwEntry `json:"swEntry" yaml:"swEntry"`
TvEntry *TvEntry `json:"tvEntry" yaml:"tvEntry"`
ProxyEntry *ProxyEntry `json:"proxyEntry" yaml:"proxyEntry"`
PromEntry *PromEntry `json:"promEntry" yaml:"promEntry"`
StaticFileEntry *StaticFileHandlerEntry `json:"staticFileHandlerEntry" yaml:"staticFileHandlerEntry"`
CommonServiceEntry *CommonServiceEntry `json:"commonServiceEntry" yaml:"commonServiceEntry"`
CertEntry *rkentry.CertEntry `json:"certEntry" yaml:"certEntry"`
}

// GrpcRegFunc Grpc registration func.
Expand Down Expand Up @@ -426,6 +430,29 @@ func RegisterGrpcEntriesWithConfig(configFilePath string) map[string]rkentry.Ent
WithZapLoggerEntryTv(zapLoggerEntry))
}

// Did we enabled static file handler?
var staticEntry *StaticFileHandlerEntry
if element.Static.Enabled {
var fs http.FileSystem
switch element.Static.SourceType {
case "pkger":
fs = pkger.Dir(element.Static.SourcePath)
case "local":
if !filepath.IsAbs(element.Static.SourcePath) {
wd, _ := os.Getwd()
element.Static.SourcePath = path.Join(wd, element.Static.SourcePath)
}
fs = http.Dir(element.Static.SourcePath)
}

staticEntry = NewStaticFileHandlerEntry(
WithZapLoggerEntryStatic(zapLoggerEntry),
WithEventLoggerEntryStatic(eventLoggerEntry),
WithNameStatic(fmt.Sprintf("%s-static", element.Name)),
WithPathStatic(element.Static.Path),
WithFileSystemStatic(fs))
}

// Did we enabled proxy?
var proxy *ProxyEntry
if element.Proxy.Enabled {
Expand Down Expand Up @@ -530,6 +557,7 @@ func RegisterGrpcEntriesWithConfig(configFilePath string) map[string]rkentry.Ent
WithProxyEntryGrpc(proxy),
WithGwMuxOptionsGrpc(gwMuxOpts...),
WithCommonServiceEntryGrpc(commonService),
WithStaticFileHandlerEntryGrpc(staticEntry),
WithEnableReflectionGrpc(element.EnableReflection),
WithGwMappingFilePathsGrpc(element.GwMappingFilePaths...),
WithCertEntryGrpc(rkentry.GlobalAppCtx.GetCertEntry(element.Cert.Ref)))
Expand Down Expand Up @@ -888,6 +916,13 @@ func WithPromEntryGrpc(prom *PromEntry) GrpcEntryOption {
}
}

// WithStaticFileHandlerEntryGrpc provide StaticFileHandlerEntry.
func WithStaticFileHandlerEntryGrpc(staticEntry *StaticFileHandlerEntry) GrpcEntryOption {
return func(entry *GrpcEntry) {
entry.StaticFileEntry = staticEntry
}
}

// WithGwRegFGrpc Provide registration function.
func WithGwRegFGrpc(f ...GwRegFunc) GrpcEntryOption {
return func(entry *GrpcEntry) {
Expand Down Expand Up @@ -1162,6 +1197,9 @@ func (entry *GrpcEntry) Bootstrap(ctx context.Context) {
entry.HttpMux.HandleFunc(entry.SwEntry.Path, entry.SwEntry.ConfigFileHandler)
entry.HttpMux.HandleFunc("/rk/v1/assets/sw/", entry.SwEntry.AssetsFileHandler)
}
if entry.IsStaticFileHandlerEnabled() {
entry.HttpMux.HandleFunc(entry.StaticFileEntry.Path, entry.StaticFileEntry.GetFileHandler)
}
if entry.IsPromEnabled() {
// Register prom path into Router.
entry.HttpMux.Handle(entry.PromEntry.Path, promhttp.HandlerFor(entry.PromEntry.Gatherer, promhttp.HandlerOpts{}))
Expand Down Expand Up @@ -1200,6 +1238,9 @@ func (entry *GrpcEntry) Bootstrap(ctx context.Context) {
if entry.IsPromEnabled() {
entry.PromEntry.Bootstrap(ctx)
}
if entry.IsStaticFileHandlerEnabled() {
entry.StaticFileEntry.Bootstrap(ctx)
}
if entry.IsTvEnabled() {
entry.TvEntry.Bootstrap(ctx)
}
Expand Down Expand Up @@ -1302,6 +1343,9 @@ func (entry *GrpcEntry) Interrupt(ctx context.Context) {
if entry.IsSwEnabled() {
entry.SwEntry.Interrupt(ctx)
}
if entry.IsStaticFileHandlerEnabled() {
entry.StaticFileEntry.Interrupt(ctx)
}
if entry.IsTvEnabled() {
entry.TvEntry.Interrupt(ctx)
}
Expand Down Expand Up @@ -1414,6 +1458,11 @@ func (entry *GrpcEntry) IsSwEnabled() bool {
return entry.SwEntry != nil
}

// IsStaticFileHandlerEnabled Is static file handler entry enabled?
func (entry *GrpcEntry) IsStaticFileHandlerEnabled() bool {
return entry.StaticFileEntry != nil
}

// IsTvEnabled Is tv enabled?
func (entry *GrpcEntry) IsTvEnabled() bool {
return entry.TvEntry != nil
Expand Down Expand Up @@ -1533,7 +1582,7 @@ func (entry *GrpcEntry) parseGwMappingHelper(bytes []byte) {
}
}

// GetGrpcEntry Get GinEntry from rkentry.GlobalAppCtx.
// GetGrpcEntry Get GrpcEntry from rkentry.GlobalAppCtx.
func GetGrpcEntry(name string) *GrpcEntry {
entryRaw := rkentry.GlobalAppCtx.GetEntry(name)
if entryRaw == nil {
Expand Down
2 changes: 1 addition & 1 deletion boot/pkged.go

Large diffs are not rendered by default.

Loading

0 comments on commit 6ddac68

Please sign in to comment.