Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
75 lines (64 loc) · 2.1 KB
/
Copy pathmain.go
File metadata and controls
75 lines (64 loc) · 2.1 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
package main
import (
"github.com/buaazp/fasthttprouter"
"github.com/namsral/flag"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/reuseport"
"log"
"time"
)
type Config struct {
WebListen string
FFmpegBinary string
ServerConvertTimeout time.Duration
}
const (
resizeHeaderNameSource = "x-resize-base"
resizeHeaderNameSchema = "x-resize-scheme"
resizeHeaderDefaultSchema = "https"
resizeQArgBitrateName = "bitrate"
resizeQArgOutSampleRateName = "samplerate"
resizeQArgDurationName = "duration"
httpClientMaxIdleConns = 64
httpClientMaxIdleConnsPerHost = 64
httpClientIdleConnTimeout = 120 * time.Second
httpClientFileDownloadTimeout = 30 * time.Second
serverMaxConcurrencyRequests = 64
serverRequestReadTimeout = 10 * time.Second
serverResponseWriteTimeout = 20 * time.Second
serverMaxBodySize = 100 * 1024 * 1024 //100 Mb
httpUserAgent = "reMP3 HTTP Fetcher"
)
var config = Config{}
func main() {
parseFlags()
listen, err := reuseport.Listen("tcp4", config.WebListen)
if err != nil {
log.Fatalf("Error in reuseport listener: %s", err)
}
router := getRouter()
server := &fasthttp.Server{
Handler: router.Handler,
DisableKeepalive: true,
MaxRequestBodySize: serverMaxBodySize,
Concurrency: serverMaxConcurrencyRequests,
ReadTimeout: serverRequestReadTimeout,
WriteTimeout: serverResponseWriteTimeout,
}
log.Printf("Server started on %s\n", config.WebListen)
if err := server.Serve(listen); err != nil {
log.Fatalf("Error in ListenAndServe: %s", err)
}
}
func getRouter() *fasthttprouter.Router {
router := fasthttprouter.New()
router.GET("/*p", getResizeHandler)
router.POST("/", postResizeHandler)
return router
}
func parseFlags() {
flag.StringVar(&config.WebListen, "WEB_LISTEN", ":8080", "Listen interface and port")
flag.StringVar(&config.FFmpegBinary, "CFG_FFMPEG_BINARY", "ffmpeg", "Path to ffmpeg binary")
flag.DurationVar(&config.ServerConvertTimeout, "CFG_CONVERT_TIMEOUT", 60*time.Second, "Convert timeout.")
flag.Parse()
}