-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
82 lines (68 loc) · 1.88 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
package main
import (
"fmt"
"net/http"
"os"
"./route"
"./model"
"./util"
_ "github.com/go-sql-driver/mysql"
"github.com/go-xorm/xorm"
)
var engine *xorm.Engine
var BIND_ADDR = "localhost:9090"
func initxorm() {
var err error
engine, err = xorm.NewEngine("mysql", "root:r00t@/www?charset=utf8")
if err != nil {
fmt.Println("NewEngine failed", err)
os.Exit(1)
}
f, err1 := os.Create("sql.log")
if err1 != nil {
fmt.Println(err1.Error())
}
engine.ShowSQL(true)
engine.SetLogger(xorm.NewSimpleLogger(f))
// 同步结构体与数据表
if err2 := engine.Sync2(new(model.TbLicenseModule), new(model.TbFunctionModule)); err2 != nil {
fmt.Printf("Fail to sync database: %v\n", err2)
}
fmt.Println("init OK")
}
func Home(res http.ResponseWriter, req *http.Request) {
res.Write([]byte("Home Page"))
util.GetLoggerInstance().Println("Home")
fmt.Println("Home fmt")
}
func Doc(res http.ResponseWriter, req *http.Request) {
res.Write([]byte("Doc Page"))
util.GetLoggerInstance().Println("Doc")
fmt.Println("Doc fmt")
}
func HandleFunction(res http.ResponseWriter, req *http.Request) {
route.Function(res, req, engine)
}
func HandleLicense(res http.ResponseWriter, req *http.Request) {
route.License(res, req, engine)
}
func HandleConfig(res http.ResponseWriter, req *http.Request) {
route.Config(res, req, engine)
}
func HandleVerify(res http.ResponseWriter, req *http.Request) {
route.Verify(res, req, engine)
}
func main() {
initxorm()
http.HandleFunc("/", Home)
http.HandleFunc("/doc", Doc)
http.HandleFunc("/api/function", HandleFunction)
http.HandleFunc("/api/license", HandleLicense)
http.HandleFunc("/api/config", HandleConfig)
http.HandleFunc("/api/verify", HandleVerify)
util.GetLoggerInstance().Println("Server Start At", BIND_ADDR)
err := http.ListenAndServe(BIND_ADDR, nil)
if err != nil {
util.GetLoggerInstance().Println("Start Server Failed:", err)
}
}