-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebsite.go
89 lines (76 loc) · 2.08 KB
/
website.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
package main
import (
"crypto/rand"
"encoding/hex"
"html/template"
"log"
"net/http"
"github.com/gorilla/mux"
)
// PageData represents the data to be rendered in the template
type PageData struct {
Title string
APIKey string
}
// renderTemplate renders the HTML template with the given data
func renderTemplate(w http.ResponseWriter, tmpl string, data *PageData) {
tmplFile := tmpl + ".html"
t, err := template.ParseFiles(tmplFile)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = t.Execute(w, data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
// IndexHandler handles requests to the homepage
func IndexHandler(w http.ResponseWriter, r *http.Request) {
data := &PageData{
Title: "Home",
}
renderTemplate(w, "index", data)
}
// LoginHandler handles requests to the login page
func LoginHandler(w http.ResponseWriter, r *http.Request) {
data := &PageData{
Title: "Login",
}
renderTemplate(w, "login", data)
}
// SignupHandler handles requests to the signup page
func SignupHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
// TODO: Add code to process the signup form and save the user to the database
apiKey := generateAPIKey()
data := &PageData{
Title: "Welcome",
APIKey: apiKey,
}
renderTemplate(w, "welcome", data)
} else {
data := &PageData{
Title: "Signup",
}
renderTemplate(w, "signup", data)
}
}
// generateAPIKey generates a random API key for a user
func generateAPIKey() string {
key := make([]byte, 16)
rand.Read(key)
return hex.EncodeToString(key)
}
func main() {
r := mux.NewRouter()
// Serve static files (e.g., CSS, JS, images)
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
// Define routes
r.HandleFunc("/", IndexHandler).Methods("GET")
r.HandleFunc("/login", LoginHandler).Methods("GET")
r.HandleFunc("/signup", SignupHandler).Methods("GET", "POST")
// Start the server
log.Println("Server started on http://localhost:8000")
log.Fatal(http.ListenAndServe(":8000", r))
}