/
base.go
79 lines (71 loc) · 1.4 KB
/
base.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
package main
import (
"database/sql"
"fmt"
"github.com/gin-gonic/gin"
_ "github.com/go-sql-driver/mysql"
"github.com/naoina/toml"
"html/template"
"io/ioutil"
"net/http"
"os"
)
type ShowMessage interface {
ShowMessage(c *gin.Context)
}
type msg struct {
msg string
}
type umsg struct {
msg string
url string
}
/*
Show message with template
*/
func (m *msg) ShowMessage(c *gin.Context) {
c.HTML(http.StatusOK, "message.html", gin.H{
"message": template.HTML(m.msg),
})
}
func (m *umsg) ShowMessage(c *gin.Context) {
c.HTML(http.StatusOK, "message.html", gin.H{
"message": template.HTML(m.msg),
"url": m.url,
})
}
func GetDB(config *appConfig) *sql.DB {
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4",
config.Db_user, config.Db_password, config.Db_host, config.Db_port, config.Db_name))
if err != nil {
panic(err.Error())
}
db.SetMaxIdleConns(50)
db.SetMaxOpenConns(100)
return db
}
type appConfig struct {
Db_host string
Db_port int
Db_name string
Db_user string
Db_password string
Admin_user string
Admin_password string
}
func GetConfig() *appConfig {
f, err := os.Open("config.toml")
if err != nil {
panic(err)
}
defer f.Close()
buf, err := ioutil.ReadAll(f)
if err != nil {
panic(err)
}
var config appConfig
if err := toml.Unmarshal(buf, &config); err != nil {
panic(err)
}
return &config
}