package main import ( _ "embed" "fmt" "html/template" "log" "net/http" ) // File for persistent storage const DataFilename = "main.data" // layout for how the date should be output in html var DateLayout string = "02/01/2006 on 15:04" // template for listing out the ideas, should be bundled inside the executable // //go:embed ideas.html var ideas_html string // ToDo's // - [ ] Add a post // - [ ] Serialize data to a file // - [ ] Remove a post // - [x] Change the date format printing // - [ ] work with funcmaps in templates // - [ ] Put a reaction on a post // Represents an idea // CreatedAt is a formatted date string // out of 5 rating of the idea // formatted time string with DateLayout type Idea struct { Title string Description string Author string CreatedAt string } func main() { var ideas []Idea // Create a .data file in the cache directory { // data := os.Getenv("XDG_CACHE_HOME") // if data == "" { // data = "." // } // data = data + "" // // file, err := os.Create(data + "/" + DataFilename) // if err != nil { // panic(err) // } } // hardcoded data { ideas = []Idea{ { Title: "Better Keylogger", Description: "Keylogger which records statistics about your typing.", Author: "Me", CreatedAt: "26/09/2024 on 14:51", }, { Title: "music tracker", Description: "An app that can track music across multiple music apps such as spotify, apple music, deezer, etc.", Author: "Me", CreatedAt: "24/09/2024 on 14:32", }, { Title: "automated hosting", Description: "Service to host a website where people can drag&drop their files and they are hosted, it also provides a subdomain automatically.", Author: "Him", }, } } // // Example of parsing a string into time with the layout // CreatedAt: func() string { // t, _ := time.Parse(DateLayout, "24/09/2024 on 14:40") // return t.Format(DateLayout) tmpl, err := template.New("mytemplate").Parse(ideas_html) if err != nil { log.Fatalln(err) } http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { tmpl.Execute(w, struct{ Ideas []Idea }{ideas}) }) fmt.Println("Listening on http://localhost:8080") err = http.ListenAndServe(":8080", nil) if err != nil { log.Fatalln(err) } }