-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
120 lines (94 loc) · 2.81 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"flag"
"github.com/astaxie/beego/context"
"github.com/GoAdminGroup/go-admin/modules/logger"
"net/http"
"os"
"os/signal"
_ "github.com/GoAdminGroup/go-admin/adapter/beego" // web framework adapter
_ "github.com/GoAdminGroup/go-admin/modules/db/drivers/sqlite" // sql driver
_ "github.com/GoAdminGroup/themes/adminlte" // ui theme
"github.com/GoAdminGroup/go-admin/engine"
"github.com/GoAdminGroup/go-admin/template"
"github.com/GoAdminGroup/go-admin/template/chartjs"
"github.com/astaxie/beego"
"github.com/easymesh/autoproxy-web/models"
"github.com/easymesh/autoproxy-web/pages"
)
var (
Debug bool
Help bool
TlsCert string
TlsKey string
TlsEnable bool
Port int
Config string
)
func init() {
flag.BoolVar(&Help, "help", false, "usage help")
flag.BoolVar(&Debug, "debug", false, "debug")
flag.IntVar(&Port, "port", 8000, "bind port for console")
flag.StringVar(&Config, "config", "./config.json", "console config file")
flag.BoolVar(&TlsEnable, "tls", false, "console with tls")
flag.StringVar(&TlsCert, "cert", "", "certificate file")
flag.StringVar(&TlsKey, "key", "", "private key file name")
}
func ProccessExit(eng *engine.Engine) {
eng.SqliteConnection().Close()
pages.EnginFini()
logger.Info("console shutdown")
os.Exit(-1)
}
func main() {
flag.Parse()
if Help {
flag.Usage()
return
}
app := beego.NewApp()
template.AddComp(chartjs.NewChart())
beego.SetStaticPath("/uploads", "uploads")
eng := engine.Default()
eng.AddConfigFromJSON(Config)
eng.AddGenerator("users", pages.UserTableGet)
eng.AddGenerator("domains", pages.DomainTableGet)
eng.AddGenerator("remotes", pages.RemoteTableGet)
eng.AddGenerator("proxys", pages.ProxyTableGet)
if err := eng.Use(app); err != nil {
logger.Error(err.Error())
panic(err)
}
//eng.HTML("GET", "/admin/", pages.GetDashBoard)
app.Handlers.Any("/admin/", func(ctx *context.Context) {
ctx.Redirect(http.StatusFound,"/admin/info/proxys")
})
app.Handlers.Any("/", func(ctx *context.Context) {
ctx.Redirect(http.StatusFound,"/admin")
})
models.Init(eng.SqliteConnection())
if TlsEnable {
beego.BConfig.Listen.EnableHTTP = false
beego.BConfig.Listen.EnableHTTPS = true
beego.BConfig.Listen.HTTPSAddr = "0.0.0.0"
beego.BConfig.Listen.HTTPSPort = Port
beego.BConfig.Listen.HTTPSCertFile = TlsCert
beego.BConfig.Listen.HTTPSKeyFile = TlsKey
pages.TLSCertSet(TlsCert, TlsKey)
} else {
beego.BConfig.Listen.HTTPAddr = "0.0.0.0"
beego.BConfig.Listen.HTTPPort = Port
}
go func() {
app.Run()
logger.Info("proxy service shutdown")
ProccessExit(eng)
}()
pages.DomainInit()
pages.EnginInit()
logger.Infof("proxy server %s start success", VersionGet())
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt, os.Kill)
<-quit
ProccessExit(eng)
}