2023-12-02 00:27:32 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
2024-02-01 23:05:17 +02:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2023-12-02 00:27:32 +02:00
|
|
|
"html/template"
|
|
|
|
"log/slog"
|
|
|
|
"net/http"
|
2024-02-01 23:05:17 +02:00
|
|
|
"os"
|
2023-12-03 22:24:48 +02:00
|
|
|
"slices"
|
2023-12-03 22:36:42 +02:00
|
|
|
"strings"
|
2023-12-03 22:59:37 +02:00
|
|
|
"time"
|
2023-12-02 00:27:32 +02:00
|
|
|
)
|
|
|
|
|
2023-12-03 22:24:48 +02:00
|
|
|
//go:embed "static/css/*.css" "templates/*.html"
|
2023-12-02 00:27:32 +02:00
|
|
|
var static embed.FS
|
2023-12-03 22:24:48 +02:00
|
|
|
var templates map[string]*template.Template
|
2024-02-01 23:05:17 +02:00
|
|
|
var configFile string
|
2023-12-03 22:24:48 +02:00
|
|
|
|
|
|
|
func main() {
|
2024-02-01 23:05:17 +02:00
|
|
|
cfgFile := flag.String("config", "./data/resume.yaml", "Path to the configuration YAML")
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
_, err := readConfig(*cfgFile)
|
|
|
|
if err != nil {
|
|
|
|
slog.Error(fmt.Sprintf("error reading configuration: %s", err.Error()))
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
configFile = *cfgFile
|
2023-12-03 22:24:48 +02:00
|
|
|
staticFileServer := http.FileServer(http.FS(static))
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/", home)
|
|
|
|
mux.HandleFunc("/light", home)
|
|
|
|
mux.HandleFunc("/dark", home)
|
|
|
|
mux.Handle("/static/", staticFileServer)
|
|
|
|
|
|
|
|
slog.Info("Starting go-resume server, listening on port 3000")
|
2024-02-01 23:05:17 +02:00
|
|
|
err = http.ListenAndServe("0.0.0.0:3000", mux)
|
2023-12-03 22:24:48 +02:00
|
|
|
slog.Error(err.Error())
|
|
|
|
}
|
2023-12-02 00:27:32 +02:00
|
|
|
|
|
|
|
func home(w http.ResponseWriter, r *http.Request) {
|
2023-12-03 22:24:48 +02:00
|
|
|
acceptedPages := []string{"/", "/light", "/dark"}
|
|
|
|
if !slices.Contains(acceptedPages, r.URL.Path) {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
}
|
2023-12-02 00:27:32 +02:00
|
|
|
templateFiles := []string{
|
2023-12-03 22:24:48 +02:00
|
|
|
"templates/index.html",
|
|
|
|
"templates/metatag.html",
|
|
|
|
"templates/githubCorner.html",
|
2023-12-02 00:27:32 +02:00
|
|
|
}
|
2023-12-03 22:24:48 +02:00
|
|
|
tmpl, err := template.ParseFS(static, templateFiles...)
|
2023-12-02 00:27:32 +02:00
|
|
|
if err != nil {
|
|
|
|
slog.Error(err.Error())
|
|
|
|
http.Error(w, "Server error", http.StatusInternalServerError)
|
|
|
|
}
|
2024-02-01 23:05:17 +02:00
|
|
|
data, err := readConfig(configFile)
|
2023-12-03 22:24:48 +02:00
|
|
|
if err != nil {
|
|
|
|
slog.Error(err.Error())
|
|
|
|
http.Error(w, "Server error", http.StatusInternalServerError)
|
|
|
|
}
|
2023-12-03 22:36:42 +02:00
|
|
|
// TODO: Theme from browser
|
|
|
|
if strings.HasSuffix(r.URL.Path, "/light") || strings.HasSuffix(r.URL.Path, "/dark") {
|
|
|
|
urlSlice := strings.Split(r.URL.Path, "/")
|
|
|
|
data.Theme = urlSlice[len(urlSlice)-1]
|
|
|
|
}
|
2023-12-03 22:59:37 +02:00
|
|
|
data.Year = time.Now().Format("2006")
|
2024-02-01 23:05:17 +02:00
|
|
|
tmpl.Funcs(templateFunctions())
|
2023-12-03 22:24:48 +02:00
|
|
|
err = tmpl.Execute(w, *data)
|
2023-12-02 00:27:32 +02:00
|
|
|
if err != nil {
|
|
|
|
slog.Error(err.Error())
|
|
|
|
http.Error(w, "Server error", http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
2024-02-01 23:05:17 +02:00
|
|
|
|
|
|
|
func templateFunctions() template.FuncMap {
|
|
|
|
return template.FuncMap{
|
|
|
|
"html": func(raw string) template.HTML {
|
|
|
|
return template.HTML(raw)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|