Skip to content

Commit

Permalink
flags parsing refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mejgun committed Aug 15, 2020
1 parent efbd6aa commit a3be27c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
11 changes: 11 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ type corruptedT struct {
size int64
}

type flagsT struct {
version bool
enableDebug bool
enableErrorHeaders bool
ignoreMissingHeaders bool
ignoreSSLErrors bool
portInt uint
errorVideoPath string
customdl string
}

type extractorF func(string, string, string, debugF) (string, int64, error)

type sendErrorVideoF func(http.ResponseWriter, error)
Expand Down
50 changes: 24 additions & 26 deletions streamer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,50 +21,34 @@ const defaultExpireTime = 3 * 60 * 60
const defaultErrorHeader = "Error-Header-"

func main() {
var version bool
var enableDebug bool
var enableErrorHeaders bool
var ignoreMissingHeaders bool
var ignoreSSLErrors bool
var portInt uint
var errorVideoPath string
var customdl string
flag.BoolVar(&version, "version", false, "prints current yt-proxy version")
flag.BoolVar(&enableDebug, "debug", false, "turn on debug")
flag.BoolVar(&enableErrorHeaders, "error-headers", false, "show errors in headers (insecure)")
flag.BoolVar(&ignoreMissingHeaders, "ignore-missing-headers", false, "do not strictly check video headers")
flag.BoolVar(&ignoreSSLErrors, "ignore-ssl-errors", false, "ignore video server SSL errors (insecure)")
flag.UintVar(&portInt, "port", 8080, "listen port")
flag.StringVar(&errorVideoPath, "error-video", "corrupted.mp4", "file that will be shown on errors")
flag.StringVar(&customdl, "custom-extractor", "", "use custom url extractor, will be called like this: program_name url video_height video_format")
flag.Parse()
if version {
flags := parseCLIFlags()
if flags.version {
fmt.Println(appVersion)
os.Exit(0)
}
var extractor extractorF
if len(customdl) > 0 {
extractor = getCustomDL(customdl)
if len(flags.customdl) > 0 {
extractor = getCustomDL(flags.customdl)
} else {
extractor = getYTDL()
}
var requests chan requestChan
requests = make(chan requestChan)
var links linksCache
links.cache = make(map[string]lnkT)
debug := getDebugFunc(enableDebug)
errorVideo := readErrorVideo(errorVideoPath)
sendErrorVideo := getSendErrorVideoFunc(enableErrorHeaders, errorVideo)
httpRequest := getDoRequestFunc(ignoreSSLErrors)
debug := getDebugFunc(flags.enableDebug)
errorVideo := readErrorVideo(flags.errorVideoPath)
sendErrorVideo := getSendErrorVideoFunc(flags.enableErrorHeaders, errorVideo)
httpRequest := getDoRequestFunc(flags.ignoreSSLErrors)
go parseLinks(requests, debug, &links, extractor)
port := fmt.Sprintf("%d", portInt)
port := fmt.Sprintf("%d", flags.portInt)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
log.Println(r.RemoteAddr, r.RequestURI)
http.NotFound(w, r)
})
http.HandleFunc("/play/", func(w http.ResponseWriter, r *http.Request) {
log.Println(r.RemoteAddr, r.RequestURI)
playVideo(w, r, requests, debug, sendErrorVideo, !ignoreMissingHeaders, httpRequest)
playVideo(w, r, requests, debug, sendErrorVideo, !flags.ignoreMissingHeaders, httpRequest)
})
s := &http.Server{
Addr: ":" + port,
Expand Down Expand Up @@ -230,3 +214,17 @@ func errorToHeaders(e error) ([]string, []string) {
}
return headers, filtered
}

func parseCLIFlags() flagsT {
var f flagsT
flag.BoolVar(&f.version, "version", false, "prints current yt-proxy version")
flag.BoolVar(&f.enableDebug, "debug", false, "turn on debug")
flag.BoolVar(&f.enableErrorHeaders, "error-headers", false, "show errors in headers (insecure)")
flag.BoolVar(&f.ignoreMissingHeaders, "ignore-missing-headers", false, "do not strictly check video headers")
flag.BoolVar(&f.ignoreSSLErrors, "ignore-ssl-errors", false, "do not check video server certificate (insecure)")
flag.UintVar(&f.portInt, "port", 8080, "listen port")
flag.StringVar(&f.errorVideoPath, "error-video", "corrupted.mp4", "file that will be shown on errors")
flag.StringVar(&f.customdl, "custom-extractor", "", "use custom url extractor, will be called like this: program_name url video_height video_format")
flag.Parse()
return f
}

0 comments on commit a3be27c

Please sign in to comment.