From 3b6c5832ffb0ccebfc427fdf750d9e7579ed7afb Mon Sep 17 00:00:00 2001 From: xing Date: Thu, 1 Sep 2022 10:31:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E6=8D=A2=E4=B8=AA=E5=86=99?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 21 +++++++++-- main.go | 13 +++---- models/globalInit.go | 24 +++++++++++++ models/model.go | 72 ++++++++++++++------------------------ models/wp_comments.go | 3 -- models/wp_options.go | 3 -- models/wp_posts.go | 3 -- models/wp_term_taxonomy.go | 3 -- models/wp_terms.go | 3 -- route/actions.go | 12 +++++++ route/route.go | 16 +++++++++ templates/index.html | 12 +++++++ 12 files changed, 116 insertions(+), 69 deletions(-) create mode 100644 models/globalInit.go create mode 100644 route/actions.go create mode 100644 route/route.go create mode 100644 templates/index.html diff --git a/go.mod b/go.mod index 925130b..4c5fffe 100644 --- a/go.mod +++ b/go.mod @@ -3,13 +3,28 @@ module github/fthvgb1/wp-go go 1.18 require ( + github.com/gin-gonic/gin v1.8.1 github.com/go-sql-driver/mysql v1.6.0 github.com/jmoiron/sqlx v1.3.5 gopkg.in/yaml.v2 v2.4.0 ) require ( - github.com/kr/pretty v0.3.0 // indirect - github.com/rogpeppe/go-internal v1.8.0 // indirect - gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect + 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 ) diff --git a/main.go b/main.go index abd78a2..89a84f4 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,9 @@ package main import ( - "fmt" "github/fthvgb1/wp-go/db" "github/fthvgb1/wp-go/models" + "github/fthvgb1/wp-go/route" "github/fthvgb1/wp-go/vars" ) @@ -16,14 +16,15 @@ func init() { if err != nil { panic(err) } + err = models.InitOptions() + if err != nil { + panic(err) + } } func main() { - T, t, err := models.WpPostsM.SimplePagination(nil, "wp_posts.ID,b.meta_id post_author", 4, 2, nil, models.SqlBuilder{{ - "left join", "wp_postmeta b", "b.post_id=wp_posts.ID", - }}) + err := route.SetupRouter().Run(":8082") if err != nil { - return + panic(err) } - fmt.Println(T, t) } diff --git a/models/globalInit.go b/models/globalInit.go new file mode 100644 index 0000000..03483e0 --- /dev/null +++ b/models/globalInit.go @@ -0,0 +1,24 @@ +package models + +var Options = make(map[string]string) + +func InitOptions() error { + ops, err := SimpleFind[WpOptions](SqlBuilder{{"autoload", "yes"}}, "option_name, option_value") + if err != nil { + return err + } + if len(ops) == 0 { + ops, err = SimpleFind[WpOptions](nil, "option_name, option_value") + if err != nil { + return err + } + } + for _, options := range ops { + Options[options.OptionName] = options.OptionValue + } + return nil +} + +func InitTerms() { + //terms,err := WpTermsM.SimplePagination() +} diff --git a/models/model.go b/models/model.go index 9032d96..5cf0876 100644 --- a/models/model.go +++ b/models/model.go @@ -8,22 +8,11 @@ import ( "strings" ) -type mod interface { +type model interface { PrimaryKey() string Table() string } -type model[T mod] struct { -} - -func (m model[T]) PrimaryKey() string { - return "id" -} - -func (m model[T]) Table() string { - return "" -} - type SqlBuilder [][]string func (w SqlBuilder) parseWhere(in ...[]interface{}) (string, []interface{}) { @@ -115,24 +104,7 @@ func (w SqlBuilder) parseJoin() string { return s.String() } -func (m model[T]) Find(where SqlBuilder, fields string, order SqlBuilder, join SqlBuilder, limit int, in ...[]interface{}) (r []T, err error) { - var rr T - w, args := where.parseWhere(in...) - j := join.parseJoin() - tp := "select %s from %s %s %s %s %s" - l := "" - if limit > 0 { - l = fmt.Sprintf(" limit %d", limit) - } - sql := fmt.Sprintf(tp, fields, rr.Table(), j, w, order.parseOrderBy(), l) - err = db.Db.Select(&r, sql, args...) - if err != nil { - return - } - return -} - -func (m model[T]) SimplePagination(where SqlBuilder, fields string, page, pageSize int, order SqlBuilder, join SqlBuilder, in ...[]interface{}) (r []T, total int, err error) { +func SimplePagination[T model](where SqlBuilder, fields string, page, pageSize int, order SqlBuilder, join SqlBuilder, in ...[]interface{}) (r []T, total int, err error) { var rr T w, args := where.parseWhere(in...) n := struct { @@ -165,7 +137,7 @@ func (m model[T]) SimplePagination(where SqlBuilder, fields string, page, pageSi return } -func (m model[T]) FindOneById(id int) (T, error) { +func FindOneById[T model](id int) (T, error) { var r T sql := fmt.Sprintf("select * from `%s` where `%s`=?", r.Table(), r.PrimaryKey()) err := db.Db.Get(&r, sql, id) @@ -175,7 +147,7 @@ func (m model[T]) FindOneById(id int) (T, error) { return r, nil } -func (m model[T]) FirstOne(where SqlBuilder, fields string, in ...[]interface{}) (T, error) { +func FirstOne[T model](where SqlBuilder, fields string, in ...[]interface{}) (T, error) { var r T w, args := where.parseWhere(in...) tp := "select %s from %s %s" @@ -187,7 +159,7 @@ func (m model[T]) FirstOne(where SqlBuilder, fields string, in ...[]interface{}) return r, nil } -func (m model[T]) LastOne(where SqlBuilder, fields string, in ...[]interface{}) (T, error) { +func LastOne[T model](where SqlBuilder, fields string, in ...[]interface{}) (T, error) { var r T w, args := where.parseWhere(in...) tp := "select %s from %s %s order by %s desc limit 1" @@ -199,7 +171,7 @@ func (m model[T]) LastOne(where SqlBuilder, fields string, in ...[]interface{}) return r, nil } -func (m model[T]) SimpleFind(where SqlBuilder, fields string, in ...[]interface{}) ([]T, error) { +func SimpleFind[T model](where SqlBuilder, fields string, in ...[]interface{}) ([]T, error) { var r []T var rr T w, args := where.parseWhere(in...) @@ -212,17 +184,7 @@ func (m model[T]) SimpleFind(where SqlBuilder, fields string, in ...[]interface{ return r, nil } -func (m model[T]) Get(sql string, params ...interface{}) (T, error) { - var r T - sql = strings.Replace(sql, "%table%", r.Table(), -1) - err := db.Db.Get(&r, sql, params...) - if err != nil { - return r, err - } - return r, nil -} - -func (m model[T]) Select(sql string, params ...interface{}) ([]T, error) { +func Select[T model](sql string, params ...interface{}) ([]T, error) { var r []T var rr T sql = strings.Replace(sql, "%table%", rr.Table(), -1) @@ -232,3 +194,23 @@ func (m model[T]) Select(sql string, params ...interface{}) ([]T, error) { } return r, nil } + +func Find[T model](where SqlBuilder, fields string, order SqlBuilder, join SqlBuilder, limit int, in ...[]interface{}) (r []T, err error) { + var rr T + w, args := where.parseWhere(in...) + j := join.parseJoin() + tp := "select %s from %s %s %s %s %s" + l := "" + if limit > 0 { + l = fmt.Sprintf(" limit %d", limit) + } + sql := fmt.Sprintf(tp, fields, rr.Table(), j, w, order.parseOrderBy(), l) + err = db.Db.Select(&r, sql, args...) + return +} + +func Get[T model](sql string, params ...interface{}) (r T, err error) { + sql = strings.Replace(sql, "%table%", r.Table(), -1) + err = db.Db.Get(&r, sql, params...) + return +} diff --git a/models/wp_comments.go b/models/wp_comments.go index 9908058..c276ba5 100644 --- a/models/wp_comments.go +++ b/models/wp_comments.go @@ -2,10 +2,7 @@ package models import "time" -var WpCommentsM = WpComments{} - type WpComments struct { - model[WpComments] CommentId uint64 `gorm:"column:comment_ID" db:"comment_ID" json:"comment_ID" form:"comment_ID"` CommentPostId uint64 `gorm:"column:comment_post_ID" db:"comment_post_ID" json:"comment_post_ID" form:"comment_post_ID"` CommentAuthor string `gorm:"column:comment_author" db:"comment_author" json:"comment_author" form:"comment_author"` diff --git a/models/wp_options.go b/models/wp_options.go index 6387f8f..b3df945 100644 --- a/models/wp_options.go +++ b/models/wp_options.go @@ -1,9 +1,6 @@ package models -var WpOptionsM = WpOptions{} - type WpOptions struct { - model[WpOptions] OptionId uint64 `gorm:"column:option_id" db:"option_id" json:"option_id" form:"option_id"` OptionName string `gorm:"column:option_name" db:"option_name" json:"option_name" form:"option_name"` OptionValue string `gorm:"column:option_value" db:"option_value" json:"option_value" form:"option_value"` diff --git a/models/wp_posts.go b/models/wp_posts.go index 9a8be6c..9fde601 100644 --- a/models/wp_posts.go +++ b/models/wp_posts.go @@ -2,10 +2,7 @@ package models import "time" -var WpPostsM = WpPosts{} - type WpPosts struct { - model[WpPosts] Id uint64 `gorm:"column:ID" db:"ID" json:"ID" form:"ID"` PostAuthor uint64 `gorm:"column:post_author" db:"post_author" json:"post_author" form:"post_author"` PostDate time.Time `gorm:"column:post_date" db:"post_date" json:"post_date" form:"post_date"` diff --git a/models/wp_term_taxonomy.go b/models/wp_term_taxonomy.go index cb349ba..d8c6197 100644 --- a/models/wp_term_taxonomy.go +++ b/models/wp_term_taxonomy.go @@ -1,9 +1,6 @@ package models -var WpTermTaxonomyM = WpTermTaxonomy{} - type WpTermTaxonomy struct { - model[WpTermTaxonomy] TermTaxonomyId uint64 `gorm:"column:term_taxonomy_id" db:"term_taxonomy_id" json:"term_taxonomy_id" form:"term_taxonomy_id"` TermId uint64 `gorm:"column:term_id" db:"term_id" json:"term_id" form:"term_id"` Taxonomy string `gorm:"column:taxonomy" db:"taxonomy" json:"taxonomy" form:"taxonomy"` diff --git a/models/wp_terms.go b/models/wp_terms.go index edfb8a9..63afe0b 100644 --- a/models/wp_terms.go +++ b/models/wp_terms.go @@ -1,9 +1,6 @@ package models -var WpTermsM = WpTerms{} - type WpTerms struct { - model[WpTerms] TermId uint64 `gorm:"column:term_id" db:"term_id" json:"term_id" form:"term_id"` Name string `gorm:"column:name" db:"name" json:"name" form:"name"` Slug string `gorm:"column:slug" db:"slug" json:"slug" form:"slug"` diff --git a/route/actions.go b/route/actions.go new file mode 100644 index 0000000..0f57e1b --- /dev/null +++ b/route/actions.go @@ -0,0 +1,12 @@ +package route + +import ( + "github.com/gin-gonic/gin" + "net/http" +) + +func index(c *gin.Context) { + c.HTML(http.StatusOK, "index.html", gin.H{ + "title": "Main website", + }) +} diff --git a/route/route.go b/route/route.go new file mode 100644 index 0000000..8a08770 --- /dev/null +++ b/route/route.go @@ -0,0 +1,16 @@ +package route + +import ( + "github.com/gin-gonic/gin" +) + +func SetupRouter() *gin.Engine { + // Disable Console Color + // gin.DisableConsoleColor() + r := gin.Default() + r.LoadHTMLGlob("templates/*") + // Ping test + r.GET("/", index) + + return r +} diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..d96f589 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,12 @@ + + + + + {{ .title }} + + +

+ {{ .title }} +

+ + \ No newline at end of file