-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathmain.go
186 lines (161 loc) · 5.31 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
_ "github.com/mikezss/skl-go/routers"
_ "os"
"github.com/astaxie/beego"
"github.com/astaxie/beego/config"
_ "github.com/astaxie/beego/context"
"github.com/astaxie/beego/orm"
"github.com/astaxie/beego/plugins/cors"
"github.com/beego/i18n"
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
"fmt"
"log"
"github.com/mikezss/skl-go/controllers"
"github.com/mikezss/skl-go/models"
"strings"
)
func main() {
if beego.BConfig.RunMode == "dev" {
beego.BConfig.WebConfig.DirectoryIndex = true
beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
}
beego.AddFuncMap("Calculate", models.Calculate)
beego.AddFuncMap("Unescaped", models.Unescaped)
beego.AddFuncMap("Unescapedjs", models.Unescapedjs)
beego.AddFuncMap("UnescapedJSStr", models.UnescapedJSStr)
beego.AddFuncMap("Tofirstupper", models.Tofirstupper)
beego.AddFuncMap("Tolower", models.Tolower)
beego.AddFuncMap("Replace", models.Replace)
beego.AddFuncMap("Toupper", models.Toupper)
beego.AddFuncMap("Mod", models.Mod)
beego.AddFuncMap("OutputFN", models.OutputFN)
beego.Run()
}
func init() {
dbtype := registerDB()
createTable(dbtype)
orm.Debug = true
//静态文件处理
beego.SetStaticPath("/static", "static")
beego.SetStaticPath("/file", "file")
//beego.SetViewsPath("/views")
/*CORP,增加一个跨域的header:*/
//context.NewOutput().Header("Access-Control-Allow-Origin", "*")
// beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
// AllowOrigins: []string{"http://*"},
// AllowMethods: []string{"PUT", "PATCH"},
// AllowHeaders: []string{"Origin"},
// ExposeHeaders: []string{"Content-Length"},
// AllowCredentials: true,
// }))
beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
AllowAllOrigins: true,
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowHeaders: []string{"Origin", "Authorization", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
ExposeHeaders: []string{"Content-Length", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
AllowCredentials: true,
}))
//settingLocales()
beego.AddFuncMap("i18n", i18n.Tr)
beego.BConfig.WebConfig.Session.SessionOn = true
beego.BConfig.WebConfig.Session.SessionGCMaxLifetime = 3600
}
func registerDB() string {
iniconf, err := config.NewConfig("ini", "conf/myconf.ini")
if err != nil {
log.Fatal(err)
}
dbtype := iniconf.String("dbtype")
fmt.Println("dbtype:-->" + dbtype)
datasourcename := ""
ds := make([]string, 0)
switch dbtype {
case "sqlite3":
//oa.db
datasourcename = iniconf.String(dbtype + "::dbname")
orm.RegisterDriver(dbtype, orm.DRSqlite)
case "mysql":
ds = append(ds, iniconf.String(dbtype+"::username")+":")
ds = append(ds, iniconf.String(dbtype+"::password")+"@tcp(")
ds = append(ds, iniconf.String(dbtype+"::hostname")+":")
ds = append(ds, iniconf.String(dbtype+"::port")+")/")
ds = append(ds, iniconf.String(dbtype+"::dbname")+"?charset=utf8&loc=Asia%2FShanghai&parseTime=true")
datasourcename = strings.Join(ds, "")
fmt.Println(datasourcename)
//datasourcename:-->root:root@tcp(localhost:3306)/skl-ticket?charset=utf8
//tcp:localhost:3306*mydb/root/rootroot
orm.RegisterDriver("mysql", orm.DRMySQL)
case "postgres":
//tcp:localhost:5432*mydb/postgres/postgres
ds = append(ds, "user=")
ds = append(ds, iniconf.String(dbtype+"::username")+" ")
ds = append(ds, "password=")
ds = append(ds, iniconf.String(dbtype+"::password")+" ")
ds = append(ds, "dbname=")
ds = append(ds, iniconf.String(dbtype+"::dbname")+" ")
ds = append(ds, "host=")
ds = append(ds, iniconf.String(dbtype+"::hostname")+" ")
ds = append(ds, "port=")
ds = append(ds, iniconf.String(dbtype+"::port")+" ")
ds = append(ds, "sslmode=disable")
datasourcename = strings.Join(ds, "")
fmt.Println(datasourcename)
orm.RegisterDriver(dbtype, orm.DRPostgres)
}
fmt.Println("datasourcename:-->" + datasourcename)
orm.RegisterDataBase("default", dbtype, datasourcename)
//orm.RegisterDataBase("default", driverName, dataSource, maxIdle, maxOpen)
return "default"
}
//自动建表
func createTable(dbtype string) {
name := dbtype //数据库别名
force := false //不强制建数据库
verbose := true //打印建表过程
err := orm.RunSyncdb(name, force, verbose) //建表
if err != nil {
beego.Error(err)
}
iniconf, err := config.NewConfig("ini", "conf/myconf.ini")
if err != nil {
log.Fatal(err)
}
dbtype2 := iniconf.String("dbtype")
fmt.Println("dbtype:-->" + dbtype2)
inittable := iniconf.String("inittable")
fmt.Println("inittable:-->" + inittable)
if inittable == "true" {
initTable(dbtype2)
}
}
//
func initTable(dbtype string) {
cctl := &controllers.COMMONController{}
filepath := cctl.GetCurrentDirectory() + "/" + dbtype + ".sql"
s, err := cctl.Readfile2string(filepath, "GBK")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(s)
sqlarr := strings.Split(s, ";")
o := orm.NewOrm()
//err = o.Begin()
for _, sql := range sqlarr {
fmt.Println(sql)
_, err = o.Raw(sql).Exec()
if err != nil {
fmt.Println(err)
//o.Rollback()
continue
}
}
//o.Commit()
}
func startWebsocketServer() {
ws := models.NewWebsocketServer()
go ws.Run()
}