Commit dc7e0ef7 authored by fengjie Lu's avatar fengjie Lu
Browse files

完成初始化数据库

parents
package app
import (
"github.com/labstack/echo/v4"
"github.com/sirupsen/logrus"
)
var e *echo.Echo
func StartServer() {
if err := e.start("127.0.0.1:8080"); err != nil {
logrus.Fatal(err)
}
}
package app
app_name = "Pastebin"
# possible values: DEBUG, INFO, WARNING, ERROR, FATAL
log_level = "DEBUG"
[mysql]
username = "root"
password = "Lfj123456."
db_name = "pastebin"
ip = "127.0.0.1"
port = "3306"
[redis]
ip = "127.0.0.1"
port = 6381
\ No newline at end of file
module Pastebin
go 1.19
require (
github.com/sirupsen/logrus v1.9.0
github.com/spf13/viper v1.14.0
gorm.io/driver/mysql v1.4.4
gorm.io/gorm v1.24.2
)
require (
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/spf13/afero v1.9.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
golang.org/x/sys v0.0.0-20220908164124-27713097b956 // indirect
golang.org/x/text v0.4.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
This diff is collapsed.
package main
import (
"Pastebin/app"
"Pastebin/model"
"github.com/sirupsen/logrus"
)
func main() {
logrus.SetReportCaller(true)
logrus.SetLevel(logrus.DebugLevel)
model.DBinit()
app.StartServer()
}
package model
import (
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
var DB *gorm.DB
func DBinit() {
ConnectDatabase()
err := DB.AutoMigrate(&LIST{})
if err != nil {
logrus.Fatal(err)
}
println("Connect Success!")
}
func ConnectDatabase() {
//read in config file
viper.SetConfigName("conf")
viper.SetConfigType("toml")
viper.AddConfigPath("./")
if err := viper.ReadInConfig(); err != nil {
logrus.Fatal(err)
}
println("Read in config success!")
//construct request
loginInfo := viper.GetStringMapString("mysql")
dbArgs := loginInfo["username"] + ":" + loginInfo["password"] + "@tcp(" +
loginInfo["ip"] + ":" + loginInfo["port"] + ")/" + loginInfo["db_name"] +
"?charset=utf8mb4&parseTime=True&loc=Local"
var err error
DB, err = gorm.Open(mysql.Open(dbArgs), &gorm.Config{})
if err != nil {
logrus.Fatal(err)
}
println("mysql connected!")
}
type LIST struct {
Id int `gorm:"primary_key;AUTO_INCREMENT"`
Name string
Password string
Type bool
Txt string
File []byte `gorm:"type:MediumBlob"`
ClickTime int
RemainTime int
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment