1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
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)
}
}
|