43 lines
694 B
Go
43 lines
694 B
Go
package db
|
|
|
|
import (
|
|
"github.com/glebarez/sqlite"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
var (
|
|
db *gorm.DB
|
|
)
|
|
|
|
func InitDB() {
|
|
gdb, err := gorm.Open(sqlite.Open("data.db"))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
db = gdb.Debug()
|
|
|
|
err = db.Exec(createTable).Error
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func DB() *gorm.DB {
|
|
return db
|
|
}
|
|
|
|
var (
|
|
createTable = `
|
|
create table if not exists forward (
|
|
id integer primary key not null,
|
|
name varchar(60) not null,
|
|
local_ip text not null,
|
|
local_port integer not null,
|
|
target_addr text not null,
|
|
protocol integer default 0 not null,
|
|
status integer default 0 not null,
|
|
create_time datetime DEFAULT CURRENT_TIMESTAMP not null,
|
|
update_time datetime
|
|
)`
|
|
)
|