Skip to content

Commit

Permalink
Merge pull request #6 from oneut/support_replaces
Browse files Browse the repository at this point in the history
Support replaces
  • Loading branch information
oneut committed Mar 25, 2018
2 parents 90f5317 f39e1bb commit f55db58
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 18 deletions.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 49,8 @@ LRPはgolang製のLive Reload Proxyです。
コマンドを管理できる
restartを管理できる
標準出力のキーワードでLive Reloadイベントの発火が可能
静的サーバとしての起動
プロキシサーバとして使用した際にはスタブ機能として動作
ポーリング機能は今のところない
ローカルで動かすことを目的としているので優先度が低い
必要そうなら実装を検討する
Expand Down Expand Up @@ -172,6 174,44 @@ source:
host: "localhost:8080"
```

## source.replaces
プロキシを経由してアクセスしているサイトのHTMLデータを条件を指定して置換します。
複数の設定が可能です。

## source.replaces.-.search
検索条件になるキーワードを指定します。

```
source:
host: "localhost:8080"
replaces:
- search: "//cdn.example.com"
replase: "//localhost:9500"
```

## source.replaces.-.replace
検索条件に対して置換されるキーワードを指定します

```
source:
host: "localhost:8080"
replaces:
- search: "//cdn.example.com"
replase: "//localhost:9500"
```

## source.replaces.-.regexp
検索条件に正規表現の使用を指定します。デフォルトは`false`です。

```
source:
host: "localhost:8080"
replaces:
- search: "abc(. )"
replase: "$1"
regexp: true
```

## tasks
Live Reloadを行うタスクを管理します。タスク名は任意で設定できます。
タスクごとに実行するコマンドとファイル監視を設定します。
Expand Down
4 changes: 2 additions & 2 deletions command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 74,9 @@ func (c *Command) Start() {
}
}()

stdouterr, _ := c.cmd.StderrPipe()
stderr, _ := c.cmd.StderrPipe()
go func() {
scanner := bufio.NewScanner(stdouterr)
scanner := bufio.NewScanner(stderr)
for scanner.Scan() {
line := scanner.Text()
logger.InfoCommandStdout(c.name, c.commandName, line)
Expand Down
19 changes: 19 additions & 0 deletions config/replace.go
Original file line number Diff line number Diff line change
@@ -0,0 1,19 @@
package config

type Replace struct {
Search string
Replace string
Regexp bool
}

func (r *Replace) IsValid() bool {
if r.Search == "" {
return false
}

if r.Replace == "" {
return false
}

return true
}
5 changes: 3 additions & 2 deletions config/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 3,9 @@ package config
var defaultSourceScheme string = "http"

type Source struct {
Scheme string
Host string
Scheme string
Host string
Replaces []Replace
}

func (s *Source) GetScheme() string {
Expand Down
47 changes: 33 additions & 14 deletions livereloadproxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 15,45 @@ import (
"github.com/skratchdot/open-golang/open"
)

func NewProxy(proxy config.Proxy, source config.Source) *Proxy {
return &Proxy{
func NewProxy(proxyConfig config.Proxy, sourceConfig config.Source) *Proxy {
proxy := &Proxy{
livereload: livereload.New("LivereloadProxy"),
proxyURL: &url.URL{
Scheme: proxy.GetScheme(),
Host: proxy.Host,
Scheme: proxyConfig.GetScheme(),
Host: proxyConfig.Host,
},
staticPath: proxy.StaticPath,
isBrowserOpen: proxy.IsBrowserOpen(),
staticPath: proxyConfig.StaticPath,
isBrowserOpen: proxyConfig.IsBrowserOpen(),
sourceURL: &url.URL{
Scheme: source.GetScheme(),
Host: source.Host,
Scheme: sourceConfig.GetScheme(),
Host: sourceConfig.Host,
},
scriptPath: "/livereload.js",
}

for _, replace := range sourceConfig.Replaces {
if !(replace.IsValid()) {
continue
}

proxy.AddSourceReplacer(replace)
}

return proxy
}

type Proxy struct {
livereload *livereload.Server
proxyURL *url.URL
scriptPath string
isBrowserOpen bool
sourceURL *url.URL
staticPath string
livereload *livereload.Server
proxyURL *url.URL
scriptPath string
isBrowserOpen bool
sourceURL *url.URL
staticPath string
sourceReplacers []SourceReplacer
}

func (p *Proxy) AddSourceReplacer(replaceConfig config.Replace) {
p.sourceReplacers = append(p.sourceReplacers, NewSourceReplacer(replaceConfig))
}

func (p *Proxy) Run() {
Expand Down Expand Up @@ -166,6 181,7 @@ func (p *Proxy) handleReverseProxy(w http.ResponseWriter, r *http.Request) {
modifier := func(res *http.Response) error {
res.Header.Del("Content-Length")
res.Header.Del("Content-Encoding")
res.Header.Del("Content-Security-Policy")
res.Header.Set("Cache-Control", "no-store")

contentType := res.Header.Get("Content-type")
Expand All @@ -187,6 203,9 @@ func (p *Proxy) handleReverseProxy(w http.ResponseWriter, r *http.Request) {

s = strings.Replace(s, sourceSchemeHost, proxySchemeHost, -1)
s = strings.Replace(s, sourceHost, proxySchemeHost, -1)
for _, sourceReplacer := range p.sourceReplacers {
s = sourceReplacer.Replace(s)
}

res.Body = ioutil.NopCloser(strings.NewReader(s))
return nil
Expand Down
21 changes: 21 additions & 0 deletions livereloadproxy/source_regexp_replacer.go
Original file line number Diff line number Diff line change
@@ -0,0 1,21 @@
package livereloadproxy

import (
"regexp"
)

func NewSourceRegexpReplacer(search string, replace string) *SourceRegexpReplacer {
return &SourceRegexpReplacer{
searchRegexp: regexp.MustCompile(search),
replace: replace,
}
}

type SourceRegexpReplacer struct {
searchRegexp *regexp.Regexp
replace string
}

func (s *SourceRegexpReplacer) Replace(value string) string {
return s.searchRegexp.ReplaceAllString(value, s.replace)
}
17 changes: 17 additions & 0 deletions livereloadproxy/source_replacer.go
Original file line number Diff line number Diff line change
@@ -0,0 1,17 @@
package livereloadproxy

import (
"github.com/oneut/lrp/config"
)

func NewSourceReplacer(replaceConfig config.Replace) SourceReplacer {
if replaceConfig.Regexp {
return NewSourceRegexpReplacer(replaceConfig.Search, replaceConfig.Replace)
}

return NewSourceStringReplacer(replaceConfig.Search, replaceConfig.Replace)
}

type SourceReplacer interface {
Replace(string) string
}
19 changes: 19 additions & 0 deletions livereloadproxy/source_string_replacer.go
Original file line number Diff line number Diff line change
@@ -0,0 1,19 @@
package livereloadproxy

import "strings"

func NewSourceStringReplacer(search string, replace string) *SourceStringReplacer {
return &SourceStringReplacer{
search: search,
replace: replace,
}
}

type SourceStringReplacer struct {
search string
replace string
}

func (s *SourceStringReplacer) Replace(value string) string {
return strings.Replace(value, s.search, s.replace, -1)
}
3 changes: 3 additions & 0 deletions lrp.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 5,9 @@ proxy:
source:
scheme: "https"
host: "localhost:8080"
replaces:
- search: "//cdn.example.com"
replase: "//localhost:9500"
tasks:
web:
aggregate_timeout: 300
Expand Down

0 comments on commit f55db58

Please sign in to comment.