From ed595b2cdaa2155185b9d152529e71dd4e3e9beb Mon Sep 17 00:00:00 2001 From: rtviwe Date: Sat, 6 Apr 2019 14:05:46 +0300 Subject: [PATCH 1/4] db --- .gitignore | 4 +++- main.go | 11 ++++++++++- storage/database.go | 2 -- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 50959cf..2524843 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ Будет удалено .idea/ Будет удалено vendor/ /vendor/ -.idea \ No newline at end of file +.idea +/*.db +Будет удалено test.db diff --git a/main.go b/main.go index e95119c..9265b8d 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "HotKeysBackend/utils" "fmt" "github.com/gin-gonic/gin" + _ "github.com/jinzhu/gorm/dialects/sqlite" ) var ( @@ -17,7 +18,15 @@ var ( ) func main() { - // TODO uncomment when database appears + //db, err := gorm.Open("sqlite3", "test.db") + //if err != nil { + // panic("failed to connect database") + //} + //defer db.Close() + // + //db.Create(&program.Program{}) + + // TODO //programStorage := storage.GetProgramDatabaseRepository() //keyStorage := storage.GetKeyDatabaseRepository() diff --git a/storage/database.go b/storage/database.go index c137db5..574b454 100644 --- a/storage/database.go +++ b/storage/database.go @@ -13,13 +13,11 @@ const ( func GetProgramDatabaseRepository() program.Repository { // TODO - //database := getDatabase(databasePath) return nil } func GetKeyDatabaseRepository() key.Repository { // TODO - //database := getDatabase(databasePath) return nil } From ac2c00dc1647a564e09209f67ba3e233380d0abb Mon Sep 17 00:00:00 2001 From: rtviwe Date: Sat, 6 Apr 2019 14:09:32 +0300 Subject: [PATCH 2/4] session --- main.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/main.go b/main.go index 9265b8d..18265c7 100644 --- a/main.go +++ b/main.go @@ -25,8 +25,6 @@ func main() { //defer db.Close() // //db.Create(&program.Program{}) - - // TODO //programStorage := storage.GetProgramDatabaseRepository() //keyStorage := storage.GetKeyDatabaseRepository() From f715e99f414eaf258742d5f9d60c5246737162b0 Mon Sep 17 00:00:00 2001 From: rtviwe Date: Sat, 6 Apr 2019 14:14:40 +0300 Subject: [PATCH 3/4] cors --- main.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/main.go b/main.go index 18265c7..ab4f7ea 100644 --- a/main.go +++ b/main.go @@ -6,8 +6,10 @@ import ( "HotKeysBackend/storage" "HotKeysBackend/utils" "fmt" + "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" _ "github.com/jinzhu/gorm/dialects/sqlite" + "time" ) var ( @@ -34,6 +36,17 @@ func main() { checker := checking.CreateService(programStorage, keyStorage) router := gin.Default() + router.Use(cors.New(cors.Config{ + AllowOrigins: []string{"https://foo.com"}, + AllowMethods: []string{"POST", "GET", "PUT"}, + AllowHeaders: []string{"Origin"}, + ExposeHeaders: []string{"Content-Length"}, + AllowCredentials: true, + AllowOriginFunc: func(origin string) bool { + return origin == "https://github.com" + }, + MaxAge: 4 * time.Hour, + })) router.GET(handleGetPrograms, func(context *gin.Context) { getting.HandleGetPrograms(context, getter) From 06954804b790ba7b8248a3f661f2ee3c96123b43 Mon Sep 17 00:00:00 2001 From: rtviwe Date: Thu, 18 Apr 2019 19:59:17 +0300 Subject: [PATCH 4/4] temp before my ubuntu: --- checking/endpoint.go | 55 +++++++++++++++++++++++++++++--------------- checking/service.go | 6 ++--- hotkey/hotkey.go | 2 ++ main.go | 30 +++++++++++------------- utils/error.go | 2 ++ 5 files changed, 57 insertions(+), 38 deletions(-) diff --git a/checking/endpoint.go b/checking/endpoint.go index 31cb720..a9ee265 100644 --- a/checking/endpoint.go +++ b/checking/endpoint.go @@ -1,43 +1,60 @@ package checking import ( - "HotKeysBackend/key" + "HotKeysBackend/hotkey" "HotKeysBackend/utils" "github.com/gin-gonic/gin" + "net/http" "strconv" ) -type CheckHotkeyRequest struct { - ProgramId uint `json:"programId"` - HotkeyId uint `json:"hotkeyId"` - Combination []*key.Key `json:"combination"` -} - func HandleCheckHotkey(context *gin.Context, checker Service) { - paramProgramId, err := strconv.ParseUint(context.Param(utils.ProgramId), 10, 64) + programId := getProgramId(context) + hotkeyId := getHotkeyId(context) + combination := make([]uint, hotkey.MAX_LENGTH) + + if context.Request.Body == nil { + utils.SendError(context, nil) + panic(utils.ErrorWrongBodyFormat) + } + + buf := make([]byte, 1024) + num, err := context.Request.Body.Read(buf) if err != nil { utils.SendError(context, err) - panic(utils.ErrorWrongProgramIdFormat) + panic(utils.ErrorWrongBodyFormat) } - programId := uint(paramProgramId) - paramHotkeyId, err := strconv.ParseUint(context.Param(utils.HotkeyId), 10, 64) + reqBody := string(buf[0:num]) + context.JSON(http.StatusOK, reqBody) + + right, err := checker.Check(programId, hotkeyId, combination) if err != nil { utils.SendError(context, err) - panic(utils.ErrorWrongHotkeyIdFormat) + panic(utils.ErrorChecking) } - hotkeyId := uint(paramHotkeyId) - checkHotkeyRequest := &CheckHotkeyRequest{ - ProgramId: programId, - HotkeyId: hotkeyId, - //Combination: + if right { + context.JSON(http.StatusOK, gin.H{"answer": true}) + } else { + context.JSON(http.StatusOK, gin.H{"answer": false}) } +} - err = context.BindJSON(checkHotkeyRequest) +func getProgramId(context *gin.Context) uint { + paramProgramId, err := strconv.ParseUint(context.Param(utils.ProgramId), 10, 64) if err != nil { utils.SendError(context, err) - panic(utils.ErrorWrongBodyFormat) + panic(utils.ErrorWrongProgramIdFormat) } + return uint(paramProgramId) +} +func getHotkeyId(context *gin.Context) uint { + paramHotkeyId, err := strconv.ParseUint(context.Param(utils.HotkeyId), 10, 64) + if err != nil { + utils.SendError(context, err) + panic(utils.ErrorWrongHotkeyIdFormat) + } + return uint(paramHotkeyId) } diff --git a/checking/service.go b/checking/service.go index 7bf31d2..60a0aa0 100644 --- a/checking/service.go +++ b/checking/service.go @@ -7,7 +7,7 @@ import ( ) type Service interface { - Check(projectId uint, hotkeyId uint, combination []*key.Key) (bool, error) + Check(projectId uint, hotkeyId uint, combination []uint) (bool, error) } type service struct { @@ -23,7 +23,7 @@ func CreateService(programRepository program.Repository, keyRepository key.Repos } // TODO -func (s *service) Check(projectId uint, hotkeyId uint, combination []*key.Key) (bool, error) { +func (s *service) Check(projectId uint, hotkeyId uint, combination []uint) (bool, error) { currentHotkeys, err := s.programRepository.GetHotkeys(projectId) if err != nil { return false, utils.ErrorGetProgram @@ -32,7 +32,7 @@ func (s *service) Check(projectId uint, hotkeyId uint, combination []*key.Key) ( for _, element := range *currentHotkeys { isFound := true for i, currentCombination := range *element.Combination { - if combination[i].ID != currentCombination.ID { + if combination[i] != currentCombination.ID { isFound = false } } diff --git a/hotkey/hotkey.go b/hotkey/hotkey.go index 3aeb5fe..6620118 100644 --- a/hotkey/hotkey.go +++ b/hotkey/hotkey.go @@ -10,3 +10,5 @@ type Hotkey struct { Description string `json:"desc"` Combination *[]key.Key `json:"combination"` } + +const MAX_LENGTH = 7 \ No newline at end of file diff --git a/main.go b/main.go index ab4f7ea..daed733 100644 --- a/main.go +++ b/main.go @@ -6,17 +6,15 @@ import ( "HotKeysBackend/storage" "HotKeysBackend/utils" "fmt" - "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" _ "github.com/jinzhu/gorm/dialects/sqlite" - "time" ) var ( handleGetPrograms = "/programs/" - handleGetProgram = fmt.Sprintf("/programs/:%s", utils.ProgramId) - handleGetHotkeys = fmt.Sprintf("/programs/:%s/hotkeys", utils.ProgramId) - handleCheckHotkey = fmt.Sprintf("/programs/:%s/hotkeys/:%s/check", utils.ProgramId, utils.HotkeyId) + handleGetProgram = fmt.Sprintf("/program/:%s", utils.ProgramId) + handleGetHotkeys = fmt.Sprintf("/program/:%s/hotkeys", utils.ProgramId) + handleCheckHotkey = fmt.Sprintf("/program/:%s/hotkeys/:%s/check", utils.ProgramId, utils.HotkeyId) ) func main() { @@ -36,17 +34,17 @@ func main() { checker := checking.CreateService(programStorage, keyStorage) router := gin.Default() - router.Use(cors.New(cors.Config{ - AllowOrigins: []string{"https://foo.com"}, - AllowMethods: []string{"POST", "GET", "PUT"}, - AllowHeaders: []string{"Origin"}, - ExposeHeaders: []string{"Content-Length"}, - AllowCredentials: true, - AllowOriginFunc: func(origin string) bool { - return origin == "https://github.com" - }, - MaxAge: 4 * time.Hour, - })) + //router.Use(cors.New(cors.Config{ + // AllowOrigins: []string{"https://foo.com"}, + // AllowMethods: []string{"POST", "GET", "PUT"}, + // AllowHeaders: []string{"Origin"}, + // ExposeHeaders: []string{"Content-Length"}, + // AllowCredentials: true, + // AllowOriginFunc: func(origin string) bool { + // return origin == "https://github.com" + // }, + // MaxAge: 4 * time.Hour, + //})) router.GET(handleGetPrograms, func(context *gin.Context) { getting.HandleGetPrograms(context, getter) diff --git a/utils/error.go b/utils/error.go index 5d0d3b1..563b958 100644 --- a/utils/error.go +++ b/utils/error.go @@ -17,6 +17,8 @@ var ErrorWrongHotkeyIdFormat = errors.New("hotkey id wrong format") var ErrorKeyNotFound = errors.New("key not found") +var ErrorChecking = errors.New("error while checking") + func SendError(context *gin.Context, err error) { context.JSON(http.StatusInternalServerError, gin.H{ // TODO is it safe?