123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- package main
- import (
- "bytes"
- "fmt"
- "net/http"
- "os"
- "time"
- "git.chuangxin1.com/csacred/toolkit"
- "git.chuangxin1.com/csacred/toolkit/binding"
- khttp "git.chuangxin1.com/csacred/toolkit/http"
- "github.com/chuangxin1/toolkit/types"
- "github.com/go-kit/kit/log"
- )
- // FormID http request from of user
- type FormID struct {
- ID int `json:"id" db:"id" form:"id"`
- }
- // FormUser http request from of user
- type FormUser struct {
- Page int `json:"page" db:"page" form:"page" validate:"required,gt=0"`
- PageSize int `json:"pagesize" db:"pagesize" form:"pagesize" validate:"required,gte=5,lte=100"`
- Code int `json:"code" db:"code" form:"code"`
- Name string `json:"name" db:"name" form:"name" validate:"max=20"`
- ID int `json:"id" db:"id" form:"id"`
- }
- // User user
- type User struct {
- ID int `json:"id"`
- Phone string `json:"phone"`
- Name string `json:"name"`
- Passwd string `json:"passwd"`
- Sex string `json:"sex"`
- Birthday types.DateText `json:"birthday"`
- //*
- Country types.NullInt `json:"country"`
- Province types.NullInt `json:"province"`
- City types.NullInt `json:"city"`
- Nation types.NullInt `json:"nation"`
- // */
- Avatar types.NullString `json:"avatar"`
- Signature types.NullString `json:"signature"`
- Category int `json:"category"`
- Scale int `json:"scale"`
- Score int `json:"score"`
- Balance float32 `json:"balance"`
- Status int `json:"status"`
- Stime string `json:"stime"`
- Attr types.JSONText `json:"attr"`
- }
- func get(u FormID) (user User, err error) {
- var db = toolkit.NewDB()
- //if err = db.Connect(); err == nil {
- //defer db.Close()
- var (
- buffer bytes.Buffer
- where bytes.Buffer
- )
- buffer.WriteString(`select id, phone, name, password passwd, sex, `)
- buffer.WriteString(`birthday, country, province, city, nation, avatar, `)
- buffer.WriteString(`signature, category, balance, scale, score, attr, `)
- buffer.WriteString(`status, sys_time stime from usr_user`)
- where.WriteString(` where id > 0 and id = :id`)
- buffer.WriteString(where.String())
- err = db.Row(&user, buffer.String(), u)
- //}
- return
- }
- func list(formUser FormUser) (users []User, total int, err error) {
- var db = toolkit.NewDB()
- //if err = db.Connect(); err == nil {
- //defer db.Close()
- var (
- buffer bytes.Buffer
- where bytes.Buffer
- sql bytes.Buffer
- )
- buffer.WriteString(`select id, phone, name, password passwd, sex, `)
- buffer.WriteString(`birthday, country, province, city, nation, avatar, `)
- buffer.WriteString(`signature, category, balance, scale, score, attr, `)
- buffer.WriteString(`status, sys_time stime `)
- where.WriteString(` from usr_user where id > 0`)
- if len(formUser.Name) > 0 {
- where.WriteString(` and name like :name`)
- }
- sql.WriteString(`select count(*) total`)
- sql.WriteString(where.String())
- buffer.WriteString(where.String())
- buffer.WriteString(` order by id desc`)
- buffer.WriteString(db.Limit(formUser.Page, formUser.PageSize))
- //fmt.Println(buffer.String(), sql.String())
- err = db.Row(&total, sql.String(), formUser)
- //fmt.Println("total", total)
- if err != nil {
- return
- }
- err = db.Rows(&users, buffer.String(), formUser)
- return
- }
- func main() {
- logger := log.NewLogfmtLogger(os.Stderr)
- readTimeout := 30 * time.Second
- writeTimeout := 30 * time.Second
- maxHeaderBytes := 1 << 20
- router := khttp.NewRouter()
- khttp.AddGetHandle(router, "/ver", func(w http.ResponseWriter, r *http.Request) {
- ver := map[string]string{
- "version": "0.2.0",
- "comments": "chuangxin1.com API",
- "author": "ls"}
- khttp.WriteJSON(w, toolkit.RowReplyData(ver))
- })
- khttp.AddGetHandle(router, "/users", func(w http.ResponseWriter, r *http.Request) {
- var user FormUser
- user.Page = 1
- user.PageSize = 20
- users, total, err := list(user)
- if err != nil {
- khttp.WriteJSON(w, toolkit.ErrReplyData(toolkit.ErrDataNotFound, err.Error()))
- return
- }
- khttp.WriteJSON(w, toolkit.RowsReplyData(total, users))
- })
- khttp.AddGetHandle(router, "/profile", func(w http.ResponseWriter, r *http.Request) {
- var user FormID
- binding.Bind(r, &user)
- users, err := get(user)
- if err != nil {
- khttp.WriteJSON(w, toolkit.ErrReplyData(toolkit.ErrDataNotFound, err.Error()))
- return
- }
- khttp.WriteJSON(w, toolkit.RowReplyData(users))
- })
- dns := fmt.Sprintf(
- "root:Lsar-5211@tcp(127.0.0.1:3306)/community_user?charset=utf8mb4")
- dbConfig := toolkit.DbConfig{
- Driver: `mysql`,
- DNS: dns,
- MaxOpenConns: 50,
- MaxIdle: 5,
- MaxLifetime: 0}
- toolkit.SetDbConfig(dbConfig)
- defer toolkit.FreeDB()
- //serviceRouter(router, logger)
- //hs := make(HostSwitch)
- //hs["192.168.1.8:9122"] = router
- //toolkit.StartServer(addr, hs, readTimeout, writeTimeout, maxHeaderBytes, logger)
- khttp.ListenAndServe(
- ":9006",
- router,
- readTimeout,
- writeTimeout,
- maxHeaderBytes,
- logger)
- }
|