确定服务结构

This commit is contained in:
suguo.yao 2020-03-30 13:46:11 +08:00
parent e3cd9e3c85
commit 9ec72a1980
5 changed files with 83 additions and 7 deletions

View File

@ -2,4 +2,4 @@ build:
go build . go build .
dev: dev:
go run . go run . -d "root:root@tcp(i.myschools.me:6006)/rubbish?charset=utf8mb4&parseTime=True&loc=Local"

View File

@ -1,6 +1,8 @@
package community package community
import ( import (
"sync"
"github.com/jinzhu/gorm" "github.com/jinzhu/gorm"
"yyjishu.com/rubbish-class/rubbish" "yyjishu.com/rubbish-class/rubbish"
) )
@ -10,17 +12,39 @@ type CommunityModel struct {
Name string `gorm:"type:varchar(20);"` Name string `gorm:"type:varchar(20);"`
} }
var once sync.Once
type Community struct { type Community struct {
} }
func (c *Community) Map() { func NewService() *Community {
c := &Community{}
once.Do(c.Init)
return c
} }
//Map 社区KV获取
func (c *Community) Map() (*map[string]uint, error) {
var cs []CommunityModel
if err := rubbish.DB.Find(&cs).Error; err != nil {
return nil, err
}
data := make(map[string]uint)
for _, c := range cs {
data[c.Name] = c.ID
}
return &data, nil
}
//Create 创建社区信息
func (c *Community) Create(model *CommunityModel) error { func (c *Community) Create(model *CommunityModel) error {
if err := rubbish.DB.Create(model).Error; err != nil {
return err
}
return nil return nil
} }
//Init 社区表初始化
func (c *Community) Init() { func (c *Community) Init() {
rubbish.DB.AutoMigrate(&CommunityModel{}) rubbish.DB.AutoMigrate(&CommunityModel{})
} }

View File

@ -1,7 +1,39 @@
package public package public
import "github.com/gin-gonic/gin" import (
"net/http"
"github.com/gin-gonic/gin"
"yyjishu.com/rubbish-class/community"
)
//CommunityListHandler 社区列表
func CommunityListHandler(c *gin.Context) { func CommunityListHandler(c *gin.Context) {
service := community.NewService()
data, err := service.Map()
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
"err": err.Error(),
})
return
}
c.JSON(http.StatusOK, data)
}
//CommunityCreateHandler 社区创建
func CommunityCreateHandler(c *gin.Context) {
var model community.CommunityModel
if err := c.ShouldBindJSON(&model); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, nil)
return
}
service := community.NewService()
err := service.Create(&model)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
"err": err.Error(),
})
return
}
c.JSON(http.StatusOK, &model)
} }

View File

@ -10,6 +10,7 @@ import (
"yyjishu.com/rubbish-class/admin" "yyjishu.com/rubbish-class/admin"
"yyjishu.com/rubbish-class/app" "yyjishu.com/rubbish-class/app"
"yyjishu.com/rubbish-class/public" "yyjishu.com/rubbish-class/public"
"yyjishu.com/rubbish-class/rubbish"
) )
var ( var (
@ -17,8 +18,11 @@ var (
) )
func main() { func main() {
rubbish.Addr = flag.String("d", "root:root@tcp(127.0.0.1:3306)/rubbish?charset=utf8mb4\u0026parseTime=True\u0026loc=Local", "address for mysql")
flag.Parse() flag.Parse()
rubbish.Init()
//启动时检查与创建文件夹video用于存放用户上传的视频文件 //启动时检查与创建文件夹video用于存放用户上传的视频文件
_, err := os.Stat("data") _, err := os.Stat("data")
if err != nil { if err != nil {
@ -34,7 +38,8 @@ func main() {
rbGroup := r.Group(`/rubbish`) rbGroup := r.Group(`/rubbish`)
{ {
rbGroup.GET(`/community/list`, public.CommunityListHandler) //社区列表 rbGroup.GET(`/community/list`, public.CommunityListHandler) //社区列表,OK
rbGroup.POST(`/community/create`, public.CommunityCreateHandler) //社区列表,OK
rbGroup.StaticFS(`/video`, http.Dir("./data")) rbGroup.StaticFS(`/video`, http.Dir("./data"))
authorized := rbGroup.Group(`/admin`, gin.BasicAuth(gin.Accounts{ authorized := rbGroup.Group(`/admin`, gin.BasicAuth(gin.Accounts{

View File

@ -1,4 +1,4 @@
@url=http://localhost:8080 @url=http://localhost:8080/rubbish
//@url=https://api.xintijiao.com //@url=https://api.xintijiao.com
@ -6,6 +6,21 @@
@refresh_token=31_T-96lys-lZtR4B9OtRMkFOqyX_zxJNwMH6vcKxenk7enK5jeDZ6Av-Ipj9aVWr1dzdsMaCACoxbmEFKmReHwTgtQNrOpY3TdiTkM5pEyvIQ @refresh_token=31_T-96lys-lZtR4B9OtRMkFOqyX_zxJNwMH6vcKxenk7enK5jeDZ6Av-Ipj9aVWr1dzdsMaCACoxbmEFKmReHwTgtQNrOpY3TdiTkM5pEyvIQ
@openid=oYmRQxLw6UKdlQsZYIkRKbWlCijI @openid=oYmRQxLw6UKdlQsZYIkRKbWlCijI
###############################公共部分###############################################
POST {{url}}/community/create HTTP/1.1
Content-Type: application/json
{
"name":"aaa小区"
}
###
GET {{url}}/community/list HTTP/1.1
############################### 以下微信小程序 ######################################## ############################### 以下微信小程序 ########################################
@jscode={{$guid}} @jscode={{$guid}}