-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
64 lines (50 loc) · 1.54 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
package main
import (
"cpe/calendar/handlers"
"html/template"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/gorilla/mux"
"github.com/joho/godotenv"
)
var tpl *template.Template
func init() {
// Load environment variables from .env file
godotenv.Load()
// Parse templates
tpl = template.Must(template.ParseFiles(filepath.Join("static", "index.html")))
}
func main() {
r := mux.NewRouter()
// Serve dynamic index page
r.HandleFunc("/", serveIndex).Methods("GET")
// Serve static files like JavaScript, CSS, images, etc.
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("static/"))))
// Serve calendar.ics route
r.HandleFunc("/your-cpe-calendar.ics", handlers.GenerateICSHandler).Methods("GET")
//validate route
r.HandleFunc("/validate", handlers.ValidateHandler).Methods("GET")
// check app health
r.HandleFunc("/health", handlers.Health).Methods("GET")
// Use the router in the http server
log.Fatal(http.ListenAndServe(":8080", r))
}
// serveIndex renders the index.html Go template with environment variables
func serveIndex(w http.ResponseWriter, r *http.Request) {
publicKey, _ := os.ReadFile(filepath.Join("static", "public.pem"))
publicKey = []byte(strings.ReplaceAll(string(publicKey), "\n", ""))
separator := os.Getenv("SEPARATOR")
data := struct {
PublicKey string
Separator string
}{
PublicKey: string(publicKey),
Separator: separator,
}
if err := tpl.Execute(w, data); err != nil {
http.Error(w, "Error rendering template", http.StatusInternalServerError)
}
}