Apply .gitignore rules
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"common/connection"
|
||||
"goutil/logUtilPlus"
|
||||
)
|
||||
|
||||
// AddAdmin 添加管理员
|
||||
// AddAdmin 添加新的管理员到数据库中。
|
||||
// 参数 admin: 包含管理员信息的对象。
|
||||
// 返回值: 插入操作影响的行数和可能发生的错误。
|
||||
func AddAdmin(admin *Admin) (int64, error) {
|
||||
|
||||
//处理一些验证
|
||||
|
||||
// 写入到数据库
|
||||
result := connection.GetAdminDB().Create(&admin) // 通过数据的指针来创建
|
||||
|
||||
if result.Error != nil {
|
||||
logUtilPlus.ErrorLog("添加管理员失败 错误信息:", result.Error.Error())
|
||||
}
|
||||
return admin.ID, nil
|
||||
}
|
||||
|
||||
// GetAdminByID 根据管理员ID获取管理员信息
|
||||
func GetAdminByID(adminID int64) (*Admin, error) {
|
||||
var admin Admin
|
||||
result := connection.GetAdminDB().First(&admin, adminID)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
return &admin, nil
|
||||
}
|
||||
|
||||
// 管理员登录
|
||||
func Login(account string, password string) (*Admin, error) {
|
||||
var admin Admin
|
||||
result := connection.GetAdminDB().Where("account = ? AND password = ?", account, password).First(&admin)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
return &admin, nil
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package impl_localfile
|
||||
|
||||
import "sync"
|
||||
|
||||
func (l *Logger) InfoLog(format string, args ...interface{}) {
|
||||
l.log(getLog(format, args...), info, true)
|
||||
}
|
||||
|
||||
func (l *Logger) WarnLog(format string, args ...interface{}) {
|
||||
l.log(getLog(format, args...), warn, true)
|
||||
}
|
||||
|
||||
func (l *Logger) DebugLog(format string, args ...interface{}) {
|
||||
l.log(getLog(format, args...), debug, true)
|
||||
}
|
||||
|
||||
func (l *Logger) ErrorLog(format string, args ...interface{}) {
|
||||
l.log(getLog(format, args...), _error, true)
|
||||
}
|
||||
|
||||
func (l *Logger) FatalLog(format string, args ...interface{}) {
|
||||
l.log(getLog(format, args...), fatal, true)
|
||||
}
|
||||
|
||||
func (l *Logger) CloseLog(waitFinish bool) {
|
||||
wg := sync.WaitGroup{}
|
||||
|
||||
// 关闭所有的file
|
||||
for _, logger := range l.loggerMap {
|
||||
wg.Add(1)
|
||||
go logger.Close(&wg, waitFinish)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
package configUtil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"goutil/xmlUtil"
|
||||
)
|
||||
|
||||
// bool值读取测试
|
||||
func TestBoolList(t *testing.T) {
|
||||
xmlConfigData, errMsg := getxmlConfigListData()
|
||||
if errMsg != nil {
|
||||
t.Error(errMsg)
|
||||
t.Fail()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
booList, errMsg := xmlConfigData.BoolList("html/body/ul/li/a", "id")
|
||||
if errMsg != nil {
|
||||
t.Error(errMsg)
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
fmt.Println("TestBoolList读取到的值:", booList)
|
||||
}
|
||||
|
||||
// int值读取测试
|
||||
func TestIntList(t *testing.T) {
|
||||
xmlConfigData, errMsg := getxmlConfigListData()
|
||||
if errMsg != nil {
|
||||
t.Error(errMsg)
|
||||
t.Fail()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
valList, errMsg := xmlConfigData.IntList("html/body/ul/li/a", "id")
|
||||
if errMsg != nil {
|
||||
t.Error(errMsg)
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
fmt.Println("TestInt读取到的值:", valList)
|
||||
}
|
||||
|
||||
// int64值读取测试
|
||||
func TestInt64List(t *testing.T) {
|
||||
xmlConfigData, errMsg := getxmlConfigListData()
|
||||
if errMsg != nil {
|
||||
t.Error(errMsg)
|
||||
t.Fail()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
valList, errMsg := xmlConfigData.Int64List("html/body/ul/li/a", "id")
|
||||
if errMsg != nil {
|
||||
t.Error(errMsg)
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
fmt.Println("TestInt64读取到的值:", valList)
|
||||
}
|
||||
|
||||
// Float值读取测试
|
||||
func TestFloatList(t *testing.T) {
|
||||
xmlConfigData, errMsg := getxmlConfigListData()
|
||||
if errMsg != nil {
|
||||
t.Error(errMsg)
|
||||
t.Fail()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
valList, errMsg := xmlConfigData.FloatList("html/body/ul/li/a", "id")
|
||||
if errMsg != nil {
|
||||
t.Error(errMsg)
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
fmt.Println("TestFloat读取到的值:", valList)
|
||||
}
|
||||
|
||||
// 字符串读取测试
|
||||
func TestStringList(t *testing.T) {
|
||||
xmlConfigData, errMsg := getxmlConfigListData()
|
||||
if errMsg != nil {
|
||||
t.Error(errMsg)
|
||||
t.Fail()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
valList, errMsg := xmlConfigData.StringList("html/body/ul/li/a", "id")
|
||||
if errMsg != nil {
|
||||
t.Error(errMsg)
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
fmt.Println("TestString读取到的值:", valList)
|
||||
}
|
||||
|
||||
func getxmlConfigListData() (xmlConfigData *XmlConfig, errMsg error) {
|
||||
content := `
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>Hello</title>
|
||||
<meta name="language" content="en"/>
|
||||
</head>
|
||||
<body IsPost='true'>
|
||||
<h1> This is a H1 </h1>
|
||||
<ul>
|
||||
<li><a id="1" dd='1.1' href="/">Home</a></li>
|
||||
<li><a id="2" href="/about">about</a></li>
|
||||
<li><a id="3" href="/account">login</a></li>
|
||||
<li></li>
|
||||
</ul>
|
||||
<p>
|
||||
Hello,This is an example for gxpath.
|
||||
</p>
|
||||
<footer>footer script</footer>
|
||||
</body>
|
||||
</html>
|
||||
`
|
||||
var root *xmlUtil.Node
|
||||
root, errMsg = xmlUtil.LoadFromString(content)
|
||||
if errMsg == nil {
|
||||
xmlConfigData = NewXmlConfig()
|
||||
xmlConfigData.LoadFromXmlNode(root)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
/*
|
||||
数学助手包,提供一些与数学相关的助手方法
|
||||
*/
|
||||
package mathUtil
|
||||
@@ -0,0 +1,216 @@
|
||||
// 适用于大量http(s)请求,连接复用
|
||||
package webUtil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptrace"
|
||||
"net/url"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
traceCtx context.Context
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
type TransportOPT struct {
|
||||
//超时时间
|
||||
Timeout time.Duration
|
||||
|
||||
//代理字符串,如"http://127.0.0.1:1080"
|
||||
ProxyString string
|
||||
|
||||
//最大保持连接数
|
||||
MaxIdleConns int
|
||||
|
||||
//每个主机的最大保持连接数
|
||||
MaxIdleConnsPerHost int
|
||||
|
||||
//单主机最大连接数
|
||||
MaxConnsPerHost int
|
||||
|
||||
//保持连接的超时时间
|
||||
IdleConnTimeout time.Duration
|
||||
|
||||
//禁止保持连接
|
||||
DisableKeepAlives bool
|
||||
}
|
||||
|
||||
// GET请求
|
||||
func (c *Client) Get(urlStr string, header map[string]string) (result []byte, err error) {
|
||||
req, err := http.NewRequestWithContext(c.traceCtx, http.MethodGet, urlStr, nil)
|
||||
if err != nil {
|
||||
//fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
// 处理头部(包括默认头部,以及传入的头部集合)
|
||||
if header == nil {
|
||||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
||||
} else {
|
||||
for k, v := range header {
|
||||
req.Header.Add(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
res, err := c.client.Do(req)
|
||||
if err != nil {
|
||||
//fmt.Println(err)
|
||||
return
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
result, err = ioutil.ReadAll(res.Body)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// POST请求
|
||||
func (c *Client) PostWithMap(urlStr string, data map[string]string, header map[string]string) (result []byte, err error) {
|
||||
// 组装POST数据
|
||||
postValues := url.Values{}
|
||||
for key, value := range data {
|
||||
postValues.Set(key, value)
|
||||
}
|
||||
postDataStr := postValues.Encode()
|
||||
byteData := []byte(postDataStr)
|
||||
|
||||
var req *http.Request
|
||||
req, err = http.NewRequestWithContext(c.traceCtx, http.MethodPost, urlStr, bytes.NewReader(byteData))
|
||||
if err != nil {
|
||||
//fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
// 处理头部(包括默认头部,以及传入的头部集合)
|
||||
if header == nil {
|
||||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
||||
} else {
|
||||
for k, v := range header {
|
||||
req.Header.Add(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
var res *http.Response
|
||||
res, err = c.client.Do(req)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
result, err = ioutil.ReadAll(res.Body)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// POST请求
|
||||
func (c *Client) PostWithByte(urlStr string, data []byte, header map[string]string) (result []byte, err error) {
|
||||
var req *http.Request
|
||||
req, err = http.NewRequestWithContext(c.traceCtx, http.MethodPost, urlStr, bytes.NewReader(data))
|
||||
if err != nil {
|
||||
//fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
// 处理头部(包括默认头部,以及传入的头部集合)
|
||||
if header == nil {
|
||||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
||||
} else {
|
||||
for k, v := range header {
|
||||
req.Header.Add(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
var res *http.Response
|
||||
res, err = c.client.Do(req)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
result, err = ioutil.ReadAll(res.Body)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 新建Client对象
|
||||
// transportOPT参数说明
|
||||
//
|
||||
// Timeout - 连接绝对超时时间
|
||||
// ProxyString - 代理字符串
|
||||
// MaxIdleConns - 最大空闲连接数
|
||||
// MaxIdleConnsPerHost - 每个目标主机的最大空闲连接数
|
||||
// MaxConnsPerHost - 每个目标主机的最大连接数
|
||||
// IdleConnTimeout - 空闲连接超时时间
|
||||
// DisableKeepAlives - 禁用连接保持(要使用连接复用,此值不传入或传入false;否则连接可能不会复用)
|
||||
func NewClient(transportOPT *TransportOPT) (c *Client) {
|
||||
c = &Client{}
|
||||
|
||||
//代理
|
||||
getProxy := func() func(*http.Request) (*url.URL, error) {
|
||||
if (transportOPT != nil) && len(transportOPT.ProxyString) > 0 {
|
||||
uri, _ := url.Parse(transportOPT.ProxyString)
|
||||
return http.ProxyURL(uri)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 默认参数
|
||||
timeout := 30 * time.Second
|
||||
maxIdleConns := 60000
|
||||
maxIdleConnsPerHost := 30000
|
||||
maxConnsPerHost := 30000
|
||||
idleConnTimeout := time.Minute * 1
|
||||
disableKeepAlives := false
|
||||
|
||||
if transportOPT != nil {
|
||||
// 根据传入参数修改默认参数
|
||||
if transportOPT.Timeout > 0 {
|
||||
timeout = transportOPT.Timeout
|
||||
}
|
||||
if transportOPT.MaxIdleConns > 0 {
|
||||
maxIdleConns = transportOPT.MaxIdleConns
|
||||
}
|
||||
if transportOPT.MaxIdleConnsPerHost > 0 {
|
||||
maxIdleConnsPerHost = transportOPT.MaxIdleConnsPerHost
|
||||
}
|
||||
if transportOPT.MaxConnsPerHost > 0 {
|
||||
maxConnsPerHost = transportOPT.MaxConnsPerHost
|
||||
}
|
||||
if transportOPT.IdleConnTimeout > 0 {
|
||||
idleConnTimeout = transportOPT.IdleConnTimeout
|
||||
}
|
||||
|
||||
disableKeepAlives = transportOPT.DisableKeepAlives
|
||||
}
|
||||
|
||||
clientTrace := &httptrace.ClientTrace{
|
||||
//GotConn: func(gci httptrace.GotConnInfo) {
|
||||
// fmt.Printf("conn was reused: %t\n", gci.Reused)
|
||||
//},
|
||||
}
|
||||
|
||||
c.traceCtx = httptrace.WithClientTrace(context.Background(), clientTrace)
|
||||
c.client = &http.Client{
|
||||
Timeout: timeout,
|
||||
|
||||
Transport: &http.Transport{
|
||||
Proxy: getProxy(),
|
||||
MaxIdleConns: maxIdleConns,
|
||||
MaxIdleConnsPerHost: maxIdleConnsPerHost,
|
||||
MaxConnsPerHost: maxConnsPerHost,
|
||||
IdleConnTimeout: idleConnTimeout,
|
||||
DisableKeepAlives: disableKeepAlives,
|
||||
// ForceAttemptHTTP2: true,
|
||||
},
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user