security/lib/db.go

67 lines
1.1 KiB
Go

package lib
import (
"database/sql"
"fmt"
"os"
_ "github.com/mattn/go-sqlite3"
)
var Db *sql.DB
const create_users string = `
CREATE TABLE IF NOT EXISTS users (
username TEXT NOT NULL UNIQUE,
password TEXT NOT NULL,
cookie TEXT NOT NULL
);
`
const create_vulns string = `
CREATE TABLE IF NOT EXISTS vulns (
id TEXT NOT NULL UNIQUE,
desc TEXT NOT NULL,
source TEXT NOT NULL,
severity TEXT NOT NULL,
package TEXT NOT NULL,
versions TEXT NOT NULL,
status TEXT NOT NULL,
message TEXT NOT NULL,
author TEXT NOT NULL,
date TEXT NOT NULL,
updated TEXT NOT NULL
);
`
func LoadDatabase() error {
_, err := os.Stat("db")
if err != nil && os.IsNotExist(err) {
err = os.Mkdir("db", os.ModePerm)
if err != nil {
return err
}
}
if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("cannot access to db directory")
}
Db, err = sql.Open("sqlite3", "db/data.db")
if err != nil {
return err
}
_, err = Db.Exec(create_users)
if err != nil {
return err
}
_, err = Db.Exec(create_vulns)
if err != nil {
return err
}
return nil
}