添加接口
This commit is contained in:
parent
6adf6210db
commit
14f03e42fb
|
@ -2,7 +2,9 @@ package cmd
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/huoxue1/min-report/config"
|
||||
"github.com/huoxue1/min-report/module"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
@ -16,6 +18,7 @@ var (
|
|||
},
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
config.InitConfig(configFile)
|
||||
module.InitDb(viper.GetString("db.driver"), viper.GetString("db.url"))
|
||||
},
|
||||
}
|
||||
)
|
||||
|
|
|
@ -8,6 +8,10 @@ import (
|
|||
|
||||
func server() *gin.Engine {
|
||||
engine := gin.Default()
|
||||
engine.GET("/health", func(ctx *gin.Context) {
|
||||
ctx.JSON(200, "ok")
|
||||
})
|
||||
engine.POST("/v1/api/report", utils.CreateHandlerFunc(handler.Report))
|
||||
engine.POST("/v1/api/captcha", handler.HandleCaptcha())
|
||||
return engine
|
||||
}
|
||||
|
|
|
@ -1,3 +1,9 @@
|
|||
http:
|
||||
address: 0.0.0.0
|
||||
port: 8080
|
||||
|
||||
db:
|
||||
driver: "mysql"
|
||||
url: root:123@tcp(127.0.0.1:3306)/min_report
|
||||
captcha:
|
||||
url: ""
|
7
go.mod
7
go.mod
|
@ -5,6 +5,7 @@ go 1.20
|
|||
require (
|
||||
github.com/gin-gonic/gin v1.9.1
|
||||
github.com/go-sql-driver/mysql v1.7.1
|
||||
github.com/huoxue1/go-utils v0.0.0-20230406033646-db359518379e
|
||||
github.com/sirupsen/logrus v1.9.3
|
||||
github.com/spf13/cobra v0.0.5
|
||||
github.com/spf13/viper v1.3.2
|
||||
|
@ -13,6 +14,7 @@ require (
|
|||
)
|
||||
|
||||
require (
|
||||
github.com/Lyrics-you/sail-logrus-formatter v1.3.1 // indirect
|
||||
github.com/bytedance/sonic v1.9.1 // indirect
|
||||
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
|
@ -31,6 +33,8 @@ require (
|
|||
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
|
||||
github.com/leodido/go-urn v1.2.4 // indirect
|
||||
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible // indirect
|
||||
github.com/lestrrat-go/strftime v1.0.6 // indirect
|
||||
github.com/magiconair/properties v1.8.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.19 // indirect
|
||||
github.com/mitchellh/mapstructure v1.1.2 // indirect
|
||||
|
@ -38,6 +42,7 @@ require (
|
|||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/pelletier/go-toml v1.2.0 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
github.com/spf13/afero v1.1.2 // indirect
|
||||
github.com/spf13/cast v1.3.0 // indirect
|
||||
|
@ -54,7 +59,7 @@ require (
|
|||
golang.org/x/text v0.9.0 // indirect
|
||||
golang.org/x/tools v0.6.0 // indirect
|
||||
google.golang.org/protobuf v1.30.0 // indirect
|
||||
gopkg.in/yaml.v2 v2.2.2 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
lukechampine.com/uint128 v1.2.0 // indirect
|
||||
modernc.org/cc/v3 v3.40.0 // indirect
|
||||
|
|
16
go.sum
16
go.sum
|
@ -6,6 +6,8 @@ gitee.com/travelliu/dm v1.8.11192/go.mod h1:DHTzyhCrM843x9VdKVbZ+GKXGRbKM2sJ4Lxi
|
|||
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
|
||||
github.com/Lyrics-you/sail-logrus-formatter v1.3.1 h1:y/9QraPbDwfccHa4QFZ9g2zNiPoSoQnE5MYizWLiYwY=
|
||||
github.com/Lyrics-you/sail-logrus-formatter v1.3.1/go.mod h1:e9FX8+3MxwQGGkK+8ne3kRpu0gaBc9QTE7jtP+zP070=
|
||||
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
|
||||
github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
|
||||
github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI=
|
||||
|
@ -169,6 +171,8 @@ github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/J
|
|||
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
|
||||
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
||||
github.com/hudl/fargo v1.3.0/go.mod h1:y3CKSmjA+wD2gak7sUSXTAoopbhU08POFhmITJgmKTg=
|
||||
github.com/huoxue1/go-utils v0.0.0-20230406033646-db359518379e h1:clQ/DsLQf9Ye97a65mDtF9Tl9HCHlRfesD1u/Lan3Aw=
|
||||
github.com/huoxue1/go-utils v0.0.0-20230406033646-db359518379e/go.mod h1:39wbuJr0Z2wbgxAEAx3gXR0iJg2cwj8wrbsSc3QpztE=
|
||||
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
|
||||
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
||||
github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
|
||||
|
@ -221,6 +225,7 @@ github.com/jackc/puddle v1.1.1/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dv
|
|||
github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
|
||||
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
|
||||
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
|
||||
github.com/jonboulle/clockwork v0.4.0 h1:p4Cf1aMWXnXAUh8lVfewRBx1zaTSYKrKMF2g3ST4RZ4=
|
||||
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
|
||||
github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||
github.com/json-iterator/go v1.1.8/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||
|
@ -246,6 +251,12 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
|||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
|
||||
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
|
||||
github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc h1:RKf14vYWi2ttpEmkA4aQ3j4u9dStX2t4M8UM6qqNsG8=
|
||||
github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc/go.mod h1:kopuH9ugFRkIXf3YoqHKyrJ9YfUFsckUU9S7B+XP+is=
|
||||
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible h1:Y6sqxHMyB1D2YSzWkLibYKgg+SwmyFU9dF2hn6MdTj4=
|
||||
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible/go.mod h1:ZQnN8lSECaebrkQytbHj4xNgtg8CR7RYXnPok8e0EHA=
|
||||
github.com/lestrrat-go/strftime v1.0.6 h1:CFGsDEt1pOpFNU+TJB0nhz9jl+K0hZSLE205AhTIGQQ=
|
||||
github.com/lestrrat-go/strftime v1.0.6/go.mod h1:f7jQKgV5nnJpYgdEasS+/y7EsTb8ykN2z68n3TtcTaw=
|
||||
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
||||
github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
||||
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
|
||||
|
@ -327,6 +338,8 @@ github.com/pierrec/lz4 v1.0.2-0.20190131084431-473cd7ce01a1/go.mod h1:3/3N9NVKO0
|
|||
github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
|
||||
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pkg/profile v1.2.1/go.mod h1:hJw3o1OdXxsrSjjVksARp5W95eeEaEfptyVZyv6JUPA=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
|
@ -607,8 +620,9 @@ gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWD
|
|||
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
|
||||
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
|
||||
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
||||
gopkg.in/yaml.v2 v2.2.2/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.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
package handler
|
||||
|
||||
import (
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/huoxue1/go-utils/base/log"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func HandleCaptcha() gin.HandlerFunc {
|
||||
target := viper.GetString("captcha.url")
|
||||
targetUrl, err := url.Parse(target)
|
||||
if err != nil {
|
||||
log.Panicln("captcha url parse error:", err)
|
||||
}
|
||||
return func(ctx *gin.Context) {
|
||||
proxy := httputil.NewSingleHostReverseProxy(targetUrl)
|
||||
ctx.Request.URL.Path = "/data"
|
||||
proxy.ServeHTTP(ctx.Writer, ctx.Request)
|
||||
}
|
||||
}
|
|
@ -2,10 +2,15 @@ package handler
|
|||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/huoxue1/min-report/module"
|
||||
"github.com/huoxue1/min-report/pojo"
|
||||
)
|
||||
|
||||
func Report(ctx context.Context, data *pojo.ReportData) (*pojo.CommonResp, error) {
|
||||
|
||||
func Report(ctx context.Context, req *pojo.ReportData) (*pojo.CommonResp, error) {
|
||||
_, err := module.GetReportEngine().Insert(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pojo.CommonResp{}, nil
|
||||
}
|
||||
|
|
20
module/db.go
20
module/db.go
|
@ -1,12 +1,28 @@
|
|||
package module
|
||||
|
||||
import (
|
||||
"github.com/huoxue1/go-utils/base/log"
|
||||
"github.com/huoxue1/min-report/pojo"
|
||||
"xorm.io/xorm"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
func init() {
|
||||
xorm.NewEngine("mysql", "root:123456@tcp()")
|
||||
var (
|
||||
engine *xorm.Engine
|
||||
)
|
||||
|
||||
func InitDb(driver, url string) {
|
||||
var err error
|
||||
engine, err = xorm.NewEngine(driver, url)
|
||||
if err != nil {
|
||||
log.Errorln("数据库连接失败")
|
||||
log.Panicln(err.Error())
|
||||
}
|
||||
err = engine.Charset("utf8mb4").Sync2(&pojo.ReportData{})
|
||||
if err != nil {
|
||||
log.Errorln("数据库同步失败")
|
||||
log.Panicln(err.Error())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1 +1,29 @@
|
|||
package module
|
||||
|
||||
import (
|
||||
"github.com/huoxue1/min-report/pojo"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
type ReportEngine struct {
|
||||
*xorm.Engine
|
||||
}
|
||||
|
||||
var (
|
||||
reportEngine *ReportEngine
|
||||
)
|
||||
|
||||
func newReportEngine() *ReportEngine {
|
||||
return &ReportEngine{engine}
|
||||
}
|
||||
|
||||
func GetReportEngine() *ReportEngine {
|
||||
if reportEngine == nil {
|
||||
reportEngine = newReportEngine()
|
||||
}
|
||||
return reportEngine
|
||||
}
|
||||
|
||||
func (r *ReportEngine) Insert(data *pojo.ReportData) (int64, error) {
|
||||
return r.Engine.Insert(data)
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ type ReportData struct {
|
|||
Platform string `json:"platform"`
|
||||
CourseName string `json:"course_name"`
|
||||
CourseId string `json:"course_id"`
|
||||
Ip string `json:"ip"`
|
||||
}
|
||||
|
||||
type CommonResp struct {
|
||||
|
|
Loading…
Reference in New Issue