-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
124 lines (108 loc) · 3 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"encoding/json"
"html/template"
"log"
"net/http"
"os"
"path/filepath"
)
type Project struct {
Name string `json:"name"`
Stack string `json:"stack"`
Description string `json:"description"`
URL string `json:"url"`
ImagePath string `json:"image"`
}
type Course struct {
Name string `json:"name"`
Platform string `json:"platform"`
CompletionDate string `json:"completionDate"`
URL string `json:"url"`
}
func main() {
http.HandleFunc("/", homeHandler)
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
// Get the port from the environment variable
port := os.Getenv("PORT")
if port == "" {
port = "8080" // Default to port 8080 if PORT environment variable is not set
}
log.Printf("Starting server on :%s\n", port)
if err := http.ListenAndServe(":"+port, nil); err != nil {
log.Fatalf("could not start server: %s\n", err.Error())
}
}
// Custom function to extract the file extension
func ext(path string) string {
return filepath.Ext(path)
}
func parseTemplateFiles(filenames ...string) (*template.Template, error) {
// Create a FuncMap with your custom function
funcMap := template.FuncMap{
"ext": ext,
}
paths := make([]string, len(filenames))
for i, file := range filenames {
paths[i] = filepath.Join("templates", file)
}
// Parse the templates and apply the FuncMap
return template.New("").Funcs(funcMap).ParseFiles(paths...)
}
func homeHandler(w http.ResponseWriter, r *http.Request) {
tmpl, err := parseTemplateFiles("base.html", "index.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Printf("Error parsing template: %s\n", err.Error())
return
}
projects, err := loadProjects("projects.json")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Printf("Error loading projects: %s\n", err.Error())
return
}
courses, err := loadCourses("courses.json")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Printf("Error loading courses: %s\n", err.Error())
return
}
data := struct {
Title string
Projects []Project
Courses []Course
}{
Title: "RaShunda Williams Dev Portfolio | Remote",
Projects: projects,
Courses: courses,
}
if err := tmpl.ExecuteTemplate(w, "base.html", data); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Printf("Error executing template: %s\n", err.Error())
}
}
func loadProjects(filename string) ([]Project, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
var projects []Project
if err := json.NewDecoder(file).Decode(&projects); err != nil {
return nil, err
}
return projects, nil
}
func loadCourses(filename string) ([]Course, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
var courses []Course
if err := json.NewDecoder(file).Decode(&courses); err != nil {
return nil, err
}
return courses, nil
}