initial commit
This commit is contained in:
commit
4906bc8539
|
@ -0,0 +1 @@
|
|||
.gitignore
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
|
@ -0,0 +1,8 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/GodPress.iml" filepath="$PROJECT_DIR$/GodPress.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
|
@ -0,0 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="SqlDialectMappings">
|
||||
<file url="file://$PROJECT_DIR$/models/init.go" dialect="GenericSQL" />
|
||||
<file url="PROJECT" dialect="MariaDB" />
|
||||
</component>
|
||||
</project>
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
|
@ -0,0 +1,9 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="WEB_MODULE" version="4">
|
||||
<component name="Go" enabled="true" />
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
|
@ -0,0 +1,31 @@
|
|||
module GodPress
|
||||
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/gin-contrib/gzip v0.0.5
|
||||
github.com/gin-gonic/gin v1.7.7
|
||||
github.com/go-ini/ini v1.66.4
|
||||
github.com/go-sql-driver/mysql v1.6.0
|
||||
gorm.io/driver/mysql v1.3.3
|
||||
gorm.io/gorm v1.23.5
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||
github.com/go-playground/locales v0.13.0 // indirect
|
||||
github.com/go-playground/universal-translator v0.17.0 // indirect
|
||||
github.com/go-playground/validator/v10 v10.4.1 // indirect
|
||||
github.com/golang/protobuf v1.3.3 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/jinzhu/now v1.1.4 // indirect
|
||||
github.com/json-iterator/go v1.1.9 // indirect
|
||||
github.com/leodido/go-urn v1.2.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.12 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
|
||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect
|
||||
github.com/ugorji/go/codec v1.1.7 // indirect
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
|
||||
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 // indirect
|
||||
gopkg.in/yaml.v2 v2.2.8 // indirect
|
||||
)
|
|
@ -0,0 +1,72 @@
|
|||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/gin-contrib/gzip v0.0.5 h1:mhnVU32YnnBh2LPH2iqRqsA/eR7SAqRaD388jL2s/j0=
|
||||
github.com/gin-contrib/gzip v0.0.5/go.mod h1:OPIK6HR0Um2vNmBUTlayD7qle4yVVRZT0PyhdUigrKk=
|
||||
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.7.4/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY=
|
||||
github.com/gin-gonic/gin v1.7.7 h1:3DoBmSbJbZAWqXJC3SLjAPfutPJJRN1U5pALB7EeTTs=
|
||||
github.com/gin-gonic/gin v1.7.7/go.mod h1:axIBovoeJpVj8S3BwE0uPMTeReE4+AfFtqpqaZ1qq1U=
|
||||
github.com/go-ini/ini v1.66.4 h1:dKjMqkcbkzfddhIhyglTPgMoJnkvmG+bSLrU9cTHc5M=
|
||||
github.com/go-ini/ini v1.66.4/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
|
||||
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
|
||||
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
|
||||
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
|
||||
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
|
||||
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
|
||||
github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE=
|
||||
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
|
||||
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
|
||||
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
|
||||
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
|
||||
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
||||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||
github.com/jinzhu/now v1.1.4 h1:tHnRBy1i5F2Dh8BAFxqFzxKqqvezXrL2OW1TnX+Mlas=
|
||||
github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
||||
github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns=
|
||||
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
|
||||
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
|
||||
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
|
||||
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||
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 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg=
|
||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
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.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
|
||||
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
|
||||
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI=
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=
|
||||
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
|
||||
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gorm.io/driver/mysql v1.3.3 h1:jXG9ANrwBc4+bMvBcSl8zCfPBaVoPyBEBshA8dA93X8=
|
||||
gorm.io/driver/mysql v1.3.3/go.mod h1:ChK6AHbHgDCFZyJp0F+BmVGb06PSIoh9uVYKAlRbb2U=
|
||||
gorm.io/gorm v1.23.1/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
|
||||
gorm.io/gorm v1.23.5 h1:TnlF26wScKSvknUC/Rn8t0NLLM22fypYBlvj1+aH6dM=
|
||||
gorm.io/gorm v1.23.5/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
|
|
@ -0,0 +1,20 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"GodPress/models"
|
||||
"GodPress/prjutil"
|
||||
"GodPress/routers"
|
||||
)
|
||||
|
||||
func main() {
|
||||
r := routers.InitRouter()
|
||||
prjutil.InfoLogger.Println("server start")
|
||||
err := r.Run(":" + prjutil.ServerConf.Port)
|
||||
if err != nil {
|
||||
prjutil.FatalLogger.Println("Server start failed", err)
|
||||
}
|
||||
err = models.GoDB.Close()
|
||||
if err != nil {
|
||||
prjutil.FatalLogger.Println("DB close failed", err)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"GodPress/prjutil"
|
||||
"database/sql"
|
||||
"time"
|
||||
)
|
||||
import _ "github.com/go-sql-driver/mysql"
|
||||
import gormSql "gorm.io/driver/mysql"
|
||||
import "gorm.io/gorm"
|
||||
|
||||
var DB *gorm.DB
|
||||
var GoDB *sql.DB
|
||||
|
||||
func init() {
|
||||
prjutil.InfoLogger.Println("init models")
|
||||
var err error
|
||||
GoDB, err = sql.Open("mysql", prjutil.DbConf.User+":"+prjutil.DbConf.Password+"@tcp("+prjutil.DbConf.Host+":"+prjutil.DbConf.Port+")/"+prjutil.DbConf.Database+"?charset=utf8mb4&&parseTime=true")
|
||||
if err != nil {
|
||||
prjutil.FatalLogger.Println("init models error:", err)
|
||||
}
|
||||
DB, err = gorm.Open(gormSql.New(gormSql.Config{Conn: GoDB}), &gorm.Config{})
|
||||
if err != nil {
|
||||
prjutil.FatalLogger.Println("init models error:", err)
|
||||
}
|
||||
GoDB.SetMaxIdleConns(10)
|
||||
GoDB.SetMaxOpenConns(100)
|
||||
GoDB.SetConnMaxLifetime(time.Minute * 3)
|
||||
prjutil.InfoLogger.Println("init models success")
|
||||
_, err = GoDB.Exec("create database if not exists godpress")
|
||||
if err != nil {
|
||||
prjutil.FatalLogger.Println("create database failed:", err)
|
||||
}
|
||||
_, err = GoDB.Exec("use godpress")
|
||||
err = DB.AutoMigrate()
|
||||
if err != nil {
|
||||
prjutil.FatalLogger.Println("auto migrate failed:", err)
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
package prjutil
|
||||
|
||||
import (
|
||||
"github.com/go-ini/ini"
|
||||
"log"
|
||||
)
|
||||
|
||||
var DbConf = struct {
|
||||
Host string
|
||||
Port string
|
||||
User string
|
||||
Password string
|
||||
Database string
|
||||
}{}
|
||||
|
||||
var FilePath = struct {
|
||||
PicSavePath string
|
||||
AvatarSavePath string
|
||||
VideoSavePath string
|
||||
LogPath string
|
||||
}{}
|
||||
|
||||
var ServerConf = struct {
|
||||
Port string
|
||||
Hostname string
|
||||
RunLevel string
|
||||
LogOutput string
|
||||
}{}
|
||||
|
||||
func init() {
|
||||
log.Printf("Reading config\n")
|
||||
cfg, err := ini.Load("conf.ini")
|
||||
if err != nil {
|
||||
FatalLogger.Fatalf("Failed to read config:%v", err.Error())
|
||||
}
|
||||
dbSection := cfg.Section("database")
|
||||
{
|
||||
DbConf.Host = dbSection.Key("Host").String()
|
||||
DbConf.Port = dbSection.Key("Port").String()
|
||||
DbConf.User = dbSection.Key("User").String()
|
||||
DbConf.Password = dbSection.Key("Password").String()
|
||||
DbConf.Database = dbSection.Key("Database").String()
|
||||
}
|
||||
fileSection := cfg.Section("file")
|
||||
{
|
||||
FilePath.PicSavePath = fileSection.Key("PicSavePath").String()
|
||||
FilePath.AvatarSavePath = fileSection.Key("AvatarSavePath").String()
|
||||
FilePath.VideoSavePath = fileSection.Key("VideoSavePath").String()
|
||||
FilePath.LogPath = fileSection.Key("LogPath").String()
|
||||
}
|
||||
srvSection := cfg.Section("server")
|
||||
{
|
||||
ServerConf.Port = srvSection.Key("Port").String()
|
||||
ServerConf.Hostname = srvSection.Key("Hostname").String()
|
||||
ServerConf.RunLevel = srvSection.Key("RunLevel").String()
|
||||
ServerConf.LogOutput = srvSection.Key("LogOutput").String()
|
||||
}
|
||||
log.Printf("Reading config complete\n")
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package prjutil
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
var WarningLogger *log.Logger
|
||||
var ErrorLogger *log.Logger
|
||||
var InfoLogger *log.Logger
|
||||
var DebugLogger *log.Logger
|
||||
var FatalLogger *log.Logger
|
||||
|
||||
func init() {
|
||||
InfoLogger = log.New(os.Stdout, "[INF] ", log.Ldate|log.Ltime|log.Llongfile)
|
||||
ErrorLogger = log.New(os.Stdout, "[ERR] ", log.Ldate|log.Ltime|log.Llongfile)
|
||||
WarningLogger = log.New(os.Stdout, "[WAR] ", log.Ldate|log.Ltime|log.Llongfile)
|
||||
DebugLogger = log.New(os.Stdout, "[DBG] ", log.Ldate|log.Ltime|log.Llongfile)
|
||||
FatalLogger = log.New(os.Stdout, "[FAT] ", log.Ldate|log.Ltime|log.Llongfile)
|
||||
if ServerConf.LogOutput == "file" {
|
||||
InfoLogger.Printf("Log will be output to path: %s\n", FilePath.LogPath)
|
||||
logFile, err := os.OpenFile(FilePath.LogPath+"jryy.log", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
|
||||
if err != nil {
|
||||
log.Fatalln("open log file error:", err)
|
||||
}
|
||||
InfoLogger.SetOutput(logFile)
|
||||
ErrorLogger.SetOutput(logFile)
|
||||
WarningLogger.SetOutput(logFile)
|
||||
DebugLogger.SetOutput(logFile)
|
||||
FatalLogger.SetOutput(logFile)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package prjutil
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
//GetFileMD5 获取文件的MD5值
|
||||
func GetFileMD5(filepath string) (string, error) {
|
||||
file, err := os.Open(filepath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
hash := md5.New()
|
||||
_, _ = io.Copy(hash, file)
|
||||
return hex.EncodeToString(hash.Sum(nil)), nil
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package prjutil
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestGetFileMD5(t *testing.T) {
|
||||
type args struct {
|
||||
filepath string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want string
|
||||
wantErr bool
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := GetFileMD5(tt.args.filepath)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("GetFileMD5() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if got != tt.want {
|
||||
t.Errorf("GetFileMD5() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
package prjutil
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
//SaveFile 从Context中保存文件
|
||||
func SaveFile(c *gin.Context, savePath string) (error, string) {
|
||||
file, err := c.FormFile("file")
|
||||
if err != nil {
|
||||
return err, ""
|
||||
}
|
||||
err = c.SaveUploadedFile(file, savePath+file.Filename)
|
||||
if err != nil {
|
||||
return err, ""
|
||||
}
|
||||
return nil, file.Filename
|
||||
}
|
||||
|
||||
//RenameMD5 以MD5重命名
|
||||
func RenameMD5(saveFolder string, filename string) (error, string) {
|
||||
md5str, err := GetFileMD5(saveFolder + filename)
|
||||
dot := strings.LastIndex(filename, ".")
|
||||
ext := filename[dot:]
|
||||
if err != nil {
|
||||
return err, ""
|
||||
}
|
||||
err = os.Rename(saveFolder+filename, saveFolder+md5str+ext)
|
||||
if err != nil {
|
||||
return err, ""
|
||||
}
|
||||
return nil, md5str + ext
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package prjutil
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
var UuidBinding = map[string]uint{}
|
||||
|
||||
//NoActualUser 没找到用户
|
||||
const NoActualUser = 0
|
||||
|
||||
//GetUserFromHeader 从请求头的UUID中获取用户,若无用户则返回-1
|
||||
func GetUserFromHeader(c *gin.Context) uint {
|
||||
headerStr := c.GetHeader("token")
|
||||
if headerStr == "" {
|
||||
return NoActualUser
|
||||
}
|
||||
uid, ok := UuidBinding[headerStr]
|
||||
if !ok {
|
||||
return NoActualUser
|
||||
}
|
||||
return uid
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package prjutil
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestGetUserFromHeader(t *testing.T) {
|
||||
type args struct {
|
||||
c *gin.Context
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want uint
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := GetUserFromHeader(tt.args.c); got != tt.want {
|
||||
t.Errorf("GetUserFromHeader() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package routers
|
||||
|
||||
import (
|
||||
"GodPress/prjutil"
|
||||
"github.com/gin-contrib/gzip"
|
||||
"github.com/gin-gonic/gin"
|
||||
"os"
|
||||
)
|
||||
|
||||
func InitRouter() *gin.Engine {
|
||||
r := gin.Default()
|
||||
r.Use(gzip.Gzip(gzip.DefaultCompression))
|
||||
err := r.SetTrustedProxies([]string{"127.0.0.1"})
|
||||
gin.SetMode(prjutil.ServerConf.RunLevel)
|
||||
if prjutil.ServerConf.LogOutput == "file" {
|
||||
prjutil.InfoLogger.Println("log output to file")
|
||||
gin.DefaultWriter, _ = os.OpenFile(prjutil.FilePath.LogPath+"gin.log", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
|
||||
}
|
||||
if err != nil {
|
||||
prjutil.ErrorLogger.Println("Failed to set trusted proxies", err)
|
||||
}
|
||||
api1 := r.Group("api1")
|
||||
|
||||
api1.GET("ping", Ping)
|
||||
return r
|
||||
}
|
||||
|
||||
func Ping(c *gin.Context) {
|
||||
c.JSON(200, gin.H{
|
||||
"message": "pong",
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue