-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
61 lines (48 loc) · 1.29 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
package main
import (
"fmt"
"log"
"net/http"
"time"
"github.com/todobackend/handlers"
"github.com/akrylysov/algnhsa"
"github.com/astaxie/beego/orm"
_ "github.com/go-sql-driver/mysql"
"github.com/spf13/cast"
"github.com/spf13/viper"
)
var (
ENV string
)
func init() {
setUpViper()
registerDatabase()
}
func main() {
http.HandleFunc("/todo/user", handlers.UserHandler)
http.HandleFunc("/todo/task", handlers.TaskHandler)
fmt.Println("Start...")
if ENV == "dev" {
_ = http.ListenAndServe(":3000", nil)
}
algnhsa.ListenAndServe(http.DefaultServeMux, nil)
}
//function to register the database to beego orm
func registerDatabase() {
ENV = cast.ToString(viper.Get("runmode"))
mysql := viper.Get(ENV + ".mysql").(map[string]interface{})
mysqlConf := mysql["user"].(string) + ":" + mysql["password"].(string) + "@tcp(" + mysql["host"].(string) + ")/" + mysql["database"].(string)
log.Println("conf", mysqlConf)
orm.RegisterDataBase("default", "mysql", mysqlConf)
orm.DefaultTimeLoc, _ = time.LoadLocation("Asia/Kolkata")
orm.Debug = true
}
//set up config file from conf folder
func setUpViper() {
viper.AddConfigPath("./conf")
viper.SetConfigName("env")
if err := viper.ReadInConfig(); err != nil {
fmt.Printf("Error reading config file, %s", err)
}
viper.SetEnvPrefix("global")
}