-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
61 lines (51 loc) · 1.05 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
61
package main
import (
"encoding/json"
"flag"
"io/ioutil"
"log"
"net/http"
"os"
"github.com/mitchellh/go-homedir"
"github.com/nytlabs/st-core/server"
)
var (
port = flag.String("port", "7071", "streamtools port")
)
func main() {
flag.Parse()
// Unpack settings file, or create a new one if necessary
var settings server.Settings
dir, err := homedir.Dir()
if err != nil {
log.Fatal(err)
}
fname := dir + "/.st.json"
d, err := ioutil.ReadFile(fname)
if err != nil {
if os.IsNotExist(err) {
// make a fresh settings file
log.Println("creating new settings file at", fname)
newSettings := server.NewSettings()
d, err = json.Marshal(newSettings)
err = ioutil.WriteFile(fname, d, 0644)
if err != nil {
log.Fatal(err)
}
} else {
log.Fatal(err)
}
}
err = json.Unmarshal(d, &settings)
if err != nil {
log.Fatal(err)
}
s := server.NewServer(settings)
r := s.NewRouter()
http.Handle("/", r)
log.Println("serving on", *port)
err = http.ListenAndServe(":"+*port, nil)
if err != nil {
log.Panicf(err.Error())
}
}