-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
106 changed files
with
188 additions
and
523 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,42 @@ | ||
package app | ||
|
||
import ( | ||
"errors" | ||
"github.com/gin-gonic/gin" | ||
"github.com/zgwit/iot-master/v4/lib" | ||
"github.com/zgwit/iot-master/v4/types" | ||
"github.com/zgwit/iot-master/v4/web" | ||
"net/http/httputil" | ||
) | ||
|
||
var proxies lib.Map[httputil.ReverseProxy] | ||
|
||
func ProxyApp(ctx *gin.Context) { | ||
p := proxies.Load(ctx.Param("app")) | ||
|
||
if p == nil { | ||
app := Applications.Load(ctx.Param("app")) | ||
if app == nil { | ||
_ = ctx.Error(errors.New("应用未注册")) | ||
return | ||
} | ||
|
||
rp, err := web.CreateReverseProxy(app.Type, app.Address) | ||
if err != nil { | ||
_ = ctx.Error(err) | ||
return | ||
} | ||
p = rp | ||
proxies.Store(app.Id, rp) | ||
} | ||
|
||
p.ServeHTTP(ctx.Writer, ctx.Request) | ||
|
||
ctx.Abort() | ||
} | ||
|
||
var Applications lib.Map[types.App] | ||
|
||
func Register(app *types.App) { | ||
Applications.Store(app.Id, app) | ||
} | ||
package app | ||
|
||
import ( | ||
"errors" | ||
"github.com/gin-gonic/gin" | ||
"github.com/zgwit/iot-master/v4/lib" | ||
"github.com/zgwit/iot-master/v4/pkg/web" | ||
"github.com/zgwit/iot-master/v4/types" | ||
"net/http/httputil" | ||
) | ||
|
||
var proxies lib.Map[httputil.ReverseProxy] | ||
|
||
func ProxyApp(ctx *gin.Context) { | ||
p := proxies.Load(ctx.Param("app")) | ||
|
||
if p == nil { | ||
app := Applications.Load(ctx.Param("app")) | ||
if app == nil { | ||
_ = ctx.Error(errors.New("应用未注册")) | ||
return | ||
} | ||
|
||
rp, err := web.CreateReverseProxy(app.Type, app.Address) | ||
if err != nil { | ||
_ = ctx.Error(err) | ||
return | ||
} | ||
p = rp | ||
proxies.Store(app.Id, rp) | ||
} | ||
|
||
p.ServeHTTP(ctx.Writer, ctx.Request) | ||
|
||
ctx.Abort() | ||
} | ||
|
||
var Applications lib.Map[types.App] | ||
|
||
func Register(app *types.App) { | ||
Applications.Store(app.Id, app) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,40 @@ | ||
package app | ||
|
||
import ( | ||
"github.com/zgwit/iot-master/v4/log" | ||
"os/exec" | ||
"time" | ||
) | ||
|
||
type Runner struct { | ||
path string | ||
cmd *exec.Cmd | ||
} | ||
|
||
func (r *Runner) wait() { | ||
err := r.cmd.Wait() | ||
if err != nil { | ||
log.Error(err) | ||
} | ||
|
||
//重启 TODO 间隔可配置 | ||
time.AfterFunc(time.Minute, func() { | ||
err := r.Start() | ||
if err != nil { | ||
log.Error(err) | ||
} | ||
}) | ||
} | ||
|
||
func (r *Runner) Start() error { | ||
r.cmd = exec.Command(r.path) | ||
err := r.cmd.Start() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
//协程里等待结果 | ||
go r.wait() | ||
|
||
return nil | ||
} | ||
package app | ||
|
||
import ( | ||
"github.com/zgwit/iot-master/v4/pkg/log" | ||
"os/exec" | ||
"time" | ||
) | ||
|
||
type Runner struct { | ||
path string | ||
cmd *exec.Cmd | ||
} | ||
|
||
func (r *Runner) wait() { | ||
err := r.cmd.Wait() | ||
if err != nil { | ||
log.Error(err) | ||
} | ||
|
||
//重启 TODO 间隔可配置 | ||
time.AfterFunc(time.Minute, func() { | ||
err := r.Start() | ||
if err != nil { | ||
log.Error(err) | ||
} | ||
}) | ||
} | ||
|
||
func (r *Runner) Start() error { | ||
r.cmd = exec.Command(r.path) | ||
err := r.cmd.Start() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
//协程里等待结果 | ||
go r.wait() | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.