-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
66 lines (50 loc) · 1.32 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
package main
import (
"database/sql"
"github.com/Epiteks/Epirank/config"
"github.com/Epiteks/Epirank/database"
"github.com/Epiteks/Epirank/ranking"
"github.com/Epiteks/Epirank/routes"
log "github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
_ "github.com/mattn/go-sqlite3"
"os"
)
// APIMiddleware will add the db connection to the context
func APIMiddleware(db *sql.DB) gin.HandlerFunc {
return func(c *gin.Context) {
c.Set("database", db)
c.Next()
}
}
func init() {
// Log as ASCII Formatter
log.SetFormatter(&log.TextFormatter{})
// Output to stdout, could also be a file.
log.SetOutput(os.Stdout)
// Log everything
log.SetLevel(log.InfoLevel)
}
func main() {
if db, err := database.Init(config.DatabasePath); err != nil {
log.Fatal(err)
} else {
defer db.Close()
database.CreateTable(db)
if err := ranking.InitRanking(db); err != nil {
log.Error(err)
os.Exit(-1)
}
// Webservice
router := gin.Default()
router.Use(APIMiddleware(db))
gopath := os.Getenv("GOPATH")
gopath += "/src/github.com/Epiteks/Epirank"
router.LoadHTMLGlob(gopath + "/front/html/*")
router.Static("/css", gopath+"/front/css")
router.Static("/js", gopath+"/front/js")
router.StaticFile("/favicon.ico", gopath+"/front/icons/favicon-96x96.png")
router.GET("/", routes.GetStudents)
router.Run()
}
}