added db connections and ip login
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -48,14 +49,22 @@ type TemolateData struct {
|
||||
}
|
||||
|
||||
func main() {
|
||||
godotenv.Load()
|
||||
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
|
||||
mux.HandleFunc("/", handleRoot)
|
||||
mux.HandleFunc("/", database.handleRoot)
|
||||
mux.HandleFunc("/robots.txt", serveRobots)
|
||||
mux.HandleFunc("/*", handleRoot)
|
||||
mux.HandleFunc("/*", database.handleRoot)
|
||||
|
||||
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
|
||||
|
||||
@@ -64,10 +73,13 @@ func main() {
|
||||
}
|
||||
|
||||
// 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")
|
||||
if err != nil {
|
||||
http.Error(w, "template not found", http.StatusInternalServerError)
|
||||
app.InsertLog(logs{level: "error", traceback: "template not found"})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -85,6 +97,8 @@ func handleRoot(w http.ResponseWriter, r *http.Request) {
|
||||
dox := callApi(host)
|
||||
dox2 := secondApi(host)
|
||||
|
||||
app.LogIp(logs{level: "info", ip: dox.IP})
|
||||
|
||||
templ.Execute(w, TemolateData{Ip: dox, IP2: *dox2})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user