mirror of
https://github.com/gotoeasy/glogcenter.git
synced 2025-09-15 12:58:34 +08:00
通过简单封装gin,添加web服务查询接口
This commit is contained in:
parent
a7907b2722
commit
6de7d1a8bf
@ -9,31 +9,51 @@ import (
|
||||
"hash/crc32"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// 字符串(10进制无符号整数形式)转uint32,超过uint32最大值会丢失精度
|
||||
// 字符串(10进制无符号整数形式)转int,超过int最大值会丢失精度
|
||||
// 转换失败时返回默认值
|
||||
func StringToUint32(s string, defaultVal uint32) uint32 {
|
||||
v, err := strconv.ParseUint(s, 10, 32)
|
||||
if err != nil {
|
||||
return defaultVal
|
||||
}
|
||||
return uint32(v & 0xFFFFFFFF)
|
||||
}
|
||||
|
||||
// 字符串(10进制无符号整数形式)转uint64
|
||||
// 转换失败时返回默认值
|
||||
func StringToUint64(s string, defaultVal uint64) uint64 {
|
||||
v, err := strconv.ParseUint(s, 10, 64)
|
||||
func StringToInt(s string, defaultVal int) int {
|
||||
v, err := strconv.Atoi(s)
|
||||
if err != nil {
|
||||
return defaultVal
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// // 字符串(10进制无符号整数形式)转uint32,超过uint32最大值会丢失精度
|
||||
// // 转换失败时返回默认值
|
||||
// func StringToUint32(s string, defaultVal uint32) uint32 {
|
||||
// v, err := strconv.ParseUint(s, 10, 32)
|
||||
// if err != nil {
|
||||
// return defaultVal
|
||||
// }
|
||||
// return uint32(v & 0xFFFFFFFF)
|
||||
// }
|
||||
|
||||
// 字符串(指定进制无符号整数形式)转uint64,进制base范围为2~36
|
||||
// 参数错误或转换失败都返回默认值
|
||||
func StringToUint64(s string, base int, defaultVal uint64) uint64 {
|
||||
if s == "" || base < 2 || base > 36 {
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
v, err := strconv.ParseUint(s, base, 64)
|
||||
if err != nil {
|
||||
return defaultVal
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// Uint64转指定进制形式字符串
|
||||
func Uint64ToString(val uint64, base int) string {
|
||||
return strconv.FormatUint(val, base)
|
||||
}
|
||||
|
||||
func StringToBytes(s string) []byte {
|
||||
return *(*[]byte)(unsafe.Pointer(
|
||||
&struct {
|
||||
@ -42,20 +62,30 @@ func StringToBytes(s string) []byte {
|
||||
}{s, len(s)},
|
||||
))
|
||||
}
|
||||
func StringToBool(s string, defaultVal bool) bool {
|
||||
lower := strings.ToLower(s)
|
||||
if lower == "true" {
|
||||
return true
|
||||
}
|
||||
if lower == "false" {
|
||||
return false
|
||||
}
|
||||
return defaultVal
|
||||
}
|
||||
|
||||
func BytesToString(b []byte) string {
|
||||
return *(*string)(unsafe.Pointer(&b))
|
||||
}
|
||||
|
||||
func Uint32ToBytes(num uint32) []byte {
|
||||
bkey := make([]byte, 4)
|
||||
binary.BigEndian.PutUint32(bkey, num)
|
||||
return bkey
|
||||
}
|
||||
// func Uint32ToBytes(num uint32) []byte {
|
||||
// bkey := make([]byte, 4)
|
||||
// binary.BigEndian.PutUint32(bkey, num)
|
||||
// return bkey
|
||||
// }
|
||||
|
||||
func BytesToUint32(bytes []byte) uint32 {
|
||||
return uint32(binary.BigEndian.Uint32(bytes))
|
||||
}
|
||||
// func BytesToUint32(bytes []byte) uint32 {
|
||||
// return uint32(binary.BigEndian.Uint32(bytes))
|
||||
// }
|
||||
|
||||
func Uint64ToBytes(num uint64) []byte {
|
||||
bkey := make([]byte, 8)
|
||||
|
||||
14
glc/cmn/cmn_test.go
Normal file
14
glc/cmn/cmn_test.go
Normal file
@ -0,0 +1,14 @@
|
||||
package cmn
|
||||
|
||||
import (
|
||||
"log"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_all(t *testing.T) {
|
||||
|
||||
// u64 := StringToUint64("18446744073709551615", 0)
|
||||
s64 := Uint64ToStringBase(10, 36)
|
||||
uu64 := StringToUint(s64, 36, 0)
|
||||
log.Println(s64, "-------------------", uu64, uu64)
|
||||
}
|
||||
22
glc/go.mod
22
glc/go.mod
@ -5,6 +5,28 @@ go 1.18
|
||||
require github.com/syndtr/goleveldb v1.0.0
|
||||
|
||||
require (
|
||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||
github.com/go-playground/locales v0.14.0 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.0 // indirect
|
||||
github.com/go-playground/validator/v10 v10.10.0 // indirect
|
||||
github.com/goccy/go-json v0.9.7 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/leodido/go-urn v1.2.1 // indirect
|
||||
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
|
||||
github.com/ugorji/go/codec v1.2.7 // indirect
|
||||
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 // indirect
|
||||
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069 // indirect
|
||||
golang.org/x/text v0.3.6 // indirect
|
||||
google.golang.org/protobuf v1.28.0 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/gin-gonic/gin v1.8.1
|
||||
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db // indirect
|
||||
github.com/wangbin/jiebago v0.3.2
|
||||
)
|
||||
|
||||
74
glc/go.sum
74
glc/go.sum
@ -1,20 +1,94 @@
|
||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
|
||||
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
|
||||
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
|
||||
github.com/gin-gonic/gin v1.8.1 h1:4+fr/el88TOO3ewCmQr8cx/CtZ/umlIRIs5M4NTNjf8=
|
||||
github.com/gin-gonic/gin v1.8.1/go.mod h1:ji8BvRH1azfM+SYow9zQ6SZMvR8qOMZHmsCuWR9tTTk=
|
||||
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||
github.com/go-playground/locales v0.14.0 h1:u50s323jtVGugKlcYeyzC0etD1HifMjqmJqb8WugfUU=
|
||||
github.com/go-playground/locales v0.14.0/go.mod h1:sawfccIbzZTqEDETgFXqTho0QybSa7l++s0DH+LDiLs=
|
||||
github.com/go-playground/universal-translator v0.18.0 h1:82dyy6p4OuJq4/CByFNOn/jYrnRPArHwAcmLoJZxyho=
|
||||
github.com/go-playground/universal-translator v0.18.0/go.mod h1:UvRDBj+xPUEGrFYl+lu/H90nyDXpg0fqeB/AQUGNTVA=
|
||||
github.com/go-playground/validator/v10 v10.10.0 h1:I7mrTYv78z8k8VXa/qJlOlEXn/nBh+BF8dHX5nt/dr0=
|
||||
github.com/go-playground/validator/v10 v10.10.0/go.mod h1:74x4gJWsvQexRdW8Pn3dXSGrTK4nAUsbPlLADvpJkos=
|
||||
github.com/goccy/go-json v0.9.7 h1:IcB+Aqpx/iMHu5Yooh7jEzJk1JZ7Pjtmys2ukPr7EeM=
|
||||
github.com/goccy/go-json v0.9.7/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
||||
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db h1:woRePGFeVFfLKN/pOkfl+p/TAqKOfFu+7KPlMVpok/w=
|
||||
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
|
||||
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
|
||||
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
|
||||
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
|
||||
github.com/pelletier/go-toml/v2 v2.0.1 h1:8e3L2cCQzLFi2CR4g7vGFuFxX7Jl1kKX8gW+iV0GUKU=
|
||||
github.com/pelletier/go-toml/v2 v2.0.1/go.mod h1:r9LEWfGN8R5k0VXJ+0BkIe7MYkRdwZOjgMj2KwnJFUo=
|
||||
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
|
||||
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/syndtr/goleveldb v1.0.0 h1:fBdIW9lB4Iz0n9khmH8w27SJ3QEJ7+IgjPEwGSZiFdE=
|
||||
github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ=
|
||||
github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M=
|
||||
github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
|
||||
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=
|
||||
github.com/wangbin/jiebago v0.3.2 h1:reQKp0xTXWFK7eQ19L6Ofq5xODSR2hcam55qcdCCNpw=
|
||||
github.com/wangbin/jiebago v0.3.2/go.mod h1:PAqQLauF0qAzy/63jBvO7Goh0oYBq1ocr0OXHLlujwQ=
|
||||
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 h1:/UOmuWzQfxxo9UtlXMwuQU8CMgg1eZXqTRwkSQJWKOI=
|
||||
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 h1:qWPm9rbaAMKs8Bq/9LRpbMqxWRVUAQwMI9fVrssnTfw=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069 h1:siQdpVirKtzPhKl3lZWozZraCFObP8S1v6PRp0bLrtU=
|
||||
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
|
||||
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
|
||||
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
|
||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
||||
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
13
glc/gweb/http/start_httpserver.go
Normal file
13
glc/gweb/http/start_httpserver.go
Normal file
@ -0,0 +1,13 @@
|
||||
package http
|
||||
|
||||
import "glc/gweb"
|
||||
|
||||
// 注册控制器,启动web服务
|
||||
func StartHttpServer(fnRegister func()) {
|
||||
|
||||
// 注册控制器
|
||||
fnRegister()
|
||||
|
||||
// 启动web服务
|
||||
gweb.Run()
|
||||
}
|
||||
53
glc/gweb/http_register.go
Normal file
53
glc/gweb/http_register.go
Normal file
@ -0,0 +1,53 @@
|
||||
package gweb
|
||||
|
||||
import (
|
||||
"glc/gweb/method"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type HttpController struct {
|
||||
Method string
|
||||
Path string
|
||||
Controller func(*HttpRequest) *HttpResult
|
||||
}
|
||||
|
||||
var mapHandleGet map[string]*HttpController
|
||||
var mapHandlePost map[string]*HttpController
|
||||
|
||||
func init() {
|
||||
mapHandleGet = make(map[string]*HttpController)
|
||||
}
|
||||
|
||||
func getHttpController(methodType string, path string) *HttpController {
|
||||
switch methodType {
|
||||
case method.GET:
|
||||
return mapHandleGet[strings.ToLower(path)]
|
||||
case method.POST:
|
||||
return mapHandleGet[strings.ToLower(path)]
|
||||
default:
|
||||
panic("unsuport method: " + methodType)
|
||||
}
|
||||
}
|
||||
|
||||
func RegisterController(methodType string, path string, fnController func(*HttpRequest) *HttpResult) {
|
||||
pathLower := strings.ToLower(path) // path匹配比较忽略大小写
|
||||
if mapHandleGet[pathLower] != nil || mapHandlePost[pathLower] != nil {
|
||||
panic("duplicate controller path: " + path)
|
||||
}
|
||||
|
||||
r := &HttpController{
|
||||
Method: methodType,
|
||||
Path: pathLower,
|
||||
Controller: fnController,
|
||||
}
|
||||
|
||||
switch methodType {
|
||||
case method.GET:
|
||||
mapHandleGet[pathLower] = r
|
||||
case method.POST:
|
||||
mapHandleGet[pathLower] = r
|
||||
default:
|
||||
panic("unsuport method: " + methodType)
|
||||
}
|
||||
|
||||
}
|
||||
56
glc/gweb/http_request.go
Normal file
56
glc/gweb/http_request.go
Normal file
@ -0,0 +1,56 @@
|
||||
package gweb
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type HttpRequest struct {
|
||||
ginCtx *gin.Context
|
||||
mapHead map[string][]string
|
||||
}
|
||||
|
||||
func NewHttpRequest(c *gin.Context) *HttpRequest {
|
||||
|
||||
// header整理,键忽略大小写
|
||||
mapHead := make(map[string][]string)
|
||||
for k, v := range c.Request.Header {
|
||||
key := strings.ToLower(k)
|
||||
val := mapHead[key]
|
||||
if val == nil {
|
||||
val = []string{}
|
||||
}
|
||||
val = append(val, v...)
|
||||
mapHead[key] = val
|
||||
}
|
||||
|
||||
return &HttpRequest{
|
||||
ginCtx: c,
|
||||
mapHead: mapHead,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *HttpRequest) GetHeader(name string) string {
|
||||
ary := r.mapHead[strings.ToLower(name)]
|
||||
if ary == nil {
|
||||
return ""
|
||||
}
|
||||
return ary[0]
|
||||
}
|
||||
|
||||
func (r *HttpRequest) GetHeaders(name string) []string {
|
||||
ary := r.mapHead[strings.ToLower(name)]
|
||||
if ary == nil {
|
||||
return []string{}
|
||||
}
|
||||
return ary
|
||||
}
|
||||
|
||||
func (r *HttpRequest) GetUrlParameter(name string) string {
|
||||
return r.ginCtx.Query(name)
|
||||
}
|
||||
|
||||
func (r *HttpRequest) GetFormParameter(name string) string {
|
||||
return r.ginCtx.Request.PostFormValue(name)
|
||||
}
|
||||
66
glc/gweb/http_result.go
Normal file
66
glc/gweb/http_result.go
Normal file
@ -0,0 +1,66 @@
|
||||
package gweb
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"glc/cmn"
|
||||
)
|
||||
|
||||
type HttpResult struct {
|
||||
Code int `json:"code,omitempty"`
|
||||
Message string `json:"message,omitempty"`
|
||||
Success bool `json:"success,omitempty"`
|
||||
Result any `json:"result,omitempty"`
|
||||
}
|
||||
|
||||
func Error(code int, msg string) *HttpResult {
|
||||
return &HttpResult{
|
||||
Code: code,
|
||||
Message: msg,
|
||||
Success: false,
|
||||
}
|
||||
}
|
||||
|
||||
func Error500(msg string) *HttpResult {
|
||||
return &HttpResult{
|
||||
Code: 500,
|
||||
Message: msg,
|
||||
Success: false,
|
||||
}
|
||||
}
|
||||
|
||||
func Error404() *HttpResult {
|
||||
return &HttpResult{
|
||||
Code: 404,
|
||||
Message: "page not found",
|
||||
Success: false,
|
||||
}
|
||||
}
|
||||
|
||||
func Ok() *HttpResult {
|
||||
return &HttpResult{
|
||||
Code: 200,
|
||||
Success: true,
|
||||
}
|
||||
}
|
||||
|
||||
func Ok200(msg string) *HttpResult {
|
||||
return &HttpResult{
|
||||
Code: 200,
|
||||
Message: msg,
|
||||
Success: true,
|
||||
}
|
||||
}
|
||||
|
||||
func Result(result any) *HttpResult {
|
||||
return &HttpResult{
|
||||
Code: 200,
|
||||
Success: true,
|
||||
Result: result,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *HttpResult) ToJson() string {
|
||||
bt, _ := json.Marshal(r)
|
||||
return cmn.BytesToString(bt)
|
||||
|
||||
}
|
||||
30
glc/gweb/http_server.go
Normal file
30
glc/gweb/http_server.go
Normal file
@ -0,0 +1,30 @@
|
||||
package gweb
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Run() {
|
||||
server := gin.Default()
|
||||
|
||||
server.NoRoute(func(c *gin.Context) {
|
||||
req := NewHttpRequest(c)
|
||||
path := strings.ToLower(c.Request.URL.Path)
|
||||
method := strings.ToUpper(c.Request.Method)
|
||||
|
||||
handle := getHttpController(method, path)
|
||||
if handle == nil {
|
||||
c.JSON(http.StatusNotFound, Error404())
|
||||
return
|
||||
}
|
||||
|
||||
rs := handle.Controller(req)
|
||||
c.JSON(rs.Code, rs)
|
||||
})
|
||||
|
||||
server.Run(":8080")
|
||||
|
||||
}
|
||||
6
glc/gweb/method/http_method.go
Normal file
6
glc/gweb/method/http_method.go
Normal file
@ -0,0 +1,6 @@
|
||||
package method
|
||||
|
||||
const (
|
||||
GET string = "GET"
|
||||
POST string = "POST"
|
||||
)
|
||||
10
glc/main.go
Normal file
10
glc/main.go
Normal file
@ -0,0 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"glc/gweb/http"
|
||||
"glc/web/router"
|
||||
)
|
||||
|
||||
func main() {
|
||||
http.StartHttpServer(router.Register)
|
||||
}
|
||||
23
glc/web/controller/log_search_controller.go
Normal file
23
glc/web/controller/log_search_controller.go
Normal file
@ -0,0 +1,23 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"glc/cmn"
|
||||
"glc/gweb"
|
||||
"glc/ldb"
|
||||
"glc/ldb/tokenizer"
|
||||
)
|
||||
|
||||
func LogSearchController(req *gweb.HttpRequest) *gweb.HttpResult {
|
||||
storeNmae := req.GetUrlParameter("name")
|
||||
if storeNmae == "" {
|
||||
storeNmae = "default"
|
||||
}
|
||||
searchKey := tokenizer.GetSearchKey(req.GetUrlParameter("searchKey"))
|
||||
pageSize := cmn.StringToInt(req.GetUrlParameter("pageSize"), 20)
|
||||
currentId := cmn.StringToUint64(req.GetUrlParameter("currentId"), 36, 0)
|
||||
forward := cmn.StringToBool(req.GetUrlParameter("forward"), true)
|
||||
|
||||
eng := ldb.NewEngine(storeNmae)
|
||||
rs := eng.Search(searchKey, pageSize, currentId, forward)
|
||||
return gweb.Result(rs)
|
||||
}
|
||||
11
glc/web/router/register.go
Normal file
11
glc/web/router/register.go
Normal file
@ -0,0 +1,11 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"glc/gweb"
|
||||
"glc/gweb/method"
|
||||
"glc/web/controller"
|
||||
)
|
||||
|
||||
func Register() {
|
||||
gweb.RegisterController(method.GET, "/glc/list", controller.LogSearchController)
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user