-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
96 lines (86 loc) · 1.63 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
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
"github.com/traPtitech/Emoine/repository"
"github.com/traPtitech/Emoine/router"
)
const (
dbInitDirectory = "./mysql"
)
var (
port = 80
)
func main() {
log.SetFlags(log.Llongfile)
user, ok := os.LookupEnv("MYSQL_USERNAME")
if !ok {
user = "root"
}
pass, ok := os.LookupEnv("MYSQL_PASSWORD")
if !ok {
pass = "password"
}
host, ok := os.LookupEnv("MYSQL_HOSTNAME")
if !ok {
host = "mysql"
}
dbPort, ok := os.LookupEnv("MYSQL_PORT")
if !ok {
dbPort = "3306"
}
dbname, ok := os.LookupEnv("MYSQL_DATABASE")
if !ok {
dbname = "emoine"
}
// connect to db
db := sqlx.MustConnect("mysql", fmt.Sprintf(
"%s:%s@tcp(%s:%s)/%s?parseTime=true",
user,
pass,
host,
dbPort,
dbname,
))
// db connection for batch executing, allowing multi statements
dbForBatch := sqlx.MustConnect("mysql", fmt.Sprintf(
"%s:%s@tcp(%s:%s)/%s?multiStatements=true&parseTime=true",
user,
pass,
host,
dbPort,
dbname,
))
// create schema
var paths []string
err := filepath.Walk(dbInitDirectory, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
paths = append(paths, path)
return nil
})
if err != nil {
panic(err)
}
for _, path := range paths {
data, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
dbForBatch.MustExec(string(data))
}
repo, err := repository.NewSqlxRepository(db)
if err != nil {
panic(err)
}
echo := router.Setup(repo)
if err = echo.Start(fmt.Sprintf(":%d", port)); err != nil {
panic(err)
}
}