Start to move config to a config file.

This commit is contained in:
Justin Hawkins 2021-09-28 21:17:54 +09:30
parent f7b9454835
commit 2ba4588fba
4 changed files with 74 additions and 42 deletions

59
config/config.go Normal file
View File

@ -0,0 +1,59 @@
package config
import (
"log"
"gopkg.in/yaml.v2"
)
type Server struct {
Port int `yaml:"port"`
Address string `yaml:"address"`
DownloadPath string `yaml:"download_path"`
}
type DownloadProfile struct {
Name string `yaml:"name"`
Command string `yaml:"command"`
Args []string `yaml:"args"`
}
type UI struct {
PopupWidth int `yaml:"popup_width"`
PopupHeight int `yaml:"popup_height"`
}
type Config struct {
Server Server `yaml:"server"`
UI UI `yaml:"ui"`
DownloadProfiles []DownloadProfile `yaml:"profiles"`
ConfigVersion int `yaml:"config_version"`
}
func DefaultConfig() Config {
defaultConfig := Config{}
stdProfile := DownloadProfile{Name: "standard youtube-dl video", Command: "youtube-dl", Args: []string{
"--newline",
"--write-info-json",
"-f",
"bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best",
}}
defaultConfig.DownloadProfiles = append(defaultConfig.DownloadProfiles, stdProfile)
defaultConfig.Server.Port = 6123
defaultConfig.Server.Address = "localhost:6123"
defaultConfig.Server.DownloadPath = "./"
defaultConfig.UI.PopupWidth = 500
defaultConfig.UI.PopupHeight = 500
return defaultConfig
}
func WriteDefaultConfig(path string) {
defaultConfig := DefaultConfig()
s, err := yaml.Marshal(&defaultConfig)
if err != nil {
panic(err)
}
log.Print(string(s))
}

1
go.mod
View File

@ -5,4 +5,5 @@ go 1.16
require (
github.com/gorilla/mux v1.8.0
golang.org/x/mod v0.5.1
gopkg.in/yaml.v2 v2.4.0
)

4
go.sum
View File

@ -13,3 +13,7 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

52
main.go
View File

@ -3,7 +3,6 @@ package main
import (
"embed"
"encoding/json"
"flag"
"fmt"
"html/template"
"io"
@ -18,6 +17,7 @@ import (
"strconv"
"github.com/gorilla/mux"
"github.com/tardisx/gropple/config"
"github.com/tardisx/gropple/version"
)
@ -36,48 +36,16 @@ type download struct {
var downloads []*download
var downloadId = 0
var downloadPath = "./"
var address string
var dlCmd = "youtube-dl"
type args []string
var dlArgs = args{}
var defaultArgs = args{
"--write-info-json",
"-f", "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best",
"--newline",
}
var versionInfo = version.Info{CurrentVersion: "v0.4.0"}
//go:embed web
var webFS embed.FS
func (i *args) Set(value string) error {
*i = append(*i, strings.TrimSpace(value))
return nil
}
func (i *args) String() string {
return strings.Join(*i, ",")
}
var conf config.Config
func main() {
var port int
flag.IntVar(&port, "port", 6283, "port to listen on")
flag.StringVar(&address, "address", "http://localhost:6283", "address for the service")
flag.StringVar(&downloadPath, "path", "", "path for downloaded files - defaults to current directory")
flag.StringVar(&dlCmd, "dl-cmd", "youtube-dl", "downloader to use")
flag.Var(&dlArgs, "dl-args", "arguments to the downloader")
flag.Parse()
if len(dlArgs) == 0 {
dlArgs = defaultArgs
}
conf = config.DefaultConfig()
r := mux.NewRouter()
r.HandleFunc("/", HomeHandler)
@ -90,7 +58,7 @@ func main() {
srv := &http.Server{
Handler: r,
Addr: fmt.Sprintf(":%d", port),
Addr: fmt.Sprintf(":%d", conf.Server.Port),
// Good practice: enforce timeouts for servers you create!
WriteTimeout: 5 * time.Second,
ReadTimeout: 5 * time.Second,
@ -105,7 +73,7 @@ func main() {
}()
log.Printf("starting gropple %s - https://github.com/tardisx/gropple", versionInfo.CurrentVersion)
log.Printf("go to %s for details on installing the bookmarklet and to check status", address)
log.Printf("go to %s for details on installing the bookmarklet and to check status", conf.Server.Address)
log.Fatal(srv.ListenAndServe())
}
@ -121,7 +89,7 @@ func VersionHandler(w http.ResponseWriter, r *http.Request) {
func HomeHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
bookmarkletURL := fmt.Sprintf("javascript:(function(f,s,n,o){window.open(f+encodeURIComponent(s),n,o)}('%s/fetch?url=',window.location,'yourform','width=500,height=500'));", address)
bookmarkletURL := fmt.Sprintf("javascript:(function(f,s,n,o){window.open(f+encodeURIComponent(s),n,o)}('%s/fetch?url=',window.location,'yourform','width=500,height=500'));", conf.Server.Address)
t, err := template.ParseFS(webFS, "web/layout.tmpl", "web/index.html")
if err != nil {
@ -184,7 +152,7 @@ func FetchHandler(w http.ResponseWriter, r *http.Request) {
} else {
// check the URL for a sudden but inevitable betrayal
if strings.Contains(url[0], address) {
if strings.Contains(url[0], conf.Server.Address) {
w.WriteHeader(400)
fmt.Fprint(w, "you musn't gropple your gropple :-)")
return
@ -224,11 +192,11 @@ func FetchHandler(w http.ResponseWriter, r *http.Request) {
func queue(dl *download) {
cmdSlice := []string{}
cmdSlice = append(cmdSlice, dlArgs...)
cmdSlice = append(cmdSlice, conf.DownloadProfiles[0].Args...)
cmdSlice = append(cmdSlice, dl.Url)
cmd := exec.Command(dlCmd, cmdSlice...)
cmd.Dir = downloadPath
cmd := exec.Command(conf.DownloadProfiles[0].Command, cmdSlice...)
cmd.Dir = conf.Server.DownloadPath
stdout, err := cmd.StdoutPipe()
if err != nil {