Serge Bazanski | a767467 | 2021-05-30 21:09:34 +0000 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "flag" |
| 5 | "fmt" |
Serge Bazanski | 717aad4 | 2021-07-11 16:03:43 +0000 | [diff] [blame] | 6 | "math/rand" |
Serge Bazanski | a767467 | 2021-05-30 21:09:34 +0000 | [diff] [blame] | 7 | "mime" |
| 8 | "net/http" |
Serge Bazanski | 1f06230 | 2021-06-01 22:24:34 +0000 | [diff] [blame] | 9 | "regexp" |
Serge Bazanski | a767467 | 2021-05-30 21:09:34 +0000 | [diff] [blame] | 10 | "strings" |
Serge Bazanski | 3c9092a | 2021-05-30 23:15:20 +0000 | [diff] [blame] | 11 | "sync" |
Serge Bazanski | 717aad4 | 2021-07-11 16:03:43 +0000 | [diff] [blame] | 12 | "time" |
Serge Bazanski | a767467 | 2021-05-30 21:09:34 +0000 | [diff] [blame] | 13 | |
| 14 | "code.hackerspace.pl/hscloud/go/mirko" |
| 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 | |
Serge Bazanski | a767467 | 2021-05-30 21:09:34 +0000 | [diff] [blame] | 45 | mi := mirko.New() |
| 46 | if err := mi.Listen(); err != nil { |
| 47 | glog.Exitf("Listen failed: %v", err) |
| 48 | } |
| 49 | |
| 50 | s := &service{} |
Serge Bazanski | 3c9092a | 2021-05-30 23:15:20 +0000 | [diff] [blame] | 51 | go s.feedWorker(mi.Context()) |
Serge Bazanski | 717aad4 | 2021-07-11 16:03:43 +0000 | [diff] [blame] | 52 | go s.eventsWorker(mi.Context()) |
Serge Bazanski | a767467 | 2021-05-30 21:09:34 +0000 | [diff] [blame] | 53 | |
| 54 | mux := http.NewServeMux() |
| 55 | s.registerHTTP(mux) |
| 56 | |
| 57 | go func() { |
| 58 | glog.Infof("Listening for public HTTP at %v", flagSitePublic) |
| 59 | if err := http.ListenAndServe(flagSitePublic, mux); err != nil { |
| 60 | glog.Exit(err) |
| 61 | } |
| 62 | }() |
| 63 | |
| 64 | if err := mi.Serve(); err != nil { |
| 65 | glog.Exitf("Serve failed: %v", err) |
| 66 | } |
| 67 | |
| 68 | <-mi.Done() |
| 69 | } |
| 70 | |
Serge Bazanski | 1f06230 | 2021-06-01 22:24:34 +0000 | [diff] [blame] | 71 | var ( |
| 72 | // staticRoutes define the resolution of static file paths into assets |
| 73 | // built into //hswaw/site/static, whose names correspond to their origin |
| 74 | // within the Bazel workspace. |
| 75 | // The regexp will be matched against the normalized within the URL. If it |
| 76 | // matches, its first group/submatch will be used to format the string |
| 77 | // corresponding to this regexp, and that string will be then used to |
| 78 | // retrieve a path embedded within //hswaw/site/static:static |
| 79 | // (go_embed_data). |
| 80 | // |
| 81 | // To see paths available within that go_embed_data, you can do: |
| 82 | // $ bazel build //hswaw/site/static:static |
| 83 | // $ grep -A100 'Data =' bazel-bin/hswaw/site/static/static.go |
| 84 | staticRoutes = map[*regexp.Regexp]string{ |
| 85 | regexp.MustCompile(`^static/site/(.+)$`): "hswaw/site/static/%s", |
| 86 | regexp.MustCompile(`^static/leaflet/(.+)$`): "external/com_npmjs_leaflet/package/dist/%s", |
| 87 | } |
| 88 | ) |
| 89 | |
| 90 | // handleHTTPStatic uses staticRoutes to serve static files embedded within |
| 91 | // //hswaw/site/static. |
Serge Bazanski | a767467 | 2021-05-30 21:09:34 +0000 | [diff] [blame] | 92 | func (s *service) handleHTTPStatic(w http.ResponseWriter, r *http.Request) { |
| 93 | path := strings.TrimPrefix(r.URL.Path, "/") |
Serge Bazanski | 1f06230 | 2021-06-01 22:24:34 +0000 | [diff] [blame] | 94 | for from, to := range staticRoutes { |
| 95 | match := from.FindStringSubmatch(path) |
| 96 | if match == nil { |
| 97 | continue |
| 98 | } |
| 99 | to := fmt.Sprintf(to, match[1]) |
| 100 | data, ok := static.Data[to] |
| 101 | if !ok { |
| 102 | continue |
| 103 | } |
Serge Bazanski | a767467 | 2021-05-30 21:09:34 +0000 | [diff] [blame] | 104 | parts := strings.Split(path, ".") |
| 105 | ext := fmt.Sprintf(".%s", parts[len(parts)-1]) |
| 106 | t := mime.TypeByExtension(ext) |
| 107 | w.Header().Set("Content-Type", t) |
| 108 | w.Write(data) |
Serge Bazanski | 1f06230 | 2021-06-01 22:24:34 +0000 | [diff] [blame] | 109 | return |
| 110 | |
Serge Bazanski | a767467 | 2021-05-30 21:09:34 +0000 | [diff] [blame] | 111 | } |
Serge Bazanski | 1f06230 | 2021-06-01 22:24:34 +0000 | [diff] [blame] | 112 | http.NotFound(w, r) |
Serge Bazanski | a767467 | 2021-05-30 21:09:34 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | func (s *service) registerHTTP(mux *http.ServeMux) { |
| 116 | mux.HandleFunc("/static/", s.handleHTTPStatic) |
Serge Bazanski | 11b276d | 2021-07-11 23:49:55 +0000 | [diff] [blame] | 117 | mux.HandleFunc("/spaceapi", s.handleSpaceAPI) |
Serge Bazanski | 717aad4 | 2021-07-11 16:03:43 +0000 | [diff] [blame] | 118 | mux.HandleFunc("/events.json", s.handleJSONEvents) |
| 119 | mux.HandleFunc("/event/", s.handleEvent) |
Serge Bazanski | 4d7b2f0 | 2021-05-31 22:33:51 +0000 | [diff] [blame] | 120 | mux.HandleFunc("/", s.handleIndex) |
Serge Bazanski | a767467 | 2021-05-30 21:09:34 +0000 | [diff] [blame] | 121 | } |