Serge Bazanski | 56c888b | 2021-05-30 21:48:58 +0000 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
Serge Bazanski | 11b276d | 2021-07-11 23:49:55 +0000 | [diff] [blame] | 4 | "encoding/json" |
Serge Bazanski | 56c888b | 2021-05-30 21:48:58 +0000 | [diff] [blame] | 5 | "fmt" |
| 6 | "html/template" |
| 7 | "net/http" |
Serge Bazanski | 717aad4 | 2021-07-11 16:03:43 +0000 | [diff] [blame] | 8 | "strings" |
Serge Bazanski | 56c888b | 2021-05-30 21:48:58 +0000 | [diff] [blame] | 9 | |
| 10 | "github.com/golang/glog" |
| 11 | |
Serge Bazanski | 717aad4 | 2021-07-11 16:03:43 +0000 | [diff] [blame] | 12 | "code.hackerspace.pl/hscloud/hswaw/site/calendar" |
Serge Bazanski | 56c888b | 2021-05-30 21:48:58 +0000 | [diff] [blame] | 13 | "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). |
| 20 | func 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 | |
| 45 | var ( |
Serge Bazanski | 4d7b2f0 | 2021-05-31 22:33:51 +0000 | [diff] [blame] | 46 | tmplIndex = template.Must(parseTemplates("index")) |
Serge Bazanski | 56c888b | 2021-05-30 21:48:58 +0000 | [diff] [blame] | 47 | ) |
| 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. |
| 51 | func 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 Bazanski | 4d7b2f0 | 2021-05-31 22:33:51 +0000 | [diff] [blame] | 57 | // handleIndex handles rendering the main page at /. |
| 58 | func (s *service) handleIndex(w http.ResponseWriter, r *http.Request) { |
Serge Bazanski | 15b5bc1 | 2022-11-06 17:04:16 +0000 | [diff] [blame] | 59 | // Necessary as "/" is a catch-all in net/http. |
| 60 | if r.URL.Path != "/" { |
| 61 | http.NotFound(w, r) |
| 62 | return |
| 63 | } |
| 64 | |
Serge Bazanski | d2271de | 2021-07-11 21:26:37 +0000 | [diff] [blame] | 65 | ctx := r.Context() |
| 66 | |
| 67 | atStatus, atError := getAt(ctx) |
| 68 | |
Serge Bazanski | 4d7b2f0 | 2021-05-31 22:33:51 +0000 | [diff] [blame] | 69 | render(w, tmplIndex, map[string]interface{}{ |
Serge Bazanski | d2271de | 2021-07-11 21:26:37 +0000 | [diff] [blame] | 70 | "Entries": s.getFeeds(), |
Serge Bazanski | 717aad4 | 2021-07-11 16:03:43 +0000 | [diff] [blame] | 71 | "Events": s.getEvents(), |
Serge Bazanski | d2271de | 2021-07-11 21:26:37 +0000 | [diff] [blame] | 72 | "AtStatus": atStatus, |
| 73 | "AtError": atError, |
Serge Bazanski | 3c9092a | 2021-05-30 23:15:20 +0000 | [diff] [blame] | 74 | }) |
| 75 | } |
Serge Bazanski | 11b276d | 2021-07-11 23:49:55 +0000 | [diff] [blame] | 76 | |
Serge Bazanski | 15b5bc1 | 2022-11-06 17:04:16 +0000 | [diff] [blame] | 77 | func (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 Bazanski | 717aad4 | 2021-07-11 16:03:43 +0000 | [diff] [blame] | 83 | func (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. |
| 91 | func (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 Bazanski | 11b276d | 2021-07-11 23:49:55 +0000 | [diff] [blame] | 118 | func (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 | } |