Skip to content

Commit

Permalink
Merge pull request #240 from askuy/feature/maxprocs
Browse files Browse the repository at this point in the history
embed fs
  • Loading branch information
askuy committed Jan 7, 2022
2 parents 8a92ab4 + fa25f22 commit 1ab0292
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 9 deletions.
22 changes: 13 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ EGO是一个集成里各种工程实践的框架。通过组件化的设计模

* 更多组件请查看:[https://github.com/gotomicro/ego-component](https://github.com/gotomicro/ego-component)

## 5 下载工具
## 5 版本要求
* v0.8.2以下 需要 go大于go1.13
* v0.8.3后 需要 go大于go1.16

## 6 下载工具
```bash
bash <(curl -L https://raw.githubusercontent.com/gotomicro/egoctl/main/getlatest.sh)
```
Expand All @@ -111,7 +115,7 @@ bash <(curl -L https://raw.githubusercontent.com/gotomicro/egoctl/main/getlatest
* /usr/local/bin/protoc-gen-go-http 生成HTTP工具


## 6 特性介绍
## 7 特性介绍
* 配置驱动
所有组件启动方式为`组件名称.Load("配置名称").Build()`,可以创建一个组件实例。如以下`http server``egin`是组件名称,`server.http`是配置名称
```go
Expand Down Expand Up @@ -149,9 +153,9 @@ egin.Load("server.http").Build()

![](docs/images/metric.png)

## 7 Quick Start
## 8 Quick Start

### 7.1 HelloWorld
### 8.1 HelloWorld
配置

```toml
Expand Down Expand Up @@ -185,13 +189,13 @@ func main() {
}
```

### 7.2 使用命令行运行
### 8.2 使用命令行运行
```
export EGO_DEBUG=true # 默认日志输出到logs目录,开启dev后日志输出到终端
go run main.go --config=config.toml
```

### 7.3 如下所示
### 8.3 如下所示
![图片](./docs/images/startup.png)


Expand All @@ -201,16 +205,16 @@ go run main.go --config=config.toml
"Hello Ego"%
```

### 7.4 更加友好的包编译
### 8.4 更加友好的包编译

使用scripts文件夹里的[包编译](examples/build),可以看到优雅的version提示

![图片](./docs/images/version.png)

## 8 更新日志
## 9 更新日志
[Releases](https://github.com/gotomicro/ego/releases)

## 9 加入我们
## 10 加入我们
加入我们,请在验证信息里添加``ego``关键字

![image](./docs/images/join.jpeg)
Expand Down
35 changes: 35 additions & 0 deletions server/egin/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ import (
"context"
"crypto/tls"
"crypto/x509"
"embed"
"errors"
"fmt"
"io/fs"
"net"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"sync"

Expand All @@ -30,6 +35,7 @@ type Component struct {
Server *http.Server
listener net.Listener
routerCommentMap map[string]string // router的中文注释,非并发安全
embedWrapper *embedWrapper
}

func newComponent(name string, config *Config, logger *elog.Component) *Component {
Expand All @@ -42,6 +48,14 @@ func newComponent(name string, config *Config, logger *elog.Component) *Componen
listener: nil,
routerCommentMap: make(map[string]string),
}

if config.EmbedPath != "" {
comp.embedWrapper = &embedWrapper{
embedFs: config.embedFs,
path: config.EmbedPath,
}
}

// 设置信任的header头
comp.Engine.TrustedPlatform = config.TrustedPlatform
return comp
Expand Down Expand Up @@ -163,3 +177,24 @@ func (c *Component) buildTLSConfig() (*tls.Config, error) {
}
return tlsConfig, nil
}

// HTTPEmbedFs http的文件系统
func (c *Component) HTTPEmbedFs() http.FileSystem {
return http.FS(c.embedWrapper)
}

// embedWrapper 嵌入普通的静态资源的wrapper
type embedWrapper struct {
embedFs embed.FS // 静态资源
path string // 设置embed文件到静态资源的相对路径,也就是embed注释里的路径
}

// Open 静态资源被访问的核心逻辑
func (e *embedWrapper) Open(name string) (fs.File, error) {
if filepath.Separator != '/' && strings.ContainsRune(name, filepath.Separator) {
return nil, errors.New("http: invalid character in file path")
}
fullName := filepath.Join(e.path, filepath.FromSlash(path.Clean("/"+name)))
file, err := e.embedFs.Open(fullName)
return file, err
}
3 changes: 3 additions & 0 deletions server/egin/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package egin

import (
"crypto/tls"
"embed"
"fmt"
"time"

Expand Down Expand Up @@ -36,6 +37,8 @@ type Config struct {
TLSClientAuth string // https 客户端认证方式默认为 NoClientCert(NoClientCert,RequestClientCert,RequireAnyClientCert,VerifyClientCertIfGiven,RequireAndVerifyClientCert)
TLSClientCAs []string // https client的ca,当需要双向认证的时候指定可以倒入自签证书
TrustedPlatform string // 需要用户换成自己的CDN名字,获取客户端IP地址
EmbedPath string // 嵌入embed path数据
embedFs embed.FS // 需要在build时候注入embed.Fs
TLSSessionCache tls.ClientSessionCache
blockFallback func(*gin.Context)
resourceExtract func(*gin.Context) string
Expand Down
8 changes: 8 additions & 0 deletions server/egin/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package egin

import (
"crypto/tls"
"embed"

"github.com/gin-gonic/gin"
"github.com/gotomicro/ego/core/elog"
Expand Down Expand Up @@ -47,3 +48,10 @@ func WithLogger(logger *elog.Component) Option {
c.logger = logger
}
}

// WithEmbedFs 设置embed fs
func WithEmbedFs(fs embed.FS) Option {
return func(c *Container) {
c.config.embedFs = fs
}
}

0 comments on commit 1ab0292

Please sign in to comment.