/
type.go
102 lines (85 loc) · 2.27 KB
/
type.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
package server
import (
"database/sql"
"fmt"
"net/http"
"github.com/YWJSonic/ServerUtility/dbservice"
"github.com/YWJSonic/ServerUtility/foundation"
"github.com/YWJSonic/ServerUtility/igame"
"github.com/YWJSonic/ServerUtility/messagehandle"
"github.com/YWJSonic/ServerUtility/myhttp"
"github.com/YWJSonic/ServerUtility/restfult"
"github.com/YWJSonic/ServerUtility/socket"
"github.com/YWJSonic/ServerUtility/thirdparty/transaction"
)
// IServer ...
type IServer interface {
Launch(Setting)
LaunchRestfult([]restfult.Setting)
LaunchSocket([]socket.Setting)
LaunchDB()
Log(string)
ErrorLog(string)
}
// Service IServr
type Service struct {
ShotDown chan bool
Setting Setting
Restfult *restfult.Service
Socket *socket.Service
HTTPConn *myhttp.Service
Transfer *transaction.Service
IGame igame.IGame
DBs map[string]*dbservice.DB
}
// Launch server start
func (s *Service) Launch(setting Setting) {
s.Setting = setting
s.HTTPConn = &myhttp.Service{}
s.Transfer = transaction.NewTransaction(setting.TransferURL, s.HTTPConn)
}
// LaunchRestfult service start
func (s *Service) LaunchRestfult(setting []restfult.Setting) {
s.Restfult.HTTPLisentRun(s.Setting.RestfultAdderss(), setting)
}
// LaunchSocket service start
func (s *Service) LaunchSocket(setting []socket.Setting) {
s.Socket.HTTPLisentRun(s.Setting.SocketAdderss(), setting)
}
// LaunchDB ...
func (s *Service) LaunchDB(nameDB string, setting dbservice.ConnSetting) error {
if _, ok := s.DBs[nameDB]; ok {
return nil
}
db := dbservice.DB{}
db.SetSetting(setting)
err := db.ConnectDB(nameDB, setting)
if err != nil {
return err
}
s.DBs[nameDB] = &db
return nil
}
// DBConn ...
func (s *Service) DBConn(nameDB string) *sql.DB {
if conn, ok := s.DBs[nameDB]; ok {
return conn.GetDB()
}
return nil
}
// Log ...
func (s *Service) Log(log string) {
fmt.Println(log)
}
// ErrorLog ...
func (s *Service) ErrorLog(log string) {
fmt.Println(log)
}
// HTTPResponse Respond to cliente
func (s *Service) HTTPResponse(httpconn http.ResponseWriter, data interface{}, err messagehandle.ErrorMsg) {
result := make(map[string]interface{})
result["data"] = data
result["error"] = err
fmt.Fprint(httpconn, foundation.JSONToString(result))
messagehandle.LogPrintln("HTTPResponse:", foundation.JSONToString(result))
}