blob: 7483afc055bd4f84f4f38e61b43828b265d5e756 [file] [log] [blame]
Serge Bazanski56c888b2021-05-30 21:48:58 +00001package main
2
3import (
Serge Bazanski11b276d2021-07-11 23:49:55 +00004 "encoding/json"
Serge Bazanski56c888b2021-05-30 21:48:58 +00005 "fmt"
6 "html/template"
7 "net/http"
Serge Bazanski717aad42021-07-11 16:03:43 +00008 "strings"
Serge Bazanski56c888b2021-05-30 21:48:58 +00009
10 "github.com/golang/glog"
11
Serge Bazanski717aad42021-07-11 16:03:43 +000012 "code.hackerspace.pl/hscloud/hswaw/site/calendar"
Serge Bazanski56c888b2021-05-30 21:48:58 +000013 "code.hackerspace.pl/hscloud/hswaw/site/templates"
14)
15
16// parseTemplates parses a set of templates from
17// //hswaw/site/templates/$name.html into a Go HTML template. Typical Go text
18// templating ordering behaviour applies (this basically replicates
19// template.ParseFiles, but for statically embedded files instead).
20func parseTemplates(names ...string) (*template.Template, error) {
21 if len(names) == 0 {
22 return nil, fmt.Errorf("at least one template must be given")
23 }
24
25 var t *template.Template
26 for _, n := range names {
27 path := fmt.Sprintf("hswaw/site/templates/%s.html", n)
28 data, ok := templates.Data[path]
29 if !ok {
30 return nil, fmt.Errorf("template %q (%s) not found", n, path)
31 }
32 s := string(data)
33
34 if t == nil {
35 t = template.New(n)
36 }
37 _, err := t.Parse(s)
38 if err != nil {
39 return nil, fmt.Errorf("template %q could not be parsed: %w", n, err)
40 }
41 }
42 return t, nil
43}
44
45var (
Serge Bazanski4d7b2f02021-05-31 22:33:51 +000046 tmplIndex = template.Must(parseTemplates("index"))
Serge Bazanski56c888b2021-05-30 21:48:58 +000047)
48
49// render attempts to render a given Go template with data into the HTTP
50// response writer, and logs a warning if anything goes wrong.
51func render(w http.ResponseWriter, t *template.Template, data interface{}) {
52 if err := t.Execute(w, data); err != nil {
53 glog.Warningf("Rendering %v failed: %v", t, err)
54 }
55}
56
Serge Bazanski4d7b2f02021-05-31 22:33:51 +000057// handleIndex handles rendering the main page at /.
58func (s *service) handleIndex(w http.ResponseWriter, r *http.Request) {
Serge Bazanski15b5bc12022-11-06 17:04:16 +000059 // Necessary as "/" is a catch-all in net/http.
60 if r.URL.Path != "/" {
61 http.NotFound(w, r)
62 return
63 }
64
Serge Bazanskid2271de2021-07-11 21:26:37 +000065 ctx := r.Context()
66
67 atStatus, atError := getAt(ctx)
68
Serge Bazanski4d7b2f02021-05-31 22:33:51 +000069 render(w, tmplIndex, map[string]interface{}{
Serge Bazanskid2271de2021-07-11 21:26:37 +000070 "Entries": s.getFeeds(),
Serge Bazanski717aad42021-07-11 16:03:43 +000071 "Events": s.getEvents(),
Serge Bazanskid2271de2021-07-11 21:26:37 +000072 "AtStatus": atStatus,
73 "AtError": atError,
Serge Bazanski3c9092a2021-05-30 23:15:20 +000074 })
75}
Serge Bazanski11b276d2021-07-11 23:49:55 +000076
Serge Bazanski15b5bc12022-11-06 17:04:16 +000077func (s *service) handleRobotsTxt(w http.ResponseWriter, r *http.Request) {
78 // Allow everyone everywhere. We don't really host much on hackerspace.pl anyway.
79 fmt.Fprintf(w, "User-agent: *\n")
80 fmt.Fprintf(w, "Allow: /\n")
81}
82
Serge Bazanski717aad42021-07-11 16:03:43 +000083func (s *service) handleJSONEvents(w http.ResponseWriter, r *http.Request) {
84 w.Header().Set("Content-Type", "application/json")
85 json.NewEncoder(w).Encode(s.getEvents())
86}
87
88// handleEvent is a fallback HTML-only event render view.
89// TODO(q3k): make this pretty by either making a template or redirecting to a
90// pretty viewer.
91func (s *service) handleEvent(w http.ResponseWriter, r *http.Request) {
92 parts := strings.Split(r.URL.Path, "/")
93 uid := parts[len(parts)-1]
94
95 events := s.getEvents()
96 var event *calendar.UpcomingEvent
97 for _, ev := range events {
98 if ev.UID == uid {
99 event = ev
100 break
101 }
102 }
103 if event == nil {
104 http.NotFound(w, r)
105 return
106 }
107
108 render(w, template.Must(template.New("event").Parse(`<!DOCTYPE html>
109 <meta charset="utf-8">
110 <title>Event details: {{ .Summary }}</title>
111 <body>
112 <i>this interface intentionally left ugly...</i><br/>
113 <b>summary:</b> {{ .Summary }}<br />
114 <b>date:</b> {{ .WarsawDate }}<br />
115 <pre>{{ .Description }}</pre>`)), event)
116}
117
Serge Bazanski11b276d2021-07-11 23:49:55 +0000118func (s *service) handleSpaceAPI(w http.ResponseWriter, r *http.Request) {
119 ctx := r.Context()
120 w.Header().Set("Content-Type", "application/json")
121 json.NewEncoder(w).Encode(generateSpaceAPIResponse(ctx))
122}