Compare commits
5 Commits
5288323831
..
prod
| Author | SHA1 | Date | |
|---|---|---|---|
| 9b59c36974 | |||
| 031d3006d8 | |||
| affe71d6dd | |||
| 6f54924a1f | |||
| 0d3515ad07 |
@@ -8,3 +8,9 @@ test.py
|
|||||||
binary
|
binary
|
||||||
tmp/
|
tmp/
|
||||||
scrap/
|
scrap/
|
||||||
|
*.sqlite3
|
||||||
|
*.db
|
||||||
|
init.sql
|
||||||
|
.prettierignore
|
||||||
|
.vscode/
|
||||||
|
query.sql
|
||||||
|
|||||||
+8
-1
@@ -1,10 +1,17 @@
|
|||||||
FROM golang:alpine AS builder
|
FROM golang:alpine AS builder
|
||||||
|
|
||||||
|
# go-sqlite3 is a cgo package, so it needs a C toolchain
|
||||||
|
RUN apk add --no-cache gcc musl-dev
|
||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
COPY go.mod go.sum ./
|
COPY go.mod go.sum ./
|
||||||
COPY *.go ./
|
RUN go mod download
|
||||||
|
|
||||||
|
COPY *.go ./
|
||||||
|
COPY seed.sql ./
|
||||||
|
|
||||||
|
ENV CGO_ENABLED=1
|
||||||
RUN go build -o server .
|
RUN go build -o server .
|
||||||
|
|
||||||
FROM alpine:latest
|
FROM alpine:latest
|
||||||
|
|||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
_ "embed"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
_ "github.com/mattn/go-sqlite3"
|
||||||
|
)
|
||||||
|
|
||||||
|
//go:embed seed.sql
|
||||||
|
var seedSQL string
|
||||||
|
|
||||||
|
// logs level=('debug', 'info', 'warning', 'error')
|
||||||
|
type logs struct {
|
||||||
|
level string
|
||||||
|
ip string
|
||||||
|
traceback string
|
||||||
|
date time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
type dbStruct struct {
|
||||||
|
db *sql.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
// Seed runs the schema in seed.sql. Idempotent via CREATE TABLE IF NOT EXISTS,
|
||||||
|
// so it's safe to call on every startup.
|
||||||
|
func (app *dbStruct) Seed() error {
|
||||||
|
_, err := app.db.Exec(seedSQL)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// InsertLog() : database method, only inserts level + traceback
|
||||||
|
func (app *dbStruct) InsertLog(lg logs) error {
|
||||||
|
query := `INSERT INTO logs (level, traceback) VALUES (?, ?)`
|
||||||
|
|
||||||
|
_, err := app.db.Exec(query, lg.level, lg.traceback)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// LogIp() : database method, logs level + Ip
|
||||||
|
func (app *dbStruct) LogIp(lg logs) error {
|
||||||
|
query := `INSERT INTO logs (level, ip) VALUES (?, ?)`
|
||||||
|
|
||||||
|
_, err := app.db.Exec(query, lg.level, lg.ip)
|
||||||
|
return err
|
||||||
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
services:
|
services:
|
||||||
website:
|
website:
|
||||||
env_file: .env
|
env_file: .env
|
||||||
|
environment:
|
||||||
|
DB_PATH: /app/data/zum.db
|
||||||
build:
|
build:
|
||||||
context: .
|
context: .
|
||||||
dockerfile: Dockerfile
|
dockerfile: Dockerfile
|
||||||
@@ -8,4 +10,6 @@ services:
|
|||||||
container_name: acidarchon.com
|
container_name: acidarchon.com
|
||||||
ports:
|
ports:
|
||||||
- 8088:8080
|
- 8088:8080
|
||||||
|
volumes:
|
||||||
|
- /dockers/acidarchon:/app/data
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
|||||||
@@ -2,4 +2,7 @@ module acidburnmonkey/acidarchon
|
|||||||
|
|
||||||
go 1.26.2
|
go 1.26.2
|
||||||
|
|
||||||
require github.com/joho/godotenv v1.5.1 // indirect
|
require (
|
||||||
|
github.com/joho/godotenv v1.5.1
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.44
|
||||||
|
)
|
||||||
|
|||||||
@@ -1,2 +1,4 @@
|
|||||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.44 h1:3VSe+xafpbzsLbdr2AWlAZk9yRHiBhTBakioXaCKTF8=
|
||||||
|
github.com/mattn/go-sqlite3 v1.14.44/go.mod h1:pjEuOr8IwzLJP2MfGeTb0A35jauH+C2kbHKBr7yXKVQ=
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"database/sql"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@@ -11,6 +12,7 @@ import (
|
|||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
|
_ "github.com/mattn/go-sqlite3"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ipResponse struct {
|
type ipResponse struct {
|
||||||
@@ -47,14 +49,32 @@ type TemolateData struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
godotenv.Load()
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
|
|
||||||
godotenv.Load()
|
dbPath := os.Getenv("DB_PATH")
|
||||||
|
if dbPath == "" {
|
||||||
|
dbPath = "zum.db"
|
||||||
|
}
|
||||||
|
|
||||||
|
db, err := sql.Open("sqlite3", dbPath)
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("err opening db")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
database := &dbStruct{db: db}
|
||||||
|
|
||||||
|
if err := database.Seed(); err != nil {
|
||||||
|
slog.Error("seeding db", "err", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// 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"))))
|
||||||
|
|
||||||
@@ -63,10 +83,15 @@ 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)
|
||||||
|
if logErr := app.InsertLog(logs{level: "error", traceback: "template not found"}); logErr != nil {
|
||||||
|
slog.Error("failed to write log", "err", logErr)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,6 +109,10 @@ func handleRoot(w http.ResponseWriter, r *http.Request) {
|
|||||||
dox := callApi(host)
|
dox := callApi(host)
|
||||||
dox2 := secondApi(host)
|
dox2 := secondApi(host)
|
||||||
|
|
||||||
|
if logErr := app.LogIp(logs{level: "info", ip: dox.IP}); logErr != nil {
|
||||||
|
slog.Error("failed to write log", "err", logErr)
|
||||||
|
}
|
||||||
|
|
||||||
templ.Execute(w, TemolateData{Ip: dox, IP2: *dox2})
|
templ.Execute(w, TemolateData{Ip: dox, IP2: *dox2})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,6 +146,9 @@ func callApi(ip string) ipResponse {
|
|||||||
|
|
||||||
err = json.Unmarshal(body, &values)
|
err = json.Unmarshal(body, &values)
|
||||||
|
|
||||||
|
slog.Debug("Api1 res:", "ip", values.IP, "country", values.Country, "continent:", values.ContinentCode,
|
||||||
|
"ISP", values.AsName)
|
||||||
|
|
||||||
return values
|
return values
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
CREATE TABLE IF NOT EXISTS logs (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
level TEXT NOT NULL CHECK(level IN ('debug', 'info', 'warning', 'error')),
|
||||||
|
ip TEXT,
|
||||||
|
traceback TEXT,
|
||||||
|
date DATETIME DEFAULT (datetime('now', 'localtime'))
|
||||||
|
);
|
||||||
+4
-1
@@ -15,9 +15,11 @@ h1 {
|
|||||||
|
|
||||||
.layout {
|
.layout {
|
||||||
display: flex;
|
display: flex;
|
||||||
gap: 40px;
|
|
||||||
align-items: flex-start;
|
align-items: flex-start;
|
||||||
padding: 0 40px;
|
padding: 0 40px;
|
||||||
|
gap: 40px;
|
||||||
|
width: 100%;
|
||||||
|
box-sizing: border-box;
|
||||||
}
|
}
|
||||||
|
|
||||||
.container {
|
.container {
|
||||||
@@ -28,6 +30,7 @@ h1 {
|
|||||||
width: 400px;
|
width: 400px;
|
||||||
height: 400px;
|
height: 400px;
|
||||||
flex-shrink: 0;
|
flex-shrink: 0;
|
||||||
|
margin: 0 auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
.info {
|
.info {
|
||||||
|
|||||||
+1
-1
@@ -45,8 +45,8 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div id="map"></div>
|
<div id="map"></div>
|
||||||
|
|
||||||
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
|
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
|
||||||
|
<!-- prettier-ignore -->
|
||||||
<script>
|
<script>
|
||||||
var map = L.map('map', { maxZoom: 10 }).setView([{{.IP2.Lat}}, {{.IP2.Lon}}], 8);
|
var map = L.map('map', { maxZoom: 10 }).setView([{{.IP2.Lat}}, {{.IP2.Lon}}], 8);
|
||||||
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
|
L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
|
||||||
|
|||||||
Reference in New Issue
Block a user