security/lib/db.go

67 lines
1.1 KiB
Go
Raw Normal View History

2024-03-21 20:20:24 +00:00
package lib
import (
"database/sql"
"fmt"
"os"
2024-08-13 19:25:36 +00:00
_ "github.com/mattn/go-sqlite3"
2024-03-21 20:20:24 +00:00
)
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 {
2024-08-13 19:25:36 +00:00
_, err := os.Stat("db")
if err != nil && os.IsNotExist(err) {
err = os.Mkdir("db", os.ModePerm)
if err != nil {
return err
}
}
2024-03-21 20:20:24 +00:00
2024-08-13 19:25:36 +00:00
if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("cannot access to db directory")
}
2024-03-21 20:20:24 +00:00
2024-08-13 19:25:36 +00:00
Db, err = sql.Open("sqlite3", "db/data.db")
if err != nil {
return err
}
2024-03-21 20:20:24 +00:00
2024-08-13 19:25:36 +00:00
_, err = Db.Exec(create_users)
if err != nil {
return err
}
2024-03-21 20:20:24 +00:00
2024-08-13 19:25:36 +00:00
_, err = Db.Exec(create_vulns)
if err != nil {
return err
}
2024-03-21 20:20:24 +00:00
2024-08-13 19:25:36 +00:00
return nil
}