server.go (1116B)
1 package main 2 3 import ( 4 "errors" 5 "fmt" 6 "log" 7 "net/http" 8 "path" 9 "strings" 10 11 "olowe.co/x/atom" 12 ) 13 14 type Server struct { 15 client *Client 16 } 17 18 func (srv *Server) handleReq(w http.ResponseWriter, r *http.Request) { 19 log.Println(r.Method, r.URL) 20 workspace := strings.TrimPrefix(path.Dir(r.URL.String()), "/") 21 repo := path.Base(r.URL.String()) 22 prs, err := srv.client.PullRequests(workspace, repo) 23 if errors.Is(err, errNotExist) { 24 http.NotFound(w, r) 25 return 26 } else if err != nil { 27 msg := fmt.Sprintf("get pull requests for %s/%s: %v", workspace, repo, err) 28 log.Println(msg) 29 http.Error(w, msg, http.StatusInternalServerError) 30 return 31 } 32 feed := Feed(prs) 33 feed.Title = fmt.Sprintf("%s/%s pull requests", workspace, repo) 34 feed.ID = fmt.Sprintf("https://bitbucket.org/%s/%s", workspace, repo) 35 feed.Link = []atom.Link{{feed.ID, "alternate", "text/html"}} 36 b, err := atom.Marshal(feed) 37 if err != nil { 38 msg := fmt.Sprintf("marshal atom feed: %v", err) 39 log.Println(msg) 40 http.Error(w, msg, http.StatusInternalServerError) 41 return 42 } 43 w.Header().Set("Content-Type", "application/atom+xml") 44 w.Write(b) 45 }