Serge Bazanski | a767467 | 2021-05-30 21:09:34 +0000 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
radex | a28fa4d | 2023-10-09 00:07:04 +0200 | [diff] [blame] | 4 | "context" |
Serge Bazanski | a767467 | 2021-05-30 21:09:34 +0000 | [diff] [blame] | 5 | "flag" |
| 6 | "fmt" |
Serge Bazanski | 717aad4 | 2021-07-11 16:03:43 +0000 | [diff] [blame] | 7 | "math/rand" |
Serge Bazanski | a767467 | 2021-05-30 21:09:34 +0000 | [diff] [blame] | 8 | "mime" |
| 9 | "net/http" |
Serge Bazanski | 1f06230 | 2021-06-01 22:24:34 +0000 | [diff] [blame] | 10 | "regexp" |
Serge Bazanski | a767467 | 2021-05-30 21:09:34 +0000 | [diff] [blame] | 11 | "strings" |
Serge Bazanski | 3c9092a | 2021-05-30 23:15:20 +0000 | [diff] [blame] | 12 | "sync" |
Serge Bazanski | 717aad4 | 2021-07-11 16:03:43 +0000 | [diff] [blame] | 13 | "time" |
Serge Bazanski | a767467 | 2021-05-30 21:09:34 +0000 | [diff] [blame] | 14 | |
Serge Bazanski | a767467 | 2021-05-30 21:09:34 +0000 | [diff] [blame] | 15 | "github.com/golang/glog" |
| 16 | |
Serge Bazanski | 717aad4 | 2021-07-11 16:03:43 +0000 | [diff] [blame] | 17 | "code.hackerspace.pl/hscloud/hswaw/site/calendar" |
Serge Bazanski | a767467 | 2021-05-30 21:09:34 +0000 | [diff] [blame] | 18 | "code.hackerspace.pl/hscloud/hswaw/site/static" |
| 19 | ) |
| 20 | |
| 21 | var ( |
| 22 | flagSitePublic string |
| 23 | ) |
| 24 | |
| 25 | type service struct { |
Serge Bazanski | 3c9092a | 2021-05-30 23:15:20 +0000 | [diff] [blame] | 26 | // feeds is a map from atom feed name to atom feed. This is updated by a |
| 27 | // background worker. |
| 28 | feeds map[string]*atomFeed |
| 29 | // feedsMu locks the feeds field. |
| 30 | feedsMu sync.RWMutex |
Serge Bazanski | 717aad4 | 2021-07-11 16:03:43 +0000 | [diff] [blame] | 31 | |
| 32 | // events is a list of upcoming events, sorted so the first event is the |
| 33 | // one that will happen the soonests. |
| 34 | events []*calendar.UpcomingEvent |
| 35 | // eventsMu locks the events field. |
| 36 | eventsMu sync.RWMutex |
Serge Bazanski | a767467 | 2021-05-30 21:09:34 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | func main() { |
| 40 | flag.StringVar(&flagSitePublic, "site_public", "0.0.0.0:8080", "Address at which to serve public HTTP requests") |
| 41 | flag.Parse() |
| 42 | |
Serge Bazanski | 717aad4 | 2021-07-11 16:03:43 +0000 | [diff] [blame] | 43 | rand.Seed(time.Now().UnixNano()) |
| 44 | |
radex | a28fa4d | 2023-10-09 00:07:04 +0200 | [diff] [blame] | 45 | // TODO(q3k): use sigint-interruptible context |
| 46 | ctx := context.Background() |
Serge Bazanski | a767467 | 2021-05-30 21:09:34 +0000 | [diff] [blame] | 47 | |
| 48 | s := &service{} |
radex | a28fa4d | 2023-10-09 00:07:04 +0200 | [diff] [blame] | 49 | go s.feedWorker(ctx) |
| 50 | go s.eventsWorker(ctx) |
Serge Bazanski | a767467 | 2021-05-30 21:09:34 +0000 | [diff] [blame] | 51 | |
| 52 | mux := http.NewServeMux() |
| 53 | s.registerHTTP(mux) |
| 54 | |
| 55 | go func() { |
| 56 | glog.Infof("Listening for public HTTP at %v", flagSitePublic) |
| 57 | if err := http.ListenAndServe(flagSitePublic, mux); err != nil { |
| 58 | glog.Exit(err) |
| 59 | } |
| 60 | }() |
| 61 | |
radex | a28fa4d | 2023-10-09 00:07:04 +0200 | [diff] [blame] | 62 | <-ctx.Done() |
Serge Bazanski | a767467 | 2021-05-30 21:09:34 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Serge Bazanski | 1f06230 | 2021-06-01 22:24:34 +0000 | [diff] [blame] | 65 | var ( |
| 66 | // staticRoutes define the resolution of static file paths into assets |
| 67 | // built into //hswaw/site/static, whose names correspond to their origin |
| 68 | // within the Bazel workspace. |
| 69 | // The regexp will be matched against the normalized within the URL. If it |
| 70 | // matches, its first group/submatch will be used to format the string |
| 71 | // corresponding to this regexp, and that string will be then used to |
| 72 | // retrieve a path embedded within //hswaw/site/static:static |
| 73 | // (go_embed_data). |
| 74 | // |
| 75 | // To see paths available within that go_embed_data, you can do: |
| 76 | // $ bazel build //hswaw/site/static:static |
| 77 | // $ grep -A100 'Data =' bazel-bin/hswaw/site/static/static.go |
| 78 | staticRoutes = map[*regexp.Regexp]string{ |
Serge Bazanski | 19c8b60 | 2022-02-01 09:36:47 +0000 | [diff] [blame] | 79 | regexp.MustCompile(`^static/site/(.+)$`): "hswaw/site/static/%s", |
| 80 | regexp.MustCompile(`^static/leaflet/(.+)$`): "external/com_npmjs_leaflet/package/dist/%s", |
| 81 | regexp.MustCompile(`^static/gfonts/([a-z]+)/([0-9]+)\.ttf$`): "external/com_gstatic_fonts_%s_%s/file/font.ttf", |
Serge Bazanski | 1f06230 | 2021-06-01 22:24:34 +0000 | [diff] [blame] | 82 | } |
| 83 | ) |
| 84 | |
| 85 | // handleHTTPStatic uses staticRoutes to serve static files embedded within |
| 86 | // //hswaw/site/static. |
Serge Bazanski | a767467 | 2021-05-30 21:09:34 +0000 | [diff] [blame] | 87 | func (s *service) handleHTTPStatic(w http.ResponseWriter, r *http.Request) { |
| 88 | path := strings.TrimPrefix(r.URL.Path, "/") |
Serge Bazanski | 1f06230 | 2021-06-01 22:24:34 +0000 | [diff] [blame] | 89 | for from, to := range staticRoutes { |
| 90 | match := from.FindStringSubmatch(path) |
| 91 | if match == nil { |
| 92 | continue |
| 93 | } |
Serge Bazanski | 19c8b60 | 2022-02-01 09:36:47 +0000 | [diff] [blame] | 94 | var args []interface{} |
| 95 | for _, m := range match[1:] { |
| 96 | args = append(args, m) |
| 97 | } |
| 98 | if len(args) != strings.Count(to, "%s") { |
| 99 | http.NotFound(w, r) |
| 100 | return |
| 101 | } |
| 102 | to := fmt.Sprintf(to, args...) |
Serge Bazanski | 1f06230 | 2021-06-01 22:24:34 +0000 | [diff] [blame] | 103 | data, ok := static.Data[to] |
| 104 | if !ok { |
| 105 | continue |
| 106 | } |
Serge Bazanski | a767467 | 2021-05-30 21:09:34 +0000 | [diff] [blame] | 107 | parts := strings.Split(path, ".") |
| 108 | ext := fmt.Sprintf(".%s", parts[len(parts)-1]) |
| 109 | t := mime.TypeByExtension(ext) |
| 110 | w.Header().Set("Content-Type", t) |
| 111 | w.Write(data) |
Serge Bazanski | 1f06230 | 2021-06-01 22:24:34 +0000 | [diff] [blame] | 112 | return |
| 113 | |
Serge Bazanski | a767467 | 2021-05-30 21:09:34 +0000 | [diff] [blame] | 114 | } |
Serge Bazanski | 1f06230 | 2021-06-01 22:24:34 +0000 | [diff] [blame] | 115 | http.NotFound(w, r) |
Serge Bazanski | a767467 | 2021-05-30 21:09:34 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | func (s *service) registerHTTP(mux *http.ServeMux) { |
| 119 | mux.HandleFunc("/static/", s.handleHTTPStatic) |
Serge Bazanski | 11b276d | 2021-07-11 23:49:55 +0000 | [diff] [blame] | 120 | mux.HandleFunc("/spaceapi", s.handleSpaceAPI) |
Serge Bazanski | 717aad4 | 2021-07-11 16:03:43 +0000 | [diff] [blame] | 121 | mux.HandleFunc("/events.json", s.handleJSONEvents) |
| 122 | mux.HandleFunc("/event/", s.handleEvent) |
Serge Bazanski | 15b5bc1 | 2022-11-06 17:04:16 +0000 | [diff] [blame] | 123 | mux.HandleFunc("/robots.txt", s.handleRobotsTxt) |
Serge Bazanski | 4d7b2f0 | 2021-05-31 22:33:51 +0000 | [diff] [blame] | 124 | mux.HandleFunc("/", s.handleIndex) |
Serge Bazanski | a767467 | 2021-05-30 21:09:34 +0000 | [diff] [blame] | 125 | } |