-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
187 lines (152 loc) · 4.64 KB
/
main.go
File metadata and controls
187 lines (152 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// nolint: revive,depguard
package main
import (
"context"
"fmt"
"karaoke/docs"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"database/sql"
"time"
"github.com/spf13/cobra" // swagger embed files
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger" // gin-swagger middleware
"gorm.io/driver/mysql"
"gorm.io/gorm"
log "k8s.io/klog/v2"
)
type App struct {
GormDB *gorm.DB
}
func (app App) GenGinEngine() *gin.Engine {
docs.SwaggerInfo.Title = "Karaoke API"
docs.SwaggerInfo.Version = "1.0"
docs.SwaggerInfo.BasePath = "/v1"
docs.SwaggerInfo.Schemes = []string{"http", "https"}
engine := gin.Default()
engine.Use(cors.Default())
engine.GET("/v1/videos/search", app.VideoSearch)
engine.GET("/v1/videos/recommend", app.VideoRecommend)
engine.POST("/v1/videos", app.CreateVideo)
engine.DELETE("/v1/videos/:video_id", app.DeleteVideo)
engine.PUT("/v1/videos/:video_id", app.UpdateVideo)
engine.GET("/v1/videos", app.ListVideos)
engine.GET("/v1/users", app.GetUserList)
engine.GET("/v1/user", app.GetUser)
engine.PUT("/v1/user", app.PutUser)
engine.POST("/v1/users", app.CreateUser)
engine.GET("/v1/likes", app.GetLikes)
engine.POST("/v1/likes", app.CreateLike)
engine.DELETE("/v1/likes", app.DeleteLike)
engine.GET("/v1/wishlists", app.GetWishlists)
engine.POST("/v1/wishlists", app.CreateWishlist)
engine.PUT("/v1/wishlists/top", app.TopWishlist)
engine.DELETE("/v1/wishlists", app.DeleteWishlist)
engine.POST("/v1/coupons", app.CreateCoupon)
engine.PUT("/v1/coupons/use", app.UseCoupon)
engine.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
engine.Static("/console", "./vuestic-admin/dist")
engine.NoRoute(func(c *gin.Context) {
c.File("./vuestic-admin/dist/index.html")
})
return engine
}
func setupDB() *gorm.DB {
// Add timeout parameters to DSN
// dsn := "admin:uuyDHZ$!bt6puVYudH@tcp(db-instance.cztkys1lvmzb.us-west-2.rds.amazonaws.com:3306)/karaoke?charset=utf8mb4&parseTime=True&loc=Local&timeout=20s&readTimeout=20s&writeTimeout=20s"
dsn := "admin:uuyDHZ$!bt6puVYudH@tcp(db-instance.cztkys1lvmzb.us-west-2.rds.amazonaws.com:3306)/karaoke?charset=utf8mb4&parseTime=True&loc=Local"
sqlDB, err := sql.Open("mysql", dsn)
if err != nil {
panic(fmt.Errorf("failed to open database: %w", err))
}
// Set connection pool settings
// sqlDB.SetConnMaxLifetime(20 * time.Second)
// sqlDB.SetMaxOpenConns(10)
// sqlDB.SetMaxIdleConns(5)
// Test the connection
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
if err := sqlDB.PingContext(ctx); err != nil {
panic(fmt.Errorf("failed to connect to database: %w", err))
}
log.Info("Successfully connected to database")
db, err := gorm.Open(mysql.New(mysql.Config{
Conn: sqlDB,
}), &gorm.Config{})
if err != nil {
panic(fmt.Errorf("failed to initialize GORM: %w", err))
}
// Test GORM connection with a simple query
if err := db.Raw("SELECT 1").Error; err != nil {
panic(fmt.Errorf("failed to execute test query: %w", err))
}
log.Info("Successfully tested GORM connection")
return db
}
func main() {
gormDB := setupDB()
app := &App{
GormDB: gormDB,
}
log.InitFlags(nil)
defer log.Flush()
cmdMigrate := &cobra.Command{
Use: "migrate",
Run: func(cmd *cobra.Command, args []string) {
if err := gormDB.AutoMigrate(&Video{}, &User{}, &Like{}, &Wishlist{}, &Coupon{}); err != nil {
panic(err)
}
},
}
cmdServe := &cobra.Command{
Use: "serve",
Run: func(cmd *cobra.Command, args []string) {
engine := app.GenGinEngine()
log.Info("Serving on :8080")
if err := engine.Run(":8080"); err != nil {
panic(err)
}
},
}
cmdVideo := &cobra.Command{
Use: "video",
}
cmdVideoYoutube := &cobra.Command{
Use: "youtube",
Run: func(cmd *cobra.Command, args []string) {
if err := syncVideos(gormDB); err != nil {
panic(err)
}
},
}
cmdVideoSpotify := &cobra.Command{
Use: "spotify",
}
cmdVideoSpotifyPlaylist := &cobra.Command{
Use: "playlist",
Run: func(cmd *cobra.Command, args []string) {
if err := getSpotifyPlaylistSync(gormDB); err != nil {
panic(err)
}
},
}
cmdVideoSpotifyArtist := &cobra.Command{
Use: "artist",
Run: func(cmd *cobra.Command, args []string) {
if err := syncSpotifyArtist(gormDB); err != nil {
panic(err)
}
},
}
cmdVideo.AddCommand(cmdVideoYoutube)
cmdVideo.AddCommand(cmdVideoSpotify)
cmdVideoSpotify.AddCommand(cmdVideoSpotifyPlaylist)
cmdVideoSpotify.AddCommand(cmdVideoSpotifyArtist)
rootCmd := &cobra.Command{Use: "karaoke"}
rootCmd.AddCommand(cmdMigrate)
rootCmd.AddCommand(cmdServe)
rootCmd.AddCommand(cmdVideo)
if err := rootCmd.Execute(); err != nil {
panic(err)
}
}