fix
This commit is contained in:
		
							parent
							
								
									880a32a9c8
								
							
						
					
					
						commit
						a45b1f3603
					
				
							
								
								
									
										16
									
								
								models/album.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								models/album.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,16 @@
 | 
			
		||||
package models
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Album model
 | 
			
		||||
type Album struct {
 | 
			
		||||
	ID     string    `xorm:"id"`
 | 
			
		||||
	UID    string    `xorm:"uid"`
 | 
			
		||||
	Name   string    `xorm:"name"`
 | 
			
		||||
	Public bool      `xorm:"public default false"`
 | 
			
		||||
	Ctime  time.Time `xorm:"ctime created"`
 | 
			
		||||
	Mtime  time.Time `xorm:"mtime updated"`
 | 
			
		||||
	Photos []*Photo  `xorm:"-"`
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										13
									
								
								models/photo.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								models/photo.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,13 @@
 | 
			
		||||
package models
 | 
			
		||||
 | 
			
		||||
import "time"
 | 
			
		||||
 | 
			
		||||
// Photo model
 | 
			
		||||
type Photo struct {
 | 
			
		||||
	ID        string    `xorm:"id"`
 | 
			
		||||
	Album     string    `xorm:"album"`
 | 
			
		||||
	Name      string    `xorm:"name"`
 | 
			
		||||
	Thumbnail string    `xorm:"thumbnail"`
 | 
			
		||||
	Ctime     time.Time `xorm:"ctime"`
 | 
			
		||||
	Mtime     time.Time `xorm:"mtime"`
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										85
									
								
								modules/apimsg/apimsg.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										85
									
								
								modules/apimsg/apimsg.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,85 @@
 | 
			
		||||
package apimsg
 | 
			
		||||
 | 
			
		||||
// ResObject error object
 | 
			
		||||
type ResObject struct {
 | 
			
		||||
	Status int
 | 
			
		||||
	Obj    interface{}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var objs map[string]*ResObject
 | 
			
		||||
 | 
			
		||||
// InitError initial error objs
 | 
			
		||||
func InitError() {
 | 
			
		||||
	objs = make(map[string]*ResObject)
 | 
			
		||||
 | 
			
		||||
	objs["InternalError"] = &ResObject{
 | 
			
		||||
		Status: 500,
 | 
			
		||||
		Obj: map[string]string{
 | 
			
		||||
			"message": "Server Internal Error",
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
	objs["DataFormat"] = &ResObject{
 | 
			
		||||
		Status: 400,
 | 
			
		||||
		Obj: map[string]string{
 | 
			
		||||
			"message": "Data Format Error",
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
	objs["Forbidden"] = &ResObject{
 | 
			
		||||
		Status: 403,
 | 
			
		||||
		Obj: map[string]string{
 | 
			
		||||
			"message": "Forbidden",
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
	objs["Success"] = &ResObject{
 | 
			
		||||
		Status: 200,
 | 
			
		||||
		Obj: map[string]string{
 | 
			
		||||
			"message": "Success",
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
	objs["LoginFirst"] = &ResObject{
 | 
			
		||||
		Status: 401,
 | 
			
		||||
		Obj: map[string]string{
 | 
			
		||||
			"message": "please login first",
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
	objs["TokenTimeout"] = &ResObject{
 | 
			
		||||
		Status: 401,
 | 
			
		||||
		Obj: map[string]string{
 | 
			
		||||
			"message": "token invaildated",
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
	objs["NotFound"] = &ResObject{
 | 
			
		||||
		Status: 404,
 | 
			
		||||
		Obj: map[string]string{
 | 
			
		||||
			"message": "Not Found",
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetRes get error obj
 | 
			
		||||
func GetRes(name string, msg interface{}) *ResObject {
 | 
			
		||||
	if objs == nil {
 | 
			
		||||
		InitError()
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	obj, ok := objs[name]
 | 
			
		||||
	if !ok {
 | 
			
		||||
		obj = objs["InternalError"]
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	resobj := &ResObject{}
 | 
			
		||||
	resobj.Status = obj.Status
 | 
			
		||||
	switch msg.(type) {
 | 
			
		||||
	case string:
 | 
			
		||||
		tmp := make(map[string]string)
 | 
			
		||||
		tmp["message"] = msg.(string)
 | 
			
		||||
		resobj.Obj = tmp
 | 
			
		||||
		break
 | 
			
		||||
	case map[string]interface{}:
 | 
			
		||||
		resobj.Obj = msg
 | 
			
		||||
		break
 | 
			
		||||
	default:
 | 
			
		||||
		resobj.Obj = obj.Obj
 | 
			
		||||
	}
 | 
			
		||||
	return resobj
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										35
									
								
								modules/context/context.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								modules/context/context.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,35 @@
 | 
			
		||||
package context
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"git.trj.tw/golang/go-gallery/modules/apimsg"
 | 
			
		||||
	"github.com/gin-gonic/gin"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Context custom patch context
 | 
			
		||||
type Context struct {
 | 
			
		||||
	*gin.Context
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type CustomMiddle func(*Context)
 | 
			
		||||
 | 
			
		||||
func PatchContext(handler CustomMiddle) gin.HandlerFunc {
 | 
			
		||||
	return func(c *gin.Context) {
 | 
			
		||||
		ctx := &Context{
 | 
			
		||||
			Context: c,
 | 
			
		||||
		}
 | 
			
		||||
		handler(ctx)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// NotFound response
 | 
			
		||||
func (c *Context) NotFound(msg string) {
 | 
			
		||||
	obj := apimsg.GetRes("NotFound", msg)
 | 
			
		||||
	// c.JSON(obj.Status, obj.Obj)
 | 
			
		||||
	c.AbortWithStatusJSON(obj.Status, obj.Obj)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Success response
 | 
			
		||||
func (c *Context) Success(msg interface{}) {
 | 
			
		||||
	obj := apimsg.GetRes("Success", msg)
 | 
			
		||||
	c.JSON(obj.Status, obj.Obj)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										23
									
								
								routers/account/account.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								routers/account/account.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,23 @@
 | 
			
		||||
package account
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"git.trj.tw/golang/go-gallery/modules/context"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// UserLogin route
 | 
			
		||||
func UserLogin(c *context.Context) {
 | 
			
		||||
	loginArg := struct {
 | 
			
		||||
		Account  string `form:"account" json:"account" binding:"required"`
 | 
			
		||||
		Password string `form:"password" json:"password" binding:"required"`
 | 
			
		||||
	}{
 | 
			
		||||
		Account:  "",
 | 
			
		||||
		Password: "",
 | 
			
		||||
	}
 | 
			
		||||
	err := c.ShouldBind(&loginArg)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		c.NotFound("body not found")
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	c.Success("api success")
 | 
			
		||||
}
 | 
			
		||||
@ -1,15 +1,17 @@
 | 
			
		||||
package routes
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
 | 
			
		||||
	"git.trj.tw/golang/go-gallery/models"
 | 
			
		||||
	"git.trj.tw/golang/go-gallery/modules/context"
 | 
			
		||||
	"git.trj.tw/golang/go-gallery/routers/account"
 | 
			
		||||
	"github.com/gin-gonic/gin"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// NewServ - get new service
 | 
			
		||||
func NewServ() *gin.Engine {
 | 
			
		||||
	r := gin.Default()
 | 
			
		||||
	r := gin.New()
 | 
			
		||||
	r.Use(gin.Logger())
 | 
			
		||||
	r.Use(gin.Recovery())
 | 
			
		||||
	return r
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -23,7 +25,6 @@ func SetDefaultRoutes(r *gin.Engine) {
 | 
			
		||||
			})
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		fmt.Println(accs)
 | 
			
		||||
		c.JSON(200, accs)
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
@ -35,11 +36,9 @@ func SetDefaultRoutes(r *gin.Engine) {
 | 
			
		||||
				"message": "Success",
 | 
			
		||||
			})
 | 
			
		||||
		})
 | 
			
		||||
		api.GET("s/", func(c *gin.Context) {
 | 
			
		||||
			c.JSON(200, gin.H{
 | 
			
		||||
				"status":  200,
 | 
			
		||||
				"message": "api sssss",
 | 
			
		||||
			})
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
	accountAPI := api.Group("/account")
 | 
			
		||||
	{
 | 
			
		||||
		accountAPI.POST("/login", context.PatchContext(account.UserLogin))
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user