forked from projectcamel/ggplayground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
106 lines (87 loc) · 2.9 KB
/
main.go
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
99
100
101
102
103
104
105
106
package main
import (
"context"
"crypto/md5"
"encoding/hex"
"fmt"
"log"
"net/http"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// Define a struct to represent the event doc in the database
type Event struct {
ID string `bson:"_id"`
Date time.Time `bson:"date"`
}
func main() {
http.HandleFunc("/", homeHandler)
http.HandleFunc("/event", eventHandler)
fmt.Println("Server started on http://localhost:8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
func homeHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
// Here's our HTML button - modify UI as desired
fmt.Fprintf(w, `<html>
<head>
<title>Event Button</title>
</head>
<body>
<h1>Event Button Demo</h1>
<button onclick="location.href='/event'">Click me!</button>
</body>
</html>`)
}
func eventHandler(w http.ResponseWriter, r *http.Request) {
// Generate the event ID hash (playing with MD5, this should be SHA)
eventID := generateEventID()
// Set the event date
eventDate := time.Now().AddDate(0, 1, 0) // Set the event date to one month from now
// Store the event in Mongo
err := storeEvent(eventID, eventDate)
if err != nil {
http.Error(w, "Failed to store event", http.StatusInternalServerError)
return
}
fmt.Fprintf(w, "Event button clicked! Event ID: %s, Date: %s", eventID, eventDate.String())
}
func generateEventID() string {
// Here's we're using the timestamp to generate the hash
timestamp := time.Now().String()
hash := md5.Sum([]byte(timestamp))
return hex.EncodeToString(hash[:])
}
func storeEvent(eventID string, eventDate time.Time) error {
// Set up Mongo listener
clientOptions := options.Client().ApplyURI("mongodb://localhost:27017")
client, err := mongo.Connect(context.Background(), clientOptions)
if err != nil {
return err
}
defer client.Disconnect(context.Background())
// Access the events collection in the "test" database
collection := client.Database("test").Collection("events")
// Create the event doc and add to the collection
event := Event{
ID: eventID,
Date: eventDate,
}
_, err = collection.InsertOne(context.Background(), event)
if err != nil {
return err
}
return nil
}
// couple hanging threads here I want to comment on:
// for the local test I also have to initialize the package using:
// go get go.mongodb.org/mongo-driver/mongo
// this is a CLI function so will need to test the equivalent on the domain servr
// we can repeat this template ad infinitum for other generations
// next hurdle will be connecting generations to the ID refs (eg. attach userID to eventID)
// not sure if it makes more sense to focus on filling out the struct for all generations first
// or to lock in the distributed hashing first so we don't have to refactor when ID storage changes