/
main.go
199 lines (173 loc) · 4.11 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package main
import (
"flag"
"fmt"
"os"
"runtime/pprof"
"time"
"gopkg.in/olivere/elastic.v3"
"code.google.com/p/gcfg"
log "github.com/Sirupsen/logrus"
expvarGin "github.com/gin-gonic/contrib/expvar"
"github.com/gin-gonic/gin"
"github.com/rifflock/lfshook"
"github.com/sohlich/elogrus"
"github.com/sohlich/go-plag/parser"
"github.com/sohlich/healthcheck"
)
const (
//Version of system
Version = "0.1"
//Author is maintainer of system
Author = "Radomir Sohlich <sohlich@gmail.com>"
config = "plag.conf" //configuration file
)
var (
engine = gin.Default()
mongo DataStorage = &Mongo{
Database: "plag",
AssignmentCollection: "assignments",
SubmissionCollection: "submissions",
ResultCollection: "results",
}
cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
//Log is Global logger
Log = log.StandardLogger()
//Apac is config for apac
Apac ApacConfig
//expvar
metrics *Metrics
healthChecker = healthcheck.New(time.Minute)
)
func main() {
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
fmt.Println("Error: ", err)
}
//runtime.SetCPUProfileRate(100)
err = pprof.StartCPUProfile(f)
if err != nil {
fmt.Println("Error: ", err)
}
defer pprof.StopCPUProfile()
}
initMetrics()
//Load config
cfg := loadProperties(config)
Log = NewLogger(cfg)
Apac = cfg.Apac
Log.Infof("Apac path %s", Apac.URL)
//Setup and init storage
mgoConf := cfg.Mongo
mgo, _ := mongo.(*Mongo)
mgo.AssignmentCollection = mgoConf.Assignments
mgo.ResultCollection = mgoConf.Results
mgo.SubmissionCollection = mgoConf.Submissions
mgo.Database = mgoConf.Database
mgo.ConnectionString = mgoConf.ConnectionString()
initStorage()
defer mgo.CloseSession()
healthChecker.RegisterIndicator(mgo)
healthChecker.Start()
healthChecker.AddHook("main", func(res map[string]bool) {
if res[mgo.Name()] {
metrics.SetDatabaseState(DatabaseOK)
} else {
metrics.SetDatabaseState(DatabaseNotConnected)
}
})
//Load plugins
loadPlugins()
//Setup and start webserver
webConf := cfg.Server
initGin(engine)
address := fmt.Sprintf("%s:%s",
webConf.Host,
webConf.Port)
engine.Run(address)
}
func initMetrics() {
metrics = NewMetrics()
}
func loadProperties(cfgFile string) configFile {
var err error
var cfg configFile
if cfgFile != "" {
err = gcfg.ReadFileInto(&cfg, cfgFile)
}
if err != nil {
Log.Panic(err)
}
return cfg
}
//NewLogger creates a
//logger for logrus
func NewLogger(cfg configFile) *log.Logger {
//This creates new logger
Log = log.New()
Log.Formatter = new(log.JSONFormatter)
Log.Hooks.Add(lfshook.NewHook(lfshook.PathMap{
log.InfoLevel: cfg.Log.Path,
log.ErrorLevel: cfg.Log.Path,
log.DebugLevel: cfg.Log.Path,
}))
Log.Hooks.Add(&metricsHook{
metrics,
})
client, err := elastic.NewClient(
elastic.SetURL(cfg.Elastic.URL),
elastic.SetBasicAuth(cfg.Elastic.Username, cfg.Elastic.Password))
if err != nil {
log.Error(err)
} else {
hook, err := elogrus.NewElasticHook(client, cfg.Server.Host, log.DebugLevel, "goplag")
if err != nil {
log.Error(err)
} else {
Log.Hooks.Add(hook)
}
}
return Log
}
type metricsHook struct {
metrics *Metrics
}
func (m *metricsHook) Levels() []log.Level {
return []log.Level{
log.ErrorLevel,
}
}
func (m *metricsHook) Fire(entry *log.Entry) error {
m.metrics.ErrorInc()
return nil
}
//Load plugins
func loadPlugins() {
//Load plugins
parser.SetLogger(Log)
parser.LoadPlugins("plugin")
}
//Setup gin Engine server
func initGin(ginEngine *gin.Engine) {
ginEngine.Use(logrusLogger())
ginEngine.POST("/assignment", putAssignment)
ginEngine.POST("/submission", putSubmission)
ginEngine.GET("/plugin/langs", getSupportedLangs)
ginEngine.GET("/debug/vars", expvarGin.Handler())
ginEngine.GET("/health", healthCheck)
}
func logrusLogger() gin.HandlerFunc {
return func(c *gin.Context) {
Log.Infof("%s:%s from %s", c.Request.Method, c.Request.URL.String(), c.Request.RemoteAddr)
}
}
func initStorage() {
Log.Infoln("Initializing storage")
err := mongo.OpenSession()
if err != nil {
Log.Fatal(err)
}
// metrics.SetDatabaseState(DatabaseOK)
}