Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Introduce solana auth integration" #3

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions .gitpod.yml

This file was deleted.

77 changes: 33 additions & 44 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,76 +2,65 @@ package app

import (
"fmt"
"net/http"
"os"
"time"
"log"

"gorm.io/gorm"
_ "github.com/lib/pq"

"collection/config/envconfig"
"collection/domain"
"collection/dto"
"collection/logger"
"collection/service"
"github.com/jmoiron/sqlx"

"gorm.io/driver/postgres"

"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"

"github.com/gorilla/mux"
"github.com/joho/godotenv"
)

func Start() {
envconfig.InitEnvVars()
func Start(){
err := godotenv.Load("local.env")
if err != nil {
log.Fatalf("Some error occured. Err: %s", err)
}

r := mux.NewRouter()
r :=mux.NewRouter()

dbClient := getDbClient()
//wiring
newRepositoryDb := domain.NewUserRepositoryDb(dbClient.Model(&dto.User{}))
newRepositoryDb := domain.NewUserRepositoryDb(dbClient)
us := UserHandler{service.NewUserService(newRepositoryDb)}

r.HandleFunc("/collections",us.CreateCollection).Methods("Post")

r.HandleFunc("/collections", us.CreateCollection).Methods("Post")
ginApp := gin.Default()

corsM := cors.New(cors.Config{AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"},
AllowHeaders: []string{"Origin", "Content-Length", "Content-Type", "Authorization"},
AllowCredentials: false,
MaxAge: 12 * time.Hour,
AllowOrigins: envconfig.EnvVars.ALLOWED_ORIGIN})
ginApp.Use(corsM)
// api.ApplyRoutes(ginApp)
port := envconfig.EnvVars.APP_PORT
err := ginApp.Run(fmt.Sprintf(":%d", port))
if err != nil {
logger.Fatalf("failed to serve app on port %s: %s", port, err)
}

address := os.Getenv("SERVER_ADDRESS")
port := os.Getenv("SERVER_PORT")
log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%s", address, port), r))

}

func getDbClient() *gorm.DB {
func getDbClient() *sqlx.DB{
dbUser := os.Getenv("DB_USER")
dbPasswd := os.Getenv("DB_PASSWD")
dbHost := os.Getenv("DB_HOST")
dbAddr := os.Getenv("DB_ADDR")
dbPort := os.Getenv("DB_PORT")
dbName := os.Getenv("DB_NAME")
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s sslmode=disable port=%s",
dbHost, dbUser, dbPasswd, dbName, dbPort)
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
logger.Fatalf("failed to connect to database: %s", err)
}
db:= os.Getenv("DB")

// Get underlying sql database to ping it
sqlDb, err := db.DB()
if err != nil {
logger.Fatalf("failed to ping database: %s", err)
}
dataSource := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?sslmode=disable", dbUser, dbPasswd, dbAddr, dbPort, dbName)

// If ping fails then log error and exit
if err = sqlDb.Ping(); err != nil {
logger.Fatalf("failed to ping database: %s", err)
client, err := sqlx.Open(db, dataSource)
if err != nil {
panic(err)
}
// See "Important settings" section.
client.SetConnMaxLifetime(time.Minute * 10)
client.SetMaxOpenConns(10)
client.SetMaxIdleConns(10)

logger.Info("Database is Connected")
return db
return client
}


52 changes: 0 additions & 52 deletions app/authenticate.go

This file was deleted.

47 changes: 0 additions & 47 deletions app/flowid.go

This file was deleted.

34 changes: 0 additions & 34 deletions config/envconfig/envconfig.go

This file was deleted.

55 changes: 0 additions & 55 deletions domain/FlowIdRespository.go

This file was deleted.

48 changes: 28 additions & 20 deletions domain/UserRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,52 @@ package domain
import (
"collection/errs"
"collection/logger"
"strconv"

"collection/dto"

_ "github.com/lib/pq"
"gorm.io/gorm"

// _ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)

type UserRepositoryDb struct {
client *gorm.DB
type UserRepositoryDb struct{
client *sqlx.DB
}

func (d UserRepositoryDb) AddUser(c Collection, add []dto.Address) *errs.AppError {
collectionDb := d.client.Model(&Collection{})
err := collectionDb.Create(&c).Error
if err != nil {
logger.Error("Error While creating new account for collection " + err.Error())



func (d UserRepositoryDb)AddUser(c Collection,add []dto.Address)(*errs.AppError){
sqlInsert := `INSERT INTO "Collection"(user_id,name,symbol,description,total_supply,seller_fee,mint_price,game_resource,live_mint_start)values($,$,$,$,$,$,$,$,$)`
result,err := d.client.Exec(sqlInsert,c.User_id,c.Name,c.Symbol,c.Description,c.Total_supply,c.Seller_fee,c.Mint_price,c.Game_resource,c.Live_mint_start)
if err!=nil{
logger.Error("Error While creating new account for collection "+err.Error())
// return errs.NewUnexpectedError("Unexpected error from database")
return nil
}

sellerDb := d.client.Model(&Seller{})
for _, v := range add {
newSeller := Seller{
User_id: c.User_id,
Address: v.Address,
Share: v.Share,
}
err = sellerDb.Create(&newSeller).Error
if err != nil {
logger.Error("Error While creating new account" + err.Error())
id,err:=result.LastInsertId()
if err!=nil{
logger.Error("Error While getting last insert id"+err.Error())
// return errs.NewUnexpectedError("Unexpected error from database")
return nil
}

userId := strconv.FormatInt(id,10)
addInsert := "INSERT INTO Seller(user_id,address,share)values($,$,$)"
for _,v := range add{
_,err := d.client.Exec(addInsert,userId,v.Address,v.Share)
if err!=nil{
logger.Error("Error While creating new account"+err.Error())
// return errs.NewUnexpectedError("Unexpected error from database")
return nil
}
}
return nil
}

func NewUserRepositoryDb(dbCLient *gorm.DB) UserRepositoryDb {
func NewUserRepositoryDb(dbCLient *sqlx.DB)UserRepositoryDb{
return UserRepositoryDb{dbCLient}
}
}
Loading