使用 api_config.json
This commit is contained in:
12
api_config.json
Normal file
12
api_config.json
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
{
|
||||||
|
"database_name": "/tmp/music.sqlite3",
|
||||||
|
"addr": ":8080",
|
||||||
|
"token": "!! config your very strong token here !!",
|
||||||
|
"ffmpeg_configs": {
|
||||||
|
"0. OPUS 128k": {"args": "-c:a libopus -ab 128k"},
|
||||||
|
"OPUS 96k": {"args": "-c:a libopus -ab 96k"},
|
||||||
|
"OPUS 256k": {"args": "-c:a libopus -ab 256k"},
|
||||||
|
"AAC 128k": {"args": "-c:a aac -ab 128k"},
|
||||||
|
"AAC 256k": {"args": "-c:a aac -ab 256k"}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -28,11 +28,11 @@ type FfmpegConfigs struct {
|
|||||||
|
|
||||||
type AddFfmpegConfigRequest struct {
|
type AddFfmpegConfigRequest struct {
|
||||||
Token string `json:"token"`
|
Token string `json:"token"`
|
||||||
|
Name string `json:"name"`
|
||||||
FfmpegConfig FfmpegConfig `json:"ffmpeg_config"`
|
FfmpegConfig FfmpegConfig `json:"ffmpeg_config"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type FfmpegConfig struct {
|
type FfmpegConfig struct {
|
||||||
Name string `json:"name"`
|
|
||||||
Args string `json:"args"`
|
Args string `json:"args"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -458,7 +458,7 @@ func (api *API) HandleAddFfmpegConfig(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// check name and args not null
|
// check name and args not null
|
||||||
if addFfmpegConfigRequest.FfmpegConfig.Name == "" {
|
if addFfmpegConfigRequest.Name == "" {
|
||||||
api.HandleErrorString(w, r, `"ffmpeg_config.name" can't be empty`)
|
api.HandleErrorString(w, r, `"ffmpeg_config.name" can't be empty`)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -469,7 +469,7 @@ func (api *API) HandleAddFfmpegConfig(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
log.Println("[api] Add ffmpeg config")
|
log.Println("[api] Add ffmpeg config")
|
||||||
|
|
||||||
api.APIConfig.FfmpegConfigs[addFfmpegConfigRequest.FfmpegConfig.Name] = &addFfmpegConfigRequest.FfmpegConfig
|
api.APIConfig.FfmpegConfigs[addFfmpegConfigRequest.Name] = &addFfmpegConfigRequest.FfmpegConfig
|
||||||
api.HandleOK(w, r)
|
api.HandleOK(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
31
main.go
31
main.go
@@ -1,39 +1,38 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
"msw-open-music/internal/pkg/api"
|
"msw-open-music/internal/pkg/api"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
var DatabaseName string
|
var APIConfigFilePath string
|
||||||
var Listen string
|
|
||||||
var Token string
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
flag.StringVar(&DatabaseName, "db", "/tmp/music.sqlite3", "sqlite3 database file path")
|
flag.StringVar(&APIConfigFilePath, "apiconfig", "api_config.json", "API Config Json file")
|
||||||
flag.StringVar(&Listen, "listen", ":8080", "http server listening")
|
|
||||||
flag.StringVar(&Token, "token", "mikusavetheworld", "secret token")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
var err error
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
apiConfig := api.NewAPIConfig()
|
apiConfig := api.NewAPIConfig()
|
||||||
apiConfig.FfmpegConfigs["libopus 128k"] = &api.FfmpegConfig{
|
|
||||||
Name: "libopus 128k",
|
apiConfigFile, err := os.Open(APIConfigFilePath)
|
||||||
Args: "-c:a libopus -ab 128k",
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
apiConfig.FfmpegConfigs["libopus 256k"] = &api.FfmpegConfig{
|
err = json.NewDecoder(apiConfigFile).Decode(&apiConfig)
|
||||||
Name: "libopus 256k",
|
if err != nil {
|
||||||
Args: "-c:a libopus -ab 256k",
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
apiConfig.DatabaseName = DatabaseName
|
apiConfigFile.Close()
|
||||||
apiConfig.Addr = Listen
|
|
||||||
apiConfig.Token = Token
|
|
||||||
api, err := api.NewAPI(apiConfig)
|
api, err := api.NewAPI(apiConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
log.Println("Started")
|
log.Println("Starting", apiConfig.DatabaseName, apiConfig.Addr, apiConfig.Token)
|
||||||
log.Fatal(api.Server.ListenAndServe())
|
log.Fatal(api.Server.ListenAndServe())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -573,10 +573,14 @@ const component_stream_config = {
|
|||||||
mounted() {
|
mounted() {
|
||||||
axios.get('/api/v1/get_ffmpeg_config_list',
|
axios.get('/api/v1/get_ffmpeg_config_list',
|
||||||
).then(response => {
|
).then(response => {
|
||||||
|
// 后端返回数据 ffmpeg_configs 是一个字典,name 作为 key,ffmpeg_config{} 作为 value
|
||||||
|
// 为方便前端,此处将 ffmpeg_configs 转为数组,并添加 name 到每个对象中
|
||||||
var ffmpeg_configs = response.data.ffmpeg_configs
|
var ffmpeg_configs = response.data.ffmpeg_configs
|
||||||
var tmp_list = []
|
var tmp_list = []
|
||||||
for (var key in ffmpeg_configs) {
|
for (var key in ffmpeg_configs) {
|
||||||
tmp_list.push(ffmpeg_configs[key])
|
var ffmpeg_config = ffmpeg_configs[key]
|
||||||
|
ffmpeg_config.name = key
|
||||||
|
tmp_list.push(ffmpeg_config)
|
||||||
}
|
}
|
||||||
tmp_list.sort()
|
tmp_list.sort()
|
||||||
this.ffmpeg_config_list = tmp_list
|
this.ffmpeg_config_list = tmp_list
|
||||||
|
|||||||
Reference in New Issue
Block a user