diff --git a/getway/main.go b/getway/main.go index 7589492..1339880 100644 --- a/getway/main.go +++ b/getway/main.go @@ -2,7 +2,6 @@ package main import ( "crypto/rand" - "database/sql" "encoding/base64" "log" "net/http" @@ -11,14 +10,15 @@ import ( "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" - "github.com/go-sql-driver/mysql" "github.com/golang-jwt/jwt" - "github.com/google/go-github/v32/github" + "github.com/google/go-github/v50/github" "golang.org/x/oauth2" - "golang.org/x/oauth2/github" + ogithub "golang.org/x/oauth2/github" + "gorm.io/driver/mysql" + "gorm.io/gorm" ) -var dbConn *sql.DB +var db *gorm.DB var jwtSecret = []byte(os.Getenv("JWT_SECRET")) func main() { @@ -41,21 +41,21 @@ func main() { func githubLoginHandler(c *gin.Context) { state := generateState() - code := generateCode() + // code := generateCode() - err := storeStateToDB(state, code) - if err != nil { - log.Println("Error storing state to DB:", err) - c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"message": "Internal server error"}) - return - } + // err := storeStateToDB(state, code) + // if err != nil { + // log.Println("Error storing state to DB:", err) + // c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"message": "Internal server error"}) + // return + // } oauthConfig := &oauth2.Config{ ClientID: os.Getenv("GITHUB_CLIENT_ID"), ClientSecret: os.Getenv("GITHUB_CLIENT_SECRET"), RedirectURL: "http://localhost:8000/auth/github/callback", Scopes: []string{"user:email"}, - Endpoint: github.Endpoint, + Endpoint: ogithub.Endpoint, } url := oauthConfig.AuthCodeURL(state) @@ -65,18 +65,19 @@ func githubLoginHandler(c *gin.Context) { func githubCallbackHandler(c *gin.Context) { state := c.Query("state") code := c.Query("code") + log.Println(state, code) - if !verifyState(state, code) { - c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"message": "Invalid state"}) - return - } + // if !verifyState(state, code) { + // c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"message": "Invalid state"}) + // return + // } oauthConfig := &oauth2.Config{ ClientID: os.Getenv("GITHUB_CLIENT_ID"), ClientSecret: os.Getenv("GITHUB_CLIENT_SECRET"), RedirectURL: "http://localhost:8000/auth/github/callback", Scopes: []string{"user:email"}, - Endpoint: github.Endpoint, + Endpoint: ogithub.Endpoint, } token, err := oauthConfig.Exchange(c.Request.Context(), code) @@ -101,7 +102,7 @@ func githubCallbackHandler(c *gin.Context) { return } - jwtToken, err := generateJWTToken(user.ID) + jwtToken, err := generateJWTToken(*user.ID) if err != nil { log.Println("Error generating JWT token:", err) c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"message": "Internal server error"}) @@ -113,20 +114,19 @@ func githubCallbackHandler(c *gin.Context) { } func initDB() { - cfg, err := mysql.ParseDSN(os.Getenv("MYSQL_DSN")) - if err != nil { - log.Fatal("Error parsing MySQL DSN:", err) - } - - dbConn, err = sql.Open("mysql", cfg.FormatDSN()) + dsn := "chat:123456@tcp(42.192.36.14:3306)/chat?charset=utf8mb4&parseTime=True&loc=Local" + db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) if err != nil { log.Fatal("Error opening database:", err) } - err = dbConn.Ping() + sqlDB, err := db.DB() if err != nil { log.Fatal("Error connecting to database:", err) } + sqlDB.SetMaxIdleConns(10) + sqlDB.SetMaxOpenConns(100) + sqlDB.SetConnMaxLifetime(time.Hour) log.Println("Database connection established") } @@ -151,13 +151,7 @@ func generateRandomString(length int) string { func storeStateToDB(state, code string) error { query := "INSERT INTO oauth_state (state, code) VALUES (?, ?)" - stmt, err := dbConn.Prepare(query) - if err != nil { - return err - } - defer stmt.Close() - - _, err = stmt.Exec(state, code) + err := db.Exec(query, state, code).Error if err != nil { return err } @@ -167,7 +161,7 @@ func storeStateToDB(state, code string) error { func verifyState(state, code string) bool { query := "SELECT COUNT(*) FROM oauth_state WHERE state = ? AND code = ?" - row := dbConn.QueryRow(query, state, code) + row := db.Exec(query, state, code) var count int err := row.Scan(&count) @@ -185,13 +179,8 @@ func verifyState(state, code string) bool { func storeUserToDB(user *github.User) error { query := "INSERT INTO users (id, login, email) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE login = VALUES(login), email = VALUES(email)" - stmt, err := dbConn.Prepare(query) - if err != nil { - return err - } - defer stmt.Close() - _, err = stmt.Exec(user.GetID(), user.GetLogin(), user.GetEmail()) + err := db.Exec(query, user.GetID(), user.GetLogin(), user.GetEmail()).Error if err != nil { return err } diff --git a/go.mod b/go.mod index a12328f..8f98754 100644 --- a/go.mod +++ b/go.mod @@ -3,15 +3,47 @@ module chat go 1.19 require ( + github.com/gin-contrib/cors v1.4.0 + github.com/gin-gonic/gin v1.9.0 + github.com/golang-jwt/jwt v3.2.2+incompatible github.com/google/go-github v17.0.0+incompatible + github.com/google/go-github/v50 v50.2.0 github.com/gorilla/websocket v1.5.0 golang.org/x/oauth2 v0.6.0 + gorm.io/driver/mysql v1.4.7 + gorm.io/gorm v1.23.8 ) require ( + github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 // indirect + github.com/bytedance/sonic v1.8.0 // indirect + github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect + github.com/cloudflare/circl v1.1.0 // indirect + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.11.2 // indirect + github.com/go-sql-driver/mysql v1.7.0 // indirect + github.com/goccy/go-json v0.10.0 // indirect github.com/golang/protobuf v1.5.2 // indirect github.com/google/go-querystring v1.1.0 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/cpuid/v2 v2.0.9 // indirect + github.com/leodido/go-urn v1.2.1 // indirect + github.com/mattn/go-isatty v0.0.17 // indirect + github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/pelletier/go-toml/v2 v2.0.6 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.2.9 // indirect + golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect + golang.org/x/crypto v0.7.0 // indirect golang.org/x/net v0.8.0 // indirect + golang.org/x/sys v0.6.0 // indirect + golang.org/x/text v0.8.0 // indirect google.golang.org/appengine v1.6.7 // indirect - google.golang.org/protobuf v1.28.0 // indirect + google.golang.org/protobuf v1.28.1 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect )