func main() { //SETUP cfg.Setup("cfg.json") util.SetupDB() util.SetupKeebler() g := gin.Default() // ROUTES g.GET("/", v1.GetApp) g.GET("/index", v1.GetIndex) g.GET("/logout", v1.GetLogout) g.POST("/login", v1.PostLogin) g.POST("/signup", v1.PostSignup) sp := g.Group("/spec") { // sp.DELETE("/chirp/:id", spec.DeleteChirp) // sp.GET("/chirp/:id", spec.GetChirpById) // sp.GET("/chirps", spec.GetChirps) sp.GET("/user", spec.GetUser) // sp.GET("/chirps-search/:term", spec.GetChirpsSearch) // sp.PATCH("/chirp/:id", spec.PatchChirp) // sp.POST("/chirp", spec.PostChirp) } api := g.Group("/api") api.Use(RequireAuth()) { api.DELETE("/chirp/:id", v1.DeleteChirp) api.GET("/chirp/:id", v1.GetChirpById) api.GET("/chirps", v1.GetChirps) api.GET("/chirps-by-user/:id", v1.GetChirpsByUser) api.GET("/chirps-search/:term", v1.GetChirpsSearch) api.GET("/user", v1.GetUser) api.GET("/user/:id", v1.GetUserById) api.PATCH("/chirp", v1.PatchChirp) api.PATCH("/user", v1.PatchUser) api.POST("/chirp", v1.PostChirp) } // SERVICES go http.ListenAndServe(cfg.HTTP_PORT, http.HandlerFunc(redirHttps)) log.Printf("... started " + cfg.SERVER + " redirects to Https ...") g.ServeFiles("/webapp/*filepath", http.Dir("webapp")) g.ServeFiles("/html/*filepath", http.Dir("webapp/html")) log.Printf("... started ServeFile ...") log.Printf("... serving %v TLS on port %v ", cfg.APPNAME, cfg.HTTP_PORT) g.RunTLS(cfg.HTTP_PORT, "certs/cert.pem", "certs/key.pem") }
func Test_User(t *testing.T) { cfg.Setup("../../../../cfg.json") util.SetupDB() var a model.User var b model.User var c model.User var token = util.GenHash(6) a.Accountid = 1 a.Name = token a.Email = token + "@user.com" a.HashPassword("123") log.Println("a is : ", a) // Test StmtInsert row := util.DB.QueryRow(a.StmtInsert()) err := row.Scan(&b.Id) log.Println("b is : ", b) if err != nil { t.Error("FAILED QueryRow : ", b) } // Test StmtGetById row = util.DB.QueryRow(b.StmtGetById()) err = row.Scan(&b.Id, &b.Accountid, &b.Name, &b.Email, &b.Password, &b.Active) if err != nil { t.Error("FAILED Prepare Statement : ", err) } if a.Name != b.Name { t.Error("FAILED not same Name : ", a.Name) } if a.Password != b.Password { t.Error("FAILED not same Password : "******"FAILED not same Email : ", a.Email) } // Test StmtUpdate b.Email = "updated" + token + "@email.com" b.Name = "updated" log.Println("b is now to be : ", b) _, err = util.DB.Exec(b.StmtUpdate()) log.Println("Update query : ", b.StmtUpdate()) log.Println("This, the error : ", err) c.Id = b.Id log.Println("Using this id: ", c.Id, b.Id) err = util.DB.QueryRow(c.StmtGetById()).Scan(&c.Id, &c.Accountid, &c.Name, &c.Email, &c.Password, &c.Active) if err != nil { t.Error("FAILED scan row : ", err) } log.Println("C now looks like : ", c) if c.Name != "updated" { t.Error("FAILED update Name : ", c.Name) } if c.Email != "updated"+token+"@email.com" { t.Error("FAILED update Email : ", c.Email) } // Test StmtSetActive _, err = util.DB.Exec(b.StmtSetActive(false)) if err != nil { t.Error("FAILED StmtSetActive to false : ", err) } err = util.DB.QueryRow(c.StmtGetById()).Scan(&c.Id, &c.Accountid, &c.Name, &c.Email, &c.Password, &c.Active) if err != nil { t.Error("FAILED scan row : ", err) } if c.Active != false { t.Error("FAILED updated SetActive(false) : ", c.Active) } // Test StmtSelect rows, err := util.DB.Query(a.StmtSelect("")) if err != nil { t.Error("FAILED StmtSelect : ", err) } for rows.Next() { rows.Scan(&b.Id, &b.Accountid, &b.Name, &b.Email, &b.Password, &b.Active) log.Println("This is a row : ", b) } // Test StmtSelectActive rows, err = util.DB.Query(a.StmtSelectActive()) if err != nil { t.Error("FAILED StmtSelectActive : ", err) } for rows.Next() { rows.Scan(&b.Id, &b.Accountid, &b.Name, &b.Email, &b.Password, &b.Active) log.Println("This is an active row : ", b) } // Test StmtSelectActive rows, err = util.DB.Query(a.StmtSelectByAccountId()) if err != nil { t.Error("FAILED StmtSelectByAccountId : ", err) } for rows.Next() { rows.Scan(&b.Id, &b.Accountid, &b.Name, &b.Email, &b.Password, &b.Active) log.Println("This is has for this userid row : ", b) } // Test StmtCountActive var cnt int err = util.DB.QueryRow(b.StmtCountActive(true)).Scan(&cnt) if err != nil { t.Error("FAILED StmtCountActive", err) } if cnt < 1 { t.Error("FAILED found no active Users : ", cnt) } }
func Test_Chirp(t *testing.T) { cfg.Setup("../../../../cfg.json") util.SetupDB() var a model.Chirp var b model.Chirp var c model.Chirp var token = util.GenHash(6) //var id int = 110 a.Userid = 1 a.Type = token a.Message = token + "-message" log.Println(a) // Test StmtInsert row := util.DB.QueryRow(a.StmtInsert()) err := row.Scan(&b.Id) log.Println(b) if err != nil { t.Error("FAILED QueryRow : ", b) } // Test StmtGetById row = util.DB.QueryRow(b.StmtGetById()) err = row.Scan(&b.Id, &b.Userid, &b.Type, &b.Message, &b.Create_date, &b.Active) if err != nil { t.Error("FAILED Prepare Statement : ", err) } if a.Userid != b.Userid { t.Error("FAILED not same User : "******"FAILED not same Type : ", a.Type) } if a.Message != b.Message { t.Error("FAILED not same Message : ", a.Message) } // Test StmtUpdate b.Message = "updated-msg" b.Type = "updated" log.Println("b is now updated : ", b) _, err = util.DB.Exec(b.StmtUpdate()) log.Println("Update query : ", b.StmtUpdate()) log.Println("This, the error : ", err) c.Id = b.Id log.Println("Using this id: ", c.Id, b.Id) err = util.DB.QueryRow(c.StmtGetById()).Scan(&c.Id, &c.Userid, &c.Type, &c.Message, &c.Create_date, &c.Active) if err != nil { t.Error("FAILED scan row : ", err) } log.Println("C now looks like : ", c) if c.Type != "updated" { t.Error("FAILED update Name : ", c.Type) } if c.Message != "updated-msg" { t.Error("FAILED update Message : ", c.Message) } // Test StmtSetActive _, err = util.DB.Exec(c.StmtSetActive(false)) if err != nil { t.Error("FAILED StmtSetActive to false : ", err) } err = util.DB.QueryRow(c.StmtGetById()).Scan(&c.Id, &c.Userid, &c.Type, &c.Message, &c.Create_date, &c.Active) if err != nil { t.Error("FAILED scan row : ", err) } if c.Active != false { t.Error("FAILED updated SetActive(false) : ", c.Active) } // Test StmtSetActive _, err = util.DB.Exec(c.StmtSetActive(true)) if err != nil { t.Error("FAILED StmtSetActive to false : ", err) } // Test StmtSelect rows, err := util.DB.Query(b.StmtSelect("")) if err != nil { t.Error("FAILED StmtSelect : ", err) } for rows.Next() { rows.Scan(&b.Id, &b.Userid, &b.Type, &b.Message, &b.Create_date, &b.Active) log.Println("This is a unit row : ", b) } // Test StmtSelectActive rows, err = util.DB.Query(a.StmtSelectActive()) if err != nil { t.Error("FAILED StmtSelectActive : ", err) } for rows.Next() { rows.Scan(&b.Id, &b.Userid, &b.Type, &b.Message, &b.Create_date, &b.Active) log.Println("This is an active row : ", b) } // Test StmtSelectActive rows, err = util.DB.Query(a.StmtSelectByUserId()) if err != nil { t.Error("FAILED StmtSelectByUserId : ", err) } for rows.Next() { rows.Scan(&b.Id, &b.Userid, &b.Type, &b.Message, &b.Create_date, &b.Active) log.Println("This is has for unit row : ", b) } // Test StmtCountActive var cnt int err = util.DB.QueryRow(c.StmtCountActive(true)).Scan(&cnt) if err != nil { t.Error("FAILED StmtCountActive", err) } if cnt < 1 { t.Error("FAILED found no active Units : ", cnt) } }