/
main.go
92 lines (81 loc) · 1.69 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"time"
"gopkg.in/yaml.v2"
)
// Conf object holds the server configuration
var Conf *Configuration
// TEngine is the TaskEngine used to submit new tasks
var TEngine *TaskEngine
// Configuration dictates the schema of the yml conf file
type Configuration struct {
Server struct {
Listen string
Dirs struct {
Templates string
Static string
Datasets string
}
}
Database string
Logfile string
Scripts struct {
MDS string
ML map[string]string
}
}
// LoadConfig loads the configuration file in memory
func LoadConfig(filename string) *Configuration {
b, err := ioutil.ReadFile(filename)
if err != nil {
log.Println(err)
return nil
}
conf := new(Configuration)
err = yaml.Unmarshal(b, conf)
if err != nil {
log.Println(err)
return nil
}
return conf
}
// Server entry point
func main() {
if len(os.Args) < 2 {
fmt.Fprintf(os.Stderr, "Please provide conf file\n")
os.Exit(1)
}
Conf = LoadConfig(os.Args[1])
if Conf == nil {
os.Exit(1)
}
rand.Seed(int64(time.Now().Nanosecond()))
setLogger(Conf.Logfile)
TEngine = NewTaskEngine()
fs := http.FileServer(http.Dir(Conf.Server.Dirs.Static))
http.Handle("/static/", http.StripPrefix("/static/", fs))
http.HandleFunc("/", uiHandler)
http.HandleFunc("/api/", restHandler)
err := http.ListenAndServe(Conf.Server.Listen, nil)
if err != nil {
log.Println(err)
}
}
func setLogger(logfile string) {
if logfile != "" {
f, er := os.OpenFile(logfile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if er != nil {
fmt.Println(er)
os.Exit(1)
} else {
log.SetOutput(f)
}
}
log.SetFlags(log.Ldate | log.Ltime | log.Llongfile)
}