simplification and addition of music page
crispy-caesus 114518720+crispy-caesus@users.noreply.github.com
Fri, 02 May 2025 00:48:52 +0200
8 files changed,
117 insertions(+),
32 deletions(-)
A
go.mod
@@ -0,0 +1,5 @@
+module crispy-website + +go 1.24.2 + +require github.com/mattn/go-sqlite3 v1.14.28
A
go.sum
@@ -0,0 +1,2 @@
+github.com/mattn/go-sqlite3 v1.14.28 h1:ThEiQrnbtumT+QMknw63Befp/ce/nUPgBPMlRFEum7A= +github.com/mattn/go-sqlite3 v1.14.28/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
M
index.go
→
index.go
@@ -1,49 +1,27 @@
package main import ( - "html/template" "log" "net/http" ) -type Page struct { - Title string - Body []byte -} - -var templates = template.Must(template.ParseFiles("templates/index.html")) - -func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) { - err := templates.ExecuteTemplate(w, tmpl+".html", p) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - } -} func viewHandler(w http.ResponseWriter, r *http.Request) { - title := r.URL.Path[len("/"):] // Extract title from URL path - if title == "" { - title = "Home" // Default title if none provided - } - p := &Page{Title: title} - renderTemplate(w, "index", p) + http.ServeFile(w, r, "templates/index.html") } -// New handler for plain text response func plainTextHandler(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "text/plain") // Set content type to plain text - w.Write([]byte("dh=b81129b4e9cc388c5ab63919550316fc3ca5ebe4")) // Write the plain text response + w.Header().Set("Content-Type", "text/plain") + w.Write([]byte("dh=b81129b4e9cc388c5ab63919550316fc3ca5ebe4")) } func main() { - // Serve static files from the "static" directory at the "/static/" path fs := http.FileServer(http.Dir("static")) http.Handle("/static/", http.StripPrefix("/static/", fs)) - // Set up the main route http.HandleFunc("/", viewHandler) + http.HandleFunc("/music", musicHandler) - // Set up a new route for plain text http.HandleFunc("/.well-known/discord", plainTextHandler) log.Fatal(http.ListenAndServe("0.0.0.0:8080", nil))
A
music.go
@@ -0,0 +1,17 @@
+package main + +import ( + "net/http" + "text/template" +) + +func musicHandler(w http.ResponseWriter, r *http.Request) { + results := loadMusic() + tmpl := template.Must(template.ParseFiles("templates/music.html")) + + err := tmpl.Execute(w, results) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } + +}
A
sql.go
@@ -0,0 +1,65 @@
+package main + +import ( + "database/sql" + "log" + + _ "github.com/mattn/go-sqlite3" +) + +type musicItem struct { + id int + external_ids sql.NullString + Name string + Artist string + price float64 + seller sql.NullString + note sql.NullString + purchase_date sql.NullString +} + +func openDB(DBName string) (*sql.DB, error) { + db, err := sql.Open("sqlite3", DBName+".db") + if err != nil { + panic("couldn't open db") + } + + return db, nil +} + +func loadMusic() []musicItem { + db, err := openDB("walletdrain") + if err != nil { + panic("couldn't open db") + } + defer db.Close() + + query := "SELECT * FROM music" + + rows, err := db.Query(query) + if err != nil { + panic(err) + } + defer rows.Close() + + var results []musicItem + for rows.Next() { + var result musicItem + err := rows.Scan( + &result.id, + &result.external_ids, + &result.Name, + &result.Artist, + &result.price, + &result.seller, + &result.note, + &result.purchase_date) + if err != nil { + log.Print(err) + } + + results = append(results, result) + } + return results +} +
M
templates/index.html
→
templates/index.html
@@ -45,7 +45,7 @@ <li>
<a title="Github" href="https://github.com/crispy-caesus" target="_blank" rel="noopener nofererrer">Github</a> - crispy-caesus <p> - Contains most of my little projects, like this website, as well as my dotfiles for my + Contains most of my little projects, as well as my dotfiles for my Linux configuration </p> </li>@@ -82,8 +82,7 @@ </p>
</li> <li> - <a title="toothbrushing music" href="https://bandcamp.com/crispy-caesus" target="_blank" - rel="noopener nofererrer"> + <a title="toothbrushing music" href="music"> toothbrushing music </a> <br />@@ -92,8 +91,6 @@ <p>
Over the past months I've created my little legally aquired library of DRM-free music. There are a bunch of reasons why I prefer this way of aquiring music in this way, most of which boils down to independence and artist support. - For now I'll just link my bandcamp here which should showcase most of it, I'll think about some - other solution somewhen maybe :D </p> </li> </ul>
A
templates/music.html
@@ -0,0 +1,20 @@
+<!doctype html> +<html lang="en"> + +<head> + <meta charset="UTF-8" /> + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> + <title>toothbrushing music</title> + <link rel="stylesheet" href="static/index.css" /> +</head> + +<body> + <h1>My bought music</h1> + <ul> + {{range .}} + <li>{{.Name}} - {{.Artist}}</li> + {{end}} + </ul> +</body> + +</html>