package main import ( "context" "errors" // "database/sql" "fmt" "github.com/jackc/pgx/v5/pgxpool" "log" "os" "time" "github.com/gofiber/fiber/v2" "github.com/gofiber/template/html/v2" _ "github.com/jackc/pgx/v5/pgxpool" _ "github.com/lib/pq" ) type LogEntry struct { Eid int64 `json:"eid"` Loggedts int `json:"loggedts"` Server string `json:"server"` Timestamp int `json:"timestamp"` Content string `json:"content"` Method byte `json:"method"` } type ErrorLog struct { Server string `json:"server" xml:"server" form:"server"` Timestamp int `json:"timestamp" xml:"timestamp" form:"timestamp"` Error string `json:"error" xml:"error" form:"error"` } func main() { connStr := os.Getenv("DBCONNECTION") if (connStr == "") { log.Println("DBCONNECTION not set") log.Println("Should be something like:") log.Println("postgresql://user:password@server:5432/database?sslmode=disable") log.Fatalln("Exiting...") } // connStr := "postgresql://docker:docker@docker.lan:5432/errorlogger?sslmode=disable" // connStr := "postgresql://docker:docker@192.168.1.222:5432/errorlogger?sslmode=disable" // connStr := "postgresql://docker:docker@postgres:5432/errorlogger?sslmode=disable" db, err := pgxpool.New(context.Background(), connStr) if err != nil { log.Fatal(err) } else { log.Println("connected") } defer db.Close() engine := html.New("./views", ".html") app := fiber.New(fiber.Config{ Views: engine, }) app.Get("/", func(c *fiber.Ctx) error { return indexHandler(c) }) app.Post("/error", func(c *fiber.Ctx) error { return postErrorHandler(c, db) }) app.Post("/log", func(c *fiber.Ctx) error { return postLogHandler(c, db, 1) }) app.Get("/all", func(c *fiber.Ctx) error { return getAllLogs(c, db) }) port := os.Getenv("PORT") if port == "" { port = "2410" } app.Static("/", "./public") log.Fatalln(app.Listen(fmt.Sprintf(":%v", port))) } func indexHandler(c *fiber.Ctx) error { return c.Render("index", nil) } func postErrorHandler(c *fiber.Ctx, db *pgxpool.Pool) error { log.Println(c) t := time.Now() el := new(ErrorLog) if err := c.BodyParser(el); err != nil { log.Println(err) return err } log.Println(el.Server) log.Println(el.Timestamp) log.Println(el.Error) if el.Server != "" { log.Println("Writing to DB") r, err := db.Exec(context.Background(), "INSERT into logging VALUES (nextval('logging_eid_seq'::regclass), $1, $2, $3, $4, $5)", t.UnixMilli(), el.Server, el.Timestamp, el.Error, 254) if err != nil { log.Fatalf("An error occured while executing query: %v", err) } if r.RowsAffected() != 1 { return errors.New("No row affected...") } log.Println("***") } return c.Send(c.Body()) } func postLogHandler(c *fiber.Ctx, db *pgxpool.Pool, errorLevel int) error { log.Println(c) t := time.Now() el := new(ErrorLog) if err := c.BodyParser(el); err != nil { log.Println(err) return err } log.Println(el.Server) log.Println(el.Timestamp) log.Println(el.Error) if el.Server != "" { log.Println("Writing to DB") r, err := db.Exec(context.Background(), "INSERT into logging VALUES (nextval('logging_eid_seq'::regclass), $1, $2, $3, $4, $5)", t.UnixMilli(), el.Server, el.Timestamp, el.Error, errorLevel) if err != nil { log.Fatalf("An error occured while executing query: %v", err) } if r.RowsAffected() != 1 { return errors.New("No row affected...") } log.Println("***") } return c.Send(c.Body()) } func getAllLogs(c *fiber.Ctx, db *pgxpool.Pool) error { log.Println("Get All Logs") var entries []LogEntry rows,err := db.Query(context.Background(), "SELECT * from logging") defer rows.Close() if err != nil { log.Fatalln(err) c.JSON("An error occured") } for rows.Next() { var entry LogEntry if err := rows.Scan(&entry.Eid, &entry.Loggedts, &entry.Server, &entry.Timestamp, &entry.Content, &entry.Method); err != nil { return err } entries = append(entries, entry) } if err = rows.Err(); err != nil { log.Fatal(err) return c.JSON(nil) } return c.JSON(entries) }