-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
82 lines (71 loc) · 1.96 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
package main
import (
"fmt"
"log"
"net"
"net/url"
"os"
"path"
"runtime"
"strings"
"time"
"github.com/sangianpatrick/grpc-service-demo/pkg/util"
"github.com/sangianpatrick/grpc-service-demo/src/service"
_ "github.com/joho/godotenv/autoload" // for development
"github.com/sirupsen/logrus"
"go.elastic.co/apm/module/apmlogrus"
"go.elastic.co/apm/module/apmsql"
_ "go.elastic.co/apm/module/apmsql/mysql"
"google.golang.org/grpc"
)
func main() {
port := os.Getenv("PORT")
mariadbHost := os.Getenv("MARIADB_HOST")
mariadbPort := os.Getenv("MARIADB_PORT")
mariadbUser := os.Getenv("MARIADB_USER")
mariadbPassword := os.Getenv("MARIADB_PASSWORD")
mariadbName := os.Getenv("MARIADB_DATABASE")
// set logger with apm hook
logger := logrus.New()
logger.SetFormatter(getLogFormatter())
logger.SetReportCaller(true)
logger.AddHook(&apmlogrus.Hook{
LogLevels: logrus.AllLevels,
})
// set mariadb
mariadbConnectionString := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", mariadbUser, mariadbPassword, mariadbHost, mariadbPort, mariadbName)
val := url.Values{}
val.Add("parseTime", "1")
val.Add("loc", "Asia/Jakarta")
dsn := fmt.Sprintf("%s?%s", mariadbConnectionString, val.Encode())
mariadb, err := apmsql.Open("mysql", dsn)
if err != nil {
logger.Fatal(err)
}
err = mariadb.Ping()
if err != nil {
logger.Fatal(err)
}
mariadb.SetMaxOpenConns(100)
mariadb.SetMaxIdleConns(5)
mariadb.SetConnMaxLifetime(time.Minute * 5)
server := grpc.NewServer()
// initiate service
service.InitializeUser(logger, server, mariadb)
listener, err := net.Listen("tcp", fmt.Sprintf(":%s", port))
if err != nil {
log.Fatal(err)
}
log.Fatal(server.Serve(listener))
}
func getLogFormatter() *logrus.JSONFormatter {
return &logrus.JSONFormatter{
TimestampFormat: util.MySQLFormat,
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
s := strings.Split(f.Function, ".")
funcname := s[len(s)-1]
_, filename := path.Split(f.File)
return funcname, filename
},
}
}