added db connections and ip login
This commit is contained in:
@@ -11,3 +11,6 @@ scrap/
|
|||||||
*.sqlite3
|
*.sqlite3
|
||||||
*.db
|
*.db
|
||||||
init.sql
|
init.sql
|
||||||
|
.prettierignore
|
||||||
|
.vscode/
|
||||||
|
query.sql
|
||||||
|
|||||||
@@ -0,0 +1,41 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"log"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
_ "github.com/mattn/go-sqlite3"
|
||||||
|
)
|
||||||
|
|
||||||
|
// logs level=('debug', 'info', 'warning', 'error')
|
||||||
|
type logs struct {
|
||||||
|
level string
|
||||||
|
ip string
|
||||||
|
traceback string
|
||||||
|
date time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
type dbStruct struct {
|
||||||
|
db *sql.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
// InsertLog() : database method, only inserts level + traceback
|
||||||
|
func (app *dbStruct) InsertLog(lg logs) {
|
||||||
|
query := `INSERT INTO logs (level, traceback) VALUES (?, ?)`
|
||||||
|
|
||||||
|
_, err := app.db.Exec(query, lg.level, lg.traceback)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// InsertLog() : database method, logs level + Ip
|
||||||
|
func (app *dbStruct) LogIp(lg logs) {
|
||||||
|
query := `INSERT INTO logs (level, ip) VALUES (?, ?)`
|
||||||
|
|
||||||
|
_, err := app.db.Exec(query, lg.level, lg.ip)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,6 +3,6 @@ module acidburnmonkey/acidarchon
|
|||||||
go 1.26.2
|
go 1.26.2
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/joho/godotenv v1.5.1 // indirect
|
github.com/joho/godotenv v1.5.1
|
||||||
github.com/mattn/go-sqlite3 v1.14.44 // indirect
|
github.com/mattn/go-sqlite3 v1.14.44
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@@ -48,14 +49,22 @@ type TemolateData struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
godotenv.Load()
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
|
|
||||||
godotenv.Load()
|
db, err := sql.Open("sqlite3", "zum.db")
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("err opening db")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
database := &dbStruct{db: db}
|
||||||
|
|
||||||
// routes
|
// routes
|
||||||
mux.HandleFunc("/", handleRoot)
|
mux.HandleFunc("/", database.handleRoot)
|
||||||
mux.HandleFunc("/robots.txt", serveRobots)
|
mux.HandleFunc("/robots.txt", serveRobots)
|
||||||
mux.HandleFunc("/*", handleRoot)
|
mux.HandleFunc("/*", database.handleRoot)
|
||||||
|
|
||||||
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
|
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
|
||||||
|
|
||||||
@@ -64,10 +73,13 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Routes
|
// Routes
|
||||||
func handleRoot(w http.ResponseWriter, r *http.Request) {
|
|
||||||
|
// handleRoot() : part of dbStruct
|
||||||
|
func (app *dbStruct) handleRoot(w http.ResponseWriter, r *http.Request) {
|
||||||
templ, err := template.ParseFiles("templates/home.html")
|
templ, err := template.ParseFiles("templates/home.html")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "template not found", http.StatusInternalServerError)
|
http.Error(w, "template not found", http.StatusInternalServerError)
|
||||||
|
app.InsertLog(logs{level: "error", traceback: "template not found"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,6 +97,8 @@ func handleRoot(w http.ResponseWriter, r *http.Request) {
|
|||||||
dox := callApi(host)
|
dox := callApi(host)
|
||||||
dox2 := secondApi(host)
|
dox2 := secondApi(host)
|
||||||
|
|
||||||
|
app.LogIp(logs{level: "info", ip: dox.IP})
|
||||||
|
|
||||||
templ.Execute(w, TemolateData{Ip: dox, IP2: *dox2})
|
templ.Execute(w, TemolateData{Ip: dox, IP2: *dox2})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,5 @@ CREATE TABLE IF NOT EXISTS logs (
|
|||||||
level TEXT NOT NULL CHECK(level IN ('debug', 'info', 'warning', 'error')),
|
level TEXT NOT NULL CHECK(level IN ('debug', 'info', 'warning', 'error')),
|
||||||
ip TEXT,
|
ip TEXT,
|
||||||
traceback TEXT,
|
traceback TEXT,
|
||||||
date DATETIME,
|
date DATETIME DEFAULT (datetime('now', 'localtime')),
|
||||||
repeating INTEGER
|
);
|
||||||
);
|
|
||||||
|
|||||||
Reference in New Issue
Block a user