Skip to content

Commit

Permalink
tidy up router and some minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
parMaster committed May 1, 2024
1 parent 86cbf4b commit 4c6564a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 28 deletions.
59 changes: 34 additions & 25 deletions cmd/service/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,7 @@ func (s *Server) router() http.Handler {
m := s.authService.Middleware()
router.With(m.Auth).Get("/listMeetings", s.listMeetingsHandler)

router.With(m.Trace).Get("/", func(rw http.ResponseWriter, r *http.Request) {
// Check if user logged in
userInfo, err := token.GetUserInfo(r)
log.Printf("[DEBUG] userInfo: %+v", userInfo)
log.Printf("[DEBUG] err: %+v", err)
if err != nil || userInfo.Attributes["email"] == "" {
http.Redirect(rw, r, "/login", http.StatusFound)
return
}
s.responseWithFile("web/index.html", rw)
})
router.With(m.Trace).Get("/", s.indexPageHandler)

router.With(m.Auth).Route("/stats", func(r chi.Router) {
r.Get("/{divider}", s.statsHandler)
Expand All @@ -57,24 +47,17 @@ func (s *Server) router() http.Handler {

// Public routes
router.Get("/status", s.statusHandler)
router.Get("/watchMeeting/{accessKey}", s.watchMeetingHandler)

router.Get("/watch/{accessKey}", func(rw http.ResponseWriter, r *http.Request) {
accessKey := chi.URLParam(r, "accessKey")
if accessKey == "" {
rw.WriteHeader(http.StatusBadRequest)
return
}
s.responseWithFile("web/watch.html", rw)
})
router.Get("/watchMeeting/{accessKey}", s.watchMeetingHandler)
router.Get("/watch/{accessKey}", s.watchHandler)

router.Get("/login", func(rw http.ResponseWriter, r *http.Request) {
s.responseWithFile("web/auth.html", rw)
s.respondWithFile("web/auth.html", rw)
})

router.Get("/favicon.ico", func(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Content-Type", "image/x-icon")
s.responseWithFile("web/favicon.ico", rw)
s.respondWithFile("web/favicon.ico", rw)
})

fs := http.FileServer(http.Dir(s.cfg.Storage.Repository))
Expand All @@ -83,7 +66,30 @@ func (s *Server) router() http.Handler {
return router
}

func (s *Server) responseWithFile(file string, rw http.ResponseWriter) error {
// indexPageHandler serves / path (web/index.html)
func (s *Server) indexPageHandler(rw http.ResponseWriter, r *http.Request) {
// Check if user logged in
userInfo, err := token.GetUserInfo(r)
log.Printf("[DEBUG] userInfo: %+v", userInfo)
log.Printf("[DEBUG] err: %+v", err)
if err != nil || userInfo.Attributes["email"] == "" {
http.Redirect(rw, r, "/login", http.StatusFound)
return
}
s.respondWithFile("web/index.html", rw)
}

// watchHandler serves /watchHandler/{accessKey} path (web/watchHandler.html)
func (s *Server) watchHandler(rw http.ResponseWriter, r *http.Request) {
accessKey := chi.URLParam(r, "accessKey")
if accessKey == "" {
rw.WriteHeader(http.StatusBadRequest)
return
}
s.respondWithFile("web/watch.html", rw)
}

func (s *Server) respondWithFile(file string, rw http.ResponseWriter) error {
var html []byte
var err error
if s.cfg.Server.Dbg {
Expand Down Expand Up @@ -221,7 +227,7 @@ func (s *Server) listMeetingsHandler(rw http.ResponseWriter, r *http.Request) {
// mix in an accessKey for each meeting to be used in watchMeeting
for i := range m {

s := m[i].UUID + s.cfg.Server.AccessKeySalt
s := fmt.Sprintf("%s%s", m[i].UUID, s.cfg.Server.AccessKeySalt)
h := md5.New()
io.WriteString(h, s)
m[i].AccessKey = fmt.Sprintf("%x", h.Sum(nil))
Expand Down Expand Up @@ -343,7 +349,10 @@ func (s *Server) meetingsLoadedHandler(rw http.ResponseWriter, r *http.Request)
Meetings []string `json:"meetings"`
}
var uuids req
err := json.NewDecoder(r.Body).Decode(&uuids)
r.Body = http.MaxBytesReader(rw, r.Body, int64(1<<22)) // 4MB
decoder := json.NewDecoder(r.Body)
decoder.DisallowUnknownFields()
err := decoder.Decode(&uuids)
if err != nil {
log.Printf("[ERROR] failed to decode request body, %v", err)
rw.WriteHeader(http.StatusInternalServerError)
Expand Down
7 changes: 4 additions & 3 deletions repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func (r *Repository) DownloadOnce() error {
return errors.Join(fmt.Errorf("download returned error %s", queued.Id), downErr)
}

if downErr == nil && r.meetingRecordsLoaded(queued.MeetingId) && (r.cfg.Client.DeleteDownloaded || r.cfg.Client.TrashDownloaded) {
if r.meetingRecordsLoaded(queued.MeetingId) && (r.cfg.Client.DeleteDownloaded || r.cfg.Client.TrashDownloaded) {
err := r.client.DeleteMeetingRecordings(queued.MeetingId, r.cfg.Client.DeleteDownloaded)
if err != nil {
return errors.Join(fmt.Errorf("failed to delete meeting %s", queued.MeetingId), err)
Expand Down Expand Up @@ -262,7 +262,7 @@ func (r *Repository) DownloadRecord(record *model.Record) error {
log.Printf("[ERROR] failed to free up space, %v", err)
}

url := record.DownloadURL + "?access_token=" + token.AccessToken
url := fmt.Sprintf("%s?access_token=%s", record.DownloadURL, token.AccessToken)
resp, err := grab.Get(path, url)
if err != nil {
r.store.UpdateRecord(record.Id, model.StatusFailed, "")
Expand Down Expand Up @@ -404,7 +404,8 @@ func (r *Repository) requestMeetingsLoaded(meetings []string) (loaded bool, err
}

for _, instance := range r.cfg.Commander.Instances {
resp, err := http.Post(instance+"/meetingsLoaded/"+r.cfg.Server.AccessKeySalt, "application/json", bytes.NewBuffer(body))
url := fmt.Sprintf("%s/meetingsLoaded/%s", instance, r.cfg.Server.AccessKeySalt)
resp, err := http.Post(url, "application/json", bytes.NewBuffer(body))
if err != nil {
return false, fmt.Errorf("failed to post meetingsLoaded to %s, %v", instance, err)
}
Expand Down

0 comments on commit 4c6564a

Please sign in to comment.