2026-08-15 13:07:45 -07:00
|
|
|
// Package directory serves the school directory app.
|
|
|
|
|
package directory
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"html/template"
|
|
|
|
|
"log"
|
|
|
|
|
"net/http"
|
2026-08-15 18:08:31 -07:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"heliosian/internal/auth"
|
2026-08-15 13:07:45 -07:00
|
|
|
)
|
|
|
|
|
|
2026-08-15 22:07:34 -07:00
|
|
|
var sections = []string{"people", "classrooms", "my-family", "staff", "map", "email-list"}
|
2026-08-15 18:08:31 -07:00
|
|
|
|
|
|
|
|
var legacy = map[string]string{
|
|
|
|
|
"people": "/people",
|
|
|
|
|
"explore": "/classrooms",
|
|
|
|
|
"myfamily": "/my-family",
|
|
|
|
|
"staff": "/staff",
|
|
|
|
|
"map": "/map",
|
|
|
|
|
"emails": "/email-list",
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-15 13:07:45 -07:00
|
|
|
type app struct {
|
2026-08-15 16:22:25 -07:00
|
|
|
cache *Cache
|
2026-08-15 13:07:45 -07:00
|
|
|
}
|
|
|
|
|
|
2026-08-15 16:22:25 -07:00
|
|
|
func Register(mux *http.ServeMux, cache *Cache) {
|
|
|
|
|
a := app{cache: cache}
|
2026-08-15 18:08:31 -07:00
|
|
|
for _, section := range sections {
|
|
|
|
|
mux.HandleFunc("GET /"+section, a.page)
|
|
|
|
|
}
|
2026-08-15 22:36:19 -07:00
|
|
|
mux.HandleFunc("GET /profile", a.page)
|
2026-08-15 20:39:13 -07:00
|
|
|
mux.HandleFunc("GET /people/{email}", a.page)
|
|
|
|
|
mux.HandleFunc("GET /families/{key}", a.page)
|
2026-08-15 21:58:21 -07:00
|
|
|
mux.HandleFunc("GET /classrooms/{name}", a.page)
|
|
|
|
|
mux.HandleFunc("GET /grades/{name}", a.page)
|
2026-08-15 18:08:31 -07:00
|
|
|
mux.HandleFunc("GET /dl/", a.legacyRedirect)
|
|
|
|
|
mux.HandleFunc("GET /api/directory/model", a.model)
|
2026-08-15 13:07:45 -07:00
|
|
|
}
|
|
|
|
|
|
2026-08-15 18:08:31 -07:00
|
|
|
func (a app) legacyRedirect(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
first, _, _ := strings.Cut(strings.TrimPrefix(r.URL.Path, "/dl/"), "/")
|
|
|
|
|
target, ok := legacy[first]
|
|
|
|
|
if !ok {
|
|
|
|
|
target = "/people"
|
|
|
|
|
}
|
|
|
|
|
http.Redirect(w, r, target, http.StatusMovedPermanently)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a app) page(w http.ResponseWriter, r *http.Request) {
|
2026-08-15 13:07:45 -07:00
|
|
|
t, err := template.ParseFiles("web/directory/index.html")
|
|
|
|
|
if err != nil {
|
|
|
|
|
serverError(w, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-08-15 18:08:31 -07:00
|
|
|
name := a.cache.Model().DisplayName(auth.Email(r))
|
|
|
|
|
data := map[string]string{
|
|
|
|
|
"UserName": name,
|
|
|
|
|
"UserInitial": strings.ToUpper(name[:1]),
|
2026-08-15 22:36:19 -07:00
|
|
|
"UserEmail": auth.Email(r),
|
2026-08-15 18:08:31 -07:00
|
|
|
}
|
|
|
|
|
if err := t.Execute(w, data); err != nil {
|
|
|
|
|
log.Printf("[ERROR] render directory page: %v", err)
|
2026-08-15 13:07:45 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-15 16:22:25 -07:00
|
|
|
func (a app) model(w http.ResponseWriter, r *http.Request) {
|
2026-08-15 13:07:45 -07:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2026-08-15 16:22:25 -07:00
|
|
|
if err := json.NewEncoder(w).Encode(a.cache.Model()); err != nil {
|
|
|
|
|
log.Printf("[ERROR] encode model: %v", err)
|
2026-08-15 13:07:45 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func serverError(w http.ResponseWriter, err error) {
|
|
|
|
|
log.Printf("[ERROR] %v", err)
|
|
|
|
|
http.Error(w, "internal error", http.StatusInternalServerError)
|
|
|
|
|
}
|