diff --git a/dhl.db b/dhl.db new file mode 100644 index 0000000..24a08da Binary files /dev/null and b/dhl.db differ diff --git a/go.mod b/go.mod index 69caf2f..5a072a3 100644 --- a/go.mod +++ b/go.mod @@ -4,4 +4,17 @@ go 1.25.0 require github.com/joho/godotenv v1.5.1 -require github.com/mattn/go-sqlite3 v1.14.34 // indirect +require ( + github.com/dustin/go-humanize v1.0.1 // indirect + github.com/google/uuid v1.6.0 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mattn/go-sqlite3 v1.14.34 // indirect + github.com/ncruces/go-strftime v1.0.0 // indirect + github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect + golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546 // indirect + golang.org/x/sys v0.37.0 // indirect + modernc.org/libc v1.67.6 // indirect + modernc.org/mathutil v1.7.1 // indirect + modernc.org/memory v1.11.0 // indirect + modernc.org/sqlite v1.46.1 // indirect +) diff --git a/go.sum b/go.sum index b3f44da..80d81d6 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,27 @@ +github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= +github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-sqlite3 v1.14.34 h1:3NtcvcUnFBPsuRcno8pUtupspG/GM+9nZ88zgJcp6Zk= github.com/mattn/go-sqlite3 v1.14.34/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= +github.com/ncruces/go-strftime v1.0.0 h1:HMFp8mLCTPp341M/ZnA4qaf7ZlsbTc+miZjCLOFAw7w= +github.com/ncruces/go-strftime v1.0.0/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= +golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546 h1:mgKeJMpvi0yx/sU5GsxQ7p6s2wtOnGAHZWCHUM4KGzY= +golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546/go.mod h1:j/pmGrbnkbPtQfxEe5D0VQhZC6qKbfKifgD0oM7sR70= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ= +golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= +modernc.org/libc v1.67.6 h1:eVOQvpModVLKOdT+LvBPjdQqfrZq+pC39BygcT+E7OI= +modernc.org/libc v1.67.6/go.mod h1:JAhxUVlolfYDErnwiqaLvUqc8nfb2r6S6slAgZOnaiE= +modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU= +modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg= +modernc.org/memory v1.11.0 h1:o4QC8aMQzmcwCK3t3Ux/ZHmwFPzE6hf2Y5LbkRs+hbI= +modernc.org/memory v1.11.0/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw= +modernc.org/sqlite v1.46.1 h1:eFJ2ShBLIEnUWlLy12raN0Z1plqmFX9Qe3rjQTKt6sU= +modernc.org/sqlite v1.46.1/go.mod h1:CzbrU2lSB1DKUusvwGz7rqEKIq+NUd8GWuBBZDs9/nA= diff --git a/main.go b/main.go index acfc8db..4475c14 100644 --- a/main.go +++ b/main.go @@ -1,24 +1,110 @@ package main -// https://stackoverflow.com/questions/61080317/go-lang-very-simple-http-post-requests-and-response-endpoint (simple post endpoint) -// https://stackoverflow.com/questions/16466320/is-there-a-way-to-do-repetitive-tasks-at-intervals -import ( - "log" - "net/http" +/* +Author: Valentijn van der Jagt - _ "github.com/mattn/go-sqlite3" +In this project i play around with go. This is my first time writing go, and im using a lot of help from sources. all useful sources are listed in the comments below. + +*/ + +// https://stackoverflow.com/questions/61080317/go-lang-very-simple-http-post-requests-and-response-endpoint, HTTP Server example +// https://stackoverflow.com/questions/16466320/is-there-a-way-to-do-repetitive-tasks-at-intervals, How timer intervals work in GoLang +// https://pkg.go.dev/modernc.org/sqlite#section-documentation, how to interface with specifically sqlite in GoLang +// https://github.com/mattn/go-sqlite3/blob/v1.14.34/_example/simple/simple.go, How to inferface with a database in GoLang. +import ( + "database/sql" + "log" + + _ "modernc.org/sqlite" ) -func checkRequestHeader(req *http.Request, res http.ResponseWriter, method string) bool { - if req.Method == method { - return true +func executeQuery(db *sql.DB, query string) { + _, err := db.Exec(query) + if err != nil { + log.Printf("%q: %s\n", err, query) + return } - res.WriteHeader(http.StatusMethodNotAllowed) - return false +} + +func createTables(db *sql.DB) { + executeQuery(db, ` + CREATE TABLE shipments ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + tracking_number TEXT NOT NULL UNIQUE, + service TEXT NOT NULL, + sender_name TEXT, + receiver_name TEXT, + origin_country TEXT, + origin_city TEXT, + origin_postal TEXT, + origin_address TEXT, + destination_country TEXT, + destination_city TEXT, + destination_postal TEXT, + destination_address TEXT, + pickup_date TEXT, + estimated_delivery_from TEXT, + estimated_delivery_to TEXT, + current_status TEXT, + last_update TEXT DEFAULT (datetime('now')), + created_at TEXT DEFAULT (datetime('now')) + ); + + -- Status Events: history of status updates + CREATE TABLE shipment_events ( + id INTEGER PRIMARY KEY, + shipment_id INTEGER NOT NULL, + event_time TEXT NOT NULL, + status_code TEXT NOT NULL, + status_desc TEXT, + description TEXT, + remark TEXT, + next_steps TEXT, + location_country TEXT, + location_city TEXT, + location_postal TEXT, + FOREIGN KEY (shipment_id) REFERENCES shipments(id) + ); + CREATE INDEX idx_events_shipment_time ON shipment_events(shipment_id, event_time); + + CREATE TABLE shipment_updates ( + id INTEGER PRIMARY KEY, + shipment_id INTEGER NOT NULL, + update_time TEXT DEFAULT (datetime('now')), + service TEXT, + location_country TEXT, + location_city TEXT, + status_code TEXT, + status_desc TEXT, + estimated_from TEXT, + estimated_to TEXT, + FOREIGN KEY (shipment_id) REFERENCES shipments(id) + ); + CREATE INDEX idx_updates_shipment_time ON shipment_updates(shipment_id, update_time); + `) + + log.Printf("[Database] tables created") + +} + +func dropTables(db *sql.DB) { + executeQuery(db, ` + DROP TABLE IF EXISTS shipments; + DROP TABLE IF EXISTS shipment_events; + DROP TABLE IF EXISTS shipment_updates; + `) + + log.Printf("[Database] tables dropped") + } func main() { + db, err := sql.Open("sqlite", "./dhl.db") + if err != nil { + log.Fatal(err) + } + defer db.Close() - log.Println("[Main] now serving 0.0.0.0:8090") - http.ListenAndServe(":8090", nil) + dropTables(db) // temporarely for debugging + createTables(db) }