-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from bzz/add-server
web server instead of batch report generation
- Loading branch information
Showing
9 changed files
with
890 additions
and
295 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,262 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"html/template" | ||
"log" | ||
"net/http" | ||
"os" | ||
"sort" | ||
|
||
"github.com/bzz/scholar-alert-digest/gmailutils" | ||
"github.com/bzz/scholar-alert-digest/gmailutils/token" | ||
mrkdwn "github.com/bzz/scholar-alert-digest/markdown" | ||
"github.com/bzz/scholar-alert-digest/papers" | ||
|
||
"gitlab.com/golang-commonmark/markdown" | ||
"golang.org/x/oauth2" | ||
"golang.org/x/oauth2/google" | ||
"google.golang.org/api/gmail/v1" | ||
) | ||
|
||
var ( // templates | ||
layout = ` | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>{{ template "title" }}</title> | ||
<link rel="icon" href="http://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/240/apple/232/page-with-curl_1f4c3.png"> | ||
</head> | ||
<body>{{ template "body" . }}</body> | ||
</html> | ||
` | ||
chooseLabelsForm = ` | ||
{{ define "title"}}Chose a label{{ end }} | ||
{{ define "body" }} | ||
<p>Please, chosse a Gmail label to aggregate:</p> | ||
<form action="/labels" method="POST"> | ||
{{ range . }} | ||
<div> | ||
<input type="radio" id="{{.}}" name="label" value="{{.}}"> | ||
<label for="{{.}}">{{.}}</label> | ||
</div> | ||
{{ end }} | ||
<input type="submit" value="Select Label"/> | ||
</form> | ||
{{ end }} | ||
` | ||
|
||
newMdTemplText = `# Google Scholar Alert Digest | ||
**Date**: {{.Date}} | ||
**Unread emails**: {{.UnreadEmails}} | ||
**Paper titles**: {{.TotalPapers}} | ||
**Uniq paper titles**: {{.UniqPapers}} | ||
## New papers | ||
{{ range $paper := sortedKeys .Papers }} | ||
- [{{ .Title }}]({{ .URL }}) ({{index $.Papers .}}) | ||
{{- if .Abstract.FirstLine }} | ||
<details> | ||
<summary>{{.Abstract.FirstLine}}</summary>{{.Abstract.Rest}} | ||
</details> | ||
{{ end }} | ||
{{ end }} | ||
` | ||
) | ||
|
||
var ( // configuration | ||
addr = "localhost:8080" | ||
oauthCfg = &oauth2.Config{ | ||
// from https://console.developers.google.com/project/<your-project-id>/apiui/credential | ||
ClientID: os.Getenv("SAD_GOOGLE_ID"), | ||
ClientSecret: os.Getenv("SAD_GOOGLE_SECRET"), | ||
RedirectURL: "http://localhost:8080/login/authorized", | ||
Endpoint: google.Endpoint, | ||
Scopes: []string{gmail.GmailReadonlyScope}, | ||
} | ||
) | ||
|
||
func main() { | ||
// TODO(bzz): | ||
// - configure the log level, to include requests in debug | ||
// - add default req timeouts + throttling, to prevent abuse | ||
|
||
log.Printf("starting the web server at http://%s", addr) | ||
defer log.Printf("stoping the web server") | ||
|
||
// TODO(bzz): migrate to chi-router | ||
mux := http.NewServeMux() | ||
mux.HandleFunc("/", handleRoot) | ||
mux.HandleFunc("/labels", handleLabels) | ||
mux.HandleFunc("/login", handleLogin) | ||
mux.HandleFunc("/login/authorized", handleAuth) | ||
|
||
http.ListenAndServe(addr, sessionMiddleware(mux)) | ||
} | ||
|
||
func handleRoot(w http.ResponseWriter, r *http.Request) { | ||
// get token, stored in context by middleware (from cookies) | ||
tok, authorized := token.FromContext(r.Context()) | ||
if !authorized { // TODO(bzz): move this to middleware | ||
log.Printf("Redirecting to /login as three is no session") | ||
http.Redirect(w, r, "/login", http.StatusFound) | ||
return | ||
} | ||
|
||
gmailLabel, hasLabel := token.LabelFromContext(r.Context()) | ||
if !hasLabel { | ||
log.Printf("Redirecting to /labels as there is no label") | ||
http.Redirect(w, r, "/labels", http.StatusFound) | ||
return | ||
} | ||
|
||
// fetch messages | ||
srv, _ := gmail.New(oauthCfg.Client(r.Context(), tok)) // ignore as client != nil | ||
urMsgs, err := gmailutils.Fetch(r.Context(), srv, "me", fmt.Sprintf("label:%s is:unread", gmailLabel)) | ||
if err != nil { | ||
// TODO(bzz): token expiration looks ugly here and must be handled elsewhere | ||
w.WriteHeader(http.StatusServiceUnavailable) | ||
w.Write([]byte(err.Error())) | ||
return | ||
} | ||
|
||
// aggregate | ||
errCnt, urTitlesCnt, urTitles := papers.ExtractPapersFromMsgs(urMsgs) | ||
if errCnt != 0 { | ||
log.Printf("%d errors found, extracting the papers", errCnt) | ||
} | ||
|
||
// render: generate Md, render to HTML | ||
var mdBuf bytes.Buffer | ||
mrkdwn.GenerateMd(&mdBuf, newMdTemplText, "", len(urMsgs), urTitlesCnt, urTitles, nil) | ||
|
||
htmlTemplText := `<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<link rel="icon" href="http://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/240/apple/232/page-with-curl_1f4c3.png"> | ||
<title>Scholar Alert Digest</title> | ||
</head> | ||
<body>%s</body> | ||
</html> | ||
` | ||
md := markdown.New(markdown.XHTMLOutput(true), markdown.HTML(true)) | ||
w.Write([]byte(fmt.Sprintf(htmlTemplText, md.RenderToString([]byte(mdBuf.String()))))) | ||
|
||
// TODO(bzz): | ||
// add spniner! fetching takes ~20 sec easy | ||
// use html/tmeplate tempate | ||
|
||
// tmpl := template.Must( // render combination of the nested templates | ||
// template.Must( | ||
// template.New("papers-list").Parse(layout)). | ||
// Parse(papersListPage)) | ||
// err = tmpl.Execute(w, papers) | ||
// if err != nil { | ||
// log.Printf("Failed to render a papersList template: %v", err) | ||
// } | ||
} | ||
|
||
func handleLabels(w http.ResponseWriter, r *http.Request) { | ||
tok, authorized := token.FromContext(r.Context()) | ||
if !authorized { // TODO(bzz): move this to middleware | ||
http.Redirect(w, r, "/login", http.StatusFound) | ||
return | ||
} | ||
|
||
switch r.Method { | ||
case http.MethodGet: | ||
fetchLabelsAndServeForm(w, r, tok) | ||
case http.MethodPost: | ||
saveLabelToCookies(w, r) | ||
http.Redirect(w, r, "/", http.StatusFound) | ||
} | ||
} | ||
|
||
func fetchLabelsAndServeForm(w http.ResponseWriter, r *http.Request, tok *oauth2.Token) { | ||
labelsResp, err := gmailutils.FetchLabels(r.Context(), oauthCfg, tok) | ||
if err != nil { | ||
log.Printf("Unable to retrieve all labels: %v", err) | ||
w.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
var labels []string // user labels, sorted | ||
for _, l := range labelsResp.Labels { | ||
if l.Type == "system" { | ||
continue | ||
} | ||
labels = append(labels, l.Name) | ||
} | ||
sort.Strings(labels) | ||
|
||
tmpl := template.Must( // render combination of the nested templates | ||
template.Must( | ||
template.New("choose-label").Parse(layout)). | ||
Parse(chooseLabelsForm)) | ||
err = tmpl.Execute(w, labels) | ||
if err != nil { | ||
log.Printf("Failed to render a template: %v", err) | ||
} | ||
} | ||
|
||
func saveLabelToCookies(w http.ResponseWriter, r *http.Request) { | ||
err := r.ParseForm() | ||
if err != nil { // url query part is not valid | ||
log.Printf("Unable to parse query string: %v", err) | ||
w.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
humanLabel := r.FormValue("label") | ||
label := gmailutils.FormatAsID(humanLabel) | ||
|
||
cookie := token.NewLabelCookie(label) | ||
log.Printf("Saving new cookie: %s", cookie.String()) | ||
http.SetCookie(w, cookie) | ||
} | ||
|
||
func handleLogin(w http.ResponseWriter, r *http.Request) { | ||
// the URL which shows the Google Auth page to the user | ||
url := oauthCfg.AuthCodeURL("") | ||
http.Redirect(w, r, url, http.StatusFound) | ||
} | ||
|
||
func handleAuth(w http.ResponseWriter, r *http.Request) { | ||
// get the code from URL | ||
err := r.ParseForm() | ||
if err != nil { // url query part is not valid | ||
log.Printf("Unable to parse query string: %v", err) | ||
w.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
// exchange the received code for a bearer token | ||
code := r.FormValue("code") | ||
tok, err := oauthCfg.Exchange(r.Context(), code) | ||
if err != nil { | ||
log.Printf("Unable to exchange the code %q for token: %v", code, err) | ||
w.WriteHeader(http.StatusBadRequest) | ||
return | ||
} | ||
|
||
// save token in the session cookie | ||
cookie := token.NewSessionCookie(tok) | ||
log.Printf("Saving new cookie: %s", cookie.String()) | ||
http.SetCookie(w, cookie) | ||
|
||
http.Redirect(w, r, "/", http.StatusFound) | ||
} | ||
|
||
// sessionMiddleware reads token from session cookie, saves it into the Context. | ||
func sessionMiddleware(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
log.Println(r.Method, "-", r.RequestURI /*, r.Cookies()*/) // TODO(bzz): make cookies debug level only | ||
ctx := token.NewSessionContext(r.Context(), r.Cookies()) | ||
ctx = token.NewLabelContext(ctx, r.Cookies()) | ||
next.ServeHTTP(w, r.WithContext(ctx)) | ||
}) | ||
} |
Oops, something went wrong.