-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
91 lines (79 loc) · 2.21 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
package main
import (
"context"
"database/sql"
"fmt"
"log"
"net/http"
"github.com/aWildProgrammer/fconf"
_ "github.com/lib/pq"
"github.com/solenovex/it/common"
"github.com/solenovex/it/controller"
"github.com/solenovex/it/middleware"
_ "net/http/pprof"
// lhh
_ "github.com/go-sql-driver/mysql"
)
// const (
//
// host = ""
// port = 5432
// user = ""
// password = ""
// dbname = "demo"
//
// )
type mysql_Conf struct {
host string
port string
user string
password string
dbname string
}
func runOrBuildLoadConfig() (mysql_res mysql_Conf) {
// 使用库 go get github.com/aWildProgrammer/fconf
c, err := fconf.NewFileConf("./conf/conf_mysql.ini")
if err != nil {
panic(err)
}
// fmt.Println(c.Int("mysql.port"))
mysql_res.host = c.String("mysql.host")
mysql_res.port = c.String("mysql.port")
mysql_res.user = c.String("mysql.user")
mysql_res.password = c.String("mysql.password")
mysql_res.dbname = c.String("mysql.database")
// fmt.Printf("%v \n", mysql_res)
return
}
func init() {
var err error
res := runOrBuildLoadConfig() // 读取配置文件
// fmt.Printf("%v \n", res)
// connStr := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=require", host, port, user, password, dbname)
// common.Db, err = sql.Open("postgres", connStr)
// common.Db, err = sql.Open("mysql", "root:root@tcp(127.0.0.1:3309)/test")
str_sql := res.user + ":" + res.password + "@" + "tcp(" + res.host + ":" + res.port + ")/" + res.dbname
fmt.Println(str_sql)
common.Db, err = sql.Open("mysql", str_sql)
if err != nil {
log.Fatalln(err.Error())
}
ctx := context.Background()
err = common.Db.PingContext(ctx)
if err != nil {
log.Fatalln(err.Error())
}
log.Println("Connected!")
}
func main() {
server := http.Server{
Addr: ":8080",
Handler: &middleware.BasicAuthMiddleware{},
}
// http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./wwwroot")))) // 自定义 css link 不起作用
http.Handle("/wwwroot/static/", http.StripPrefix("/wwwroot/static/", http.FileServer(http.Dir("wwwroot/static/")))) // 有效
controller.RegisterRoutes()
log.Println("Server starting...")
go http.ListenAndServe(":8000", nil)
server.ListenAndServe()
}