-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
60 lines (51 loc) · 1.36 KB
/
main.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
package main
import (
"log"
"net/http"
"os"
"time"
"github.com/akamensky/argparse"
"github.com/gorilla/mux"
"github.com/wlwanpan/localcast/common"
"github.com/wlwanpan/localcast/device"
"github.com/wlwanpan/localcast/media"
)
func initRoutes() http.Handler {
r := mux.NewRouter()
r = media.InitRoutes(r)
r = device.InitRoutes(r)
return common.SetCORS(r)
}
func main() {
parser := argparse.NewParser("localcast", "Api server for casting local media files.")
log.SetOutput(os.Stdout)
port := parser.String("p", "port", &argparse.Options{
Required: false,
Default: "4040",
Help: "Port to run the server.",
})
mediaSrc := parser.String("s", "source", &argparse.Options{
Required: true,
Help: "Path of folder to serve.",
})
if err := parser.Parse(os.Args); err != nil {
log.Fatal(err)
}
log.Printf("Loading localfiles from: %s \n", *mediaSrc)
if err := media.LoadLocalFiles(*mediaSrc); err != nil {
log.Fatal(err)
}
log.Printf("Sucessfully cached: %d", media.CachedMediaCount())
log.Printf("Initializing Google Home")
// Not sure if cache on server start.
devices := device.Load()
device.Cache(devices)
s := &http.Server{
Handler: initRoutes(),
Addr: ":" + *port,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
log.Printf("Starting server on %s\n", *port)
log.Fatal(s.ListenAndServe())
}