-
Notifications
You must be signed in to change notification settings - Fork 0
/
libra.go
171 lines (140 loc) · 5.83 KB
/
libra.go
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
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/goccy/go-json"
"github.com/gofiber/fiber/v2"
"github.com/LibraMusic/LibraCore/config"
"github.com/LibraMusic/LibraCore/db"
"github.com/LibraMusic/LibraCore/logging"
"github.com/LibraMusic/LibraCore/middleware"
"github.com/LibraMusic/LibraCore/routes"
"github.com/LibraMusic/LibraCore/sources"
"github.com/LibraMusic/LibraCore/storage"
"github.com/LibraMusic/LibraCore/utils"
)
// mimeType := mime.TypeByExtension(filepath.Ext(filePath))
func main() {
executablePath, _ := os.Executable()
executablePath, _ = filepath.EvalSymlinks(executablePath)
os.Chdir(filepath.Dir(executablePath))
conf := config.LoadConfig()
config.Conf = conf
logging.Init()
signingMethod := utils.GetCorrectSigningMethod(conf.Auth.JWTSigningMethod)
if signingMethod == "" {
logging.Fatal().Msgf("Invalid or unsupported JWT signing method: %s\n", conf.Auth.JWTSigningMethod)
}
conf.Auth.JWTSigningMethod = signingMethod
if strings.HasPrefix(conf.Auth.JWTSigningKey, "file:") {
keyPath := strings.TrimPrefix(conf.Auth.JWTSigningKey, "file:")
keyPath, err := filepath.Abs(keyPath)
if err != nil {
logging.Fatal().Err(err).Msg("Error getting absolute path of JWT signing key file")
}
keyData, err := os.ReadFile(keyPath)
if err != nil {
logging.Fatal().Err(err).Msg("Error reading JWT signing key file")
}
conf.Auth.JWTSigningKey = string(keyData)
}
err := utils.LoadPrivateKey(signingMethod, conf.Auth.JWTSigningKey)
if err != nil {
logging.Fatal().Err(err).Msg("Error loading private key")
}
db.ConnectDatabase()
err = db.DB.CleanExpiredTokens()
if err != nil {
logging.Error().Err(err).Msg("Error cleaning expired tokens")
}
storage.CleanOverfilledStorage()
sources.InitManager()
// Test code below (TODO: Remove)
s, err := sources.InitYouTubeSource()
if err != nil {
logging.Fatal().Err(err).Msg("Error initializing YouTube source")
}
a, b := s.Search("Lord of Ashes", 5, 0, map[string]interface{}{})
fmt.Println(a)
fmt.Println(b)
//fmt.Println(s.ContainsURL("https://www.youtube.com/watch?v=orimodrogvd"))
//fmt.Println(s.ContainsURL("https://www.youtube.com/watch?v=uGxcco8Uq6A"))
// Test code above (TODO: Remove)
libraService := fiber.Map{
"id": conf.Application.SourceID,
"name": conf.Application.SourceName,
"version": utils.LibraVersion,
"source_types": []string{"content", "metadata", "lyrics"},
"media_types": conf.Application.MediaTypes,
}
libraMeta := fiber.Map{
"version": utils.LibraVersion,
"database": db.DB.EngineName(),
}
fmt.Println()
fmt.Printf("Libra v%s\n", utils.LibraVersion)
fmt.Printf("Database: %s\n", db.DB.EngineName())
app := fiber.New(fiber.Config{
JSONEncoder: json.Marshal,
JSONDecoder: json.Unmarshal,
})
app.Get("/", func(c *fiber.Ctx) error {
offer := c.Accepts(fiber.MIMEApplicationJSON, fiber.MIMETextHTML)
if offer == fiber.MIMEApplicationJSON {
return c.JSON(&libraService)
} else if offer == fiber.MIMETextHTML {
// TODO: Implement
return c.SendString("<h1>Libra</h1>")
}
return c.SendStatus(fiber.StatusNotAcceptable)
})
app.Get("/meta", func(c *fiber.Ctx) error {
return c.JSON(&libraMeta)
})
app.Get("/service", func(c *fiber.Ctx) error {
return c.JSON(&libraService)
})
app.Get("/app", func(c *fiber.Ctx) error {
// TODO: Implement
return c.SendString("<h1>Libra</h1>")
})
api := app.Group("/api")
auth := api.Group("/auth")
auth.Post("/register", routes.Register)
auth.Post("/login", routes.Login)
auth.Post("/logout", middleware.JWTProtected, routes.Logout)
v1 := api.Group("/v1")
v1.Get("/playables", middleware.GlobalJWTProtected, routes.V1Playables)
v1.Get("/search", middleware.GlobalJWTProtected, routes.V1Search)
// START TO REFRACTOR
v1.Get("/track/:id", middleware.GlobalJWTProtected, routes.V1Track)
v1.Get("/track/:id/is_stored", middleware.GlobalJWTProtected, routes.V1TrackIsStored)
v1.Get("/track/:id/stream", middleware.GlobalJWTProtected, routes.V1TrackStream)
v1.Get("/track/:id/cover", middleware.GlobalJWTProtected, routes.V1TrackCover)
v1.Get("/track/:id/lyrics", middleware.GlobalJWTProtected, routes.V1TrackLyrics)
v1.Get("/track/:id/lyrics/:lang", middleware.GlobalJWTProtected, routes.V1TrackLyricsLang)
v1.Get("/album/:id", middleware.GlobalJWTProtected, routes.V1Album)
v1.Get("/album/:id/cover", middleware.GlobalJWTProtected, routes.V1AlbumCover)
v1.Get("/album/:id/tracks", middleware.GlobalJWTProtected, routes.V1AlbumTracks)
v1.Get("/video/:id", middleware.GlobalJWTProtected, routes.V1Video)
v1.Get("/video/:id/is_stored", middleware.GlobalJWTProtected, routes.V1VideoIsStored)
v1.Get("/video/:id/stream", middleware.GlobalJWTProtected, routes.V1VideoStream)
v1.Get("/video/:id/cover", middleware.GlobalJWTProtected, routes.V1VideoCover)
v1.Get("/video/:id/subtitles", middleware.GlobalJWTProtected, routes.V1VideoSubtitles)
v1.Get("/video/:id/subtitles/:lang", middleware.GlobalJWTProtected, routes.V1VideoSubtitlesLang)
v1.Get("/video/:id/lyrics", middleware.GlobalJWTProtected, routes.V1VideoSubtitles)
v1.Get("/video/:id/lyrics/:lang", middleware.GlobalJWTProtected, routes.V1VideoSubtitlesLang)
v1.Get("/playlist/:id", middleware.GlobalJWTProtected, routes.V1Playlist)
v1.Get("/playlist/:id/cover", middleware.GlobalJWTProtected, routes.V1PlaylistCover)
v1.Get("/playlist/:id/tracks", middleware.GlobalJWTProtected, routes.V1PlaylistTracks)
v1.Get("/artist/:id", middleware.GlobalJWTProtected, routes.V1Artist)
v1.Get("/artist/:id/cover", middleware.GlobalJWTProtected, routes.V1ArtistCover)
v1.Get("/artist/:id/albums", middleware.GlobalJWTProtected, routes.V1ArtistAlbums)
v1.Get("/artist/:id/tracks", middleware.GlobalJWTProtected, routes.V1ArtistTracks)
// END TO REFRACTOR
if err = app.Listen(fmt.Sprintf(":%d", conf.Application.Port)); err != nil {
logging.Fatal().Err(err).Msg("Error starting server")
}
}