Serge Bazanski | a767467 | 2021-05-30 21:09:34 +0000 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "flag" |
| 5 | "fmt" |
| 6 | "mime" |
| 7 | "net/http" |
| 8 | "strings" |
Serge Bazanski | 3c9092a | 2021-05-30 23:15:20 +0000 | [diff] [blame^] | 9 | "sync" |
Serge Bazanski | a767467 | 2021-05-30 21:09:34 +0000 | [diff] [blame] | 10 | |
| 11 | "code.hackerspace.pl/hscloud/go/mirko" |
| 12 | "github.com/golang/glog" |
| 13 | |
| 14 | "code.hackerspace.pl/hscloud/hswaw/site/static" |
| 15 | ) |
| 16 | |
| 17 | var ( |
| 18 | flagSitePublic string |
| 19 | ) |
| 20 | |
| 21 | type service struct { |
Serge Bazanski | 3c9092a | 2021-05-30 23:15:20 +0000 | [diff] [blame^] | 22 | // feeds is a map from atom feed name to atom feed. This is updated by a |
| 23 | // background worker. |
| 24 | feeds map[string]*atomFeed |
| 25 | // feedsMu locks the feeds field. |
| 26 | feedsMu sync.RWMutex |
Serge Bazanski | a767467 | 2021-05-30 21:09:34 +0000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | func main() { |
| 30 | flag.StringVar(&flagSitePublic, "site_public", "0.0.0.0:8080", "Address at which to serve public HTTP requests") |
| 31 | flag.Parse() |
| 32 | |
| 33 | mi := mirko.New() |
| 34 | if err := mi.Listen(); err != nil { |
| 35 | glog.Exitf("Listen failed: %v", err) |
| 36 | } |
| 37 | |
| 38 | s := &service{} |
Serge Bazanski | 3c9092a | 2021-05-30 23:15:20 +0000 | [diff] [blame^] | 39 | go s.feedWorker(mi.Context()) |
Serge Bazanski | a767467 | 2021-05-30 21:09:34 +0000 | [diff] [blame] | 40 | |
| 41 | mux := http.NewServeMux() |
| 42 | s.registerHTTP(mux) |
| 43 | |
| 44 | go func() { |
| 45 | glog.Infof("Listening for public HTTP at %v", flagSitePublic) |
| 46 | if err := http.ListenAndServe(flagSitePublic, mux); err != nil { |
| 47 | glog.Exit(err) |
| 48 | } |
| 49 | }() |
| 50 | |
| 51 | if err := mi.Serve(); err != nil { |
| 52 | glog.Exitf("Serve failed: %v", err) |
| 53 | } |
| 54 | |
| 55 | <-mi.Done() |
| 56 | } |
| 57 | |
| 58 | func (s *service) handleHTTPStatic(w http.ResponseWriter, r *http.Request) { |
| 59 | path := strings.TrimPrefix(r.URL.Path, "/") |
| 60 | |
| 61 | staticPath := fmt.Sprintf("hswaw/site/%s", path) |
Serge Bazanski | a767467 | 2021-05-30 21:09:34 +0000 | [diff] [blame] | 62 | if data, ok := static.Data[staticPath]; ok { |
| 63 | parts := strings.Split(path, ".") |
| 64 | ext := fmt.Sprintf(".%s", parts[len(parts)-1]) |
| 65 | t := mime.TypeByExtension(ext) |
| 66 | w.Header().Set("Content-Type", t) |
| 67 | w.Write(data) |
| 68 | } else { |
| 69 | http.NotFound(w, r) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func (s *service) registerHTTP(mux *http.ServeMux) { |
| 74 | mux.HandleFunc("/static/", s.handleHTTPStatic) |
Serge Bazanski | 3c9092a | 2021-05-30 23:15:20 +0000 | [diff] [blame^] | 75 | mux.HandleFunc("/", s.handleMain) |
Serge Bazanski | 56c888b | 2021-05-30 21:48:58 +0000 | [diff] [blame] | 76 | mux.HandleFunc("/about", s.handleAbout) |
| 77 | mux.HandleFunc("/about_en", s.handleAboutEn) |
Serge Bazanski | a767467 | 2021-05-30 21:09:34 +0000 | [diff] [blame] | 78 | } |