Skip to content

Commit

Permalink
项目结构大调整
Browse files Browse the repository at this point in the history
  • Loading branch information
zgwit committed Oct 16, 2023
1 parent d3e8c9e commit cdaa432
Show file tree
Hide file tree
Showing 106 changed files with 188 additions and 523 deletions.
84 changes: 42 additions & 42 deletions app/proxy.go
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)
}
80 changes: 40 additions & 40 deletions app/runner.go
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
}
4 changes: 2 additions & 2 deletions internal/broker/broker.go → broker/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
mochi "github.com/mochi-mqtt/server/v2"
"github.com/mochi-mqtt/server/v2/hooks/auth"
"github.com/mochi-mqtt/server/v2/listeners"
"github.com/zgwit/iot-master/v4/db"
"github.com/zgwit/iot-master/v4/log"
"github.com/zgwit/iot-master/v4/pkg/db"
"github.com/zgwit/iot-master/v4/pkg/log"
"github.com/zgwit/iot-master/v4/types"
"xorm.io/xorm"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/broker/gin_bridge.go → broker/gin_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
"github.com/zgwit/iot-master/v4/log"
"github.com/zgwit/iot-master/v4/pkg/log"
"io"
"net"
"net/http"
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion internal/broker/options.go → broker/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package broker

import (
"github.com/zgwit/iot-master/v4/config"
"github.com/zgwit/iot-master/v4/env"
"github.com/zgwit/iot-master/v4/pkg/env"
)

type Options struct {
Expand Down
File renamed without changes.
File renamed without changes.
144 changes: 0 additions & 144 deletions internal/api/aggregator.go

This file was deleted.

4 changes: 2 additions & 2 deletions internal/api/alarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package api

import (
"github.com/gin-gonic/gin"
curd "github.com/zgwit/iot-master/v4/curd"
"github.com/zgwit/iot-master/v4/db"
curd "github.com/zgwit/iot-master/v4/pkg/curd"
"github.com/zgwit/iot-master/v4/pkg/db"
"github.com/zgwit/iot-master/v4/types"
)

Expand Down
7 changes: 2 additions & 5 deletions internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"github.com/zgwit/iot-master/v4/app"
"github.com/zgwit/iot-master/v4/curd"
"github.com/zgwit/iot-master/v4/pkg/curd"
"github.com/zgwit/iot-master/v4/pkg/web"
"github.com/zgwit/iot-master/v4/types"
"github.com/zgwit/iot-master/v4/web"
"net/http"
)

Expand Down Expand Up @@ -103,12 +103,9 @@ func RegisterRoutes(router *gin.RouterGroup) {

deviceRouter(router.Group("/device"))
alarmRouter(router.Group("/alarm"))
validatorRouter(router.Group("/validator"))
subscriptionRouter(router.Group("/subscription"))
notificationRouter(router.Group("/notification"))

aggregatorRouter(router.Group("/aggregator"))

historyRouter(router.Group("/history"))
groupRouter(router.Group("/history/group"))

Expand Down
2 changes: 1 addition & 1 deletion internal/api/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package api

import (
"github.com/gin-gonic/gin"
curd "github.com/zgwit/iot-master/v4/curd"
curd "github.com/zgwit/iot-master/v4/pkg/curd"
"github.com/zgwit/iot-master/v4/types"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/api/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package api

import (
"github.com/gin-gonic/gin"
"github.com/zgwit/iot-master/v4/curd"
"github.com/zgwit/iot-master/v4/pkg/curd"
"io/ioutil"
"mime"
"os"
Expand Down
Loading

0 comments on commit cdaa432

Please sign in to comment.