-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathLghSampleMain.go
91 lines (74 loc) · 1.82 KB
/
LghSampleMain.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
package main
/**
* 作者:林冠宏
*
* author: LinGuanHong
*
* My GitHub : https://github.com/af913337456/
*
* My Blog : http://www.cnblogs.com/linguanh/
*
* */
/**
模板 main.go
*/
import (
"github.com/gorilla/mux"
"net/http"
"fmt"
"crypto/x509"
"io/ioutil"
"crypto/tls"
)
func test(w http.ResponseWriter,r *http.Request) {
fmt.Fprintf(w,"======= hello world! =======")
}
// http 监听
func httpListen(router *mux.Router) {
url := serverConfig.Host+serverConfig.Port
err := http.ListenAndServe(url,router)
if err !=nil {
Log("http 监听错误 :")
Log(err)
return
}
}
// https 监听
func httpsListen(router *mux.Router) {
basePath := "" // /home/lgh/
pool := x509.NewCertPool()
caCertPath := basePath+"" // ca.crt
caCrt, err := ioutil.ReadFile(caCertPath)
if err != nil {
Log("Read ca File err:", err)
return
}
pool.AppendCertsFromPEM(caCrt)
s := &http.Server{
Addr: serverConfig.Host+serverConfig.Port, // :8888
Handler: router,
TLSConfig: &tls.Config{
ClientCAs: pool,
ClientAuth: tls.RequireAndVerifyClientCert, /** 开启双向验证 */
},
}
s.ListenAndServeTLS(basePath+"server.crt",basePath+"server.key")
}
func setRouter() *mux.Router {
router := new (mux.Router)
router.HandleFunc("/",test).Methods("POST")
router.HandleFunc("/insert",insert_luser_sample).Methods("POST")
router.HandleFunc("/select",select_luser_sample).Methods("GET")
router.HandleFunc("/update",update_luser_sample).Methods("POST")
router.HandleFunc("/delete",delete_luser_sample).Methods("POST")
/** 在下面添加你的回调方法 */
/** add your func below */
return router
}
func main() {
bindServerConfig()
Log("配置信息:")
Log(serverConfig)
myDb = setDB(serverConfig.DbUser,serverConfig.DbPw,serverConfig.DbName,serverConfig.DbPort)
httpListen(setRouter())
}