Skip to content

Commit

Permalink
replace generic -production flag with specific flags
Browse files Browse the repository at this point in the history
It's better to be explicit and clear with the flags. Instead of having
one catch-all -production flag that controls many things, replace it
with three specific, targeted flags. The new flags include -store-dir,
-analytics-file, and -secure-cookie.

Also rename -statefile flag to -state-file for consistency.

This change updates home to follow more modern best practices,
and makes it consistent with other, newer codebases.
  • Loading branch information
dmitshur committed Mar 17, 2019
1 parent ce682e8 commit 2ffc368
Show file tree
Hide file tree
Showing 17 changed files with 78 additions and 103 deletions.
5 changes: 2 additions & 3 deletions about.go
Expand Up @@ -21,13 +21,12 @@ import (

var aboutHTML = template.Must(template.New("").Parse(`<html>
<head>
<title>Dmitri Shuralyov - About</title>
{{.AnalyticsHTML}} <title>Dmitri Shuralyov - About</title>
<link href="/icon.png" rel="icon" type="image/png">
<meta name="viewport" content="width=device-width">
<link href="/assets/fonts/fonts.css" rel="stylesheet" type="text/css">
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
<link href="/assets/about/style.css" rel="stylesheet" type="text/css">
{{if .Production}}` + googleAnalytics + `{{end}}
</head>
<body>
<div style="max-width: 800px; margin: 0 auto 100px auto;">`))
Expand All @@ -39,7 +38,7 @@ func initAbout(notifications notifications.Service, users users.Service) {
}

w.Header().Set("Content-Type", "text/html; charset=utf-8")
data := struct{ Production bool }{*productionFlag}
data := struct{ AnalyticsHTML template.HTML }{analyticsHTML}
err := aboutHTML.Execute(w, data)
if err != nil {
return err
Expand Down
10 changes: 3 additions & 7 deletions blog.go
Expand Up @@ -22,7 +22,7 @@ import (

var blogHTML = template.Must(template.New("").Parse(`<html>
<head>
<title>Dmitri Shuralyov - Blog</title>
{{.AnalyticsHTML}} <title>Dmitri Shuralyov - Blog</title>
<link href="/icon.png" rel="icon" type="image/png">
<meta name="viewport" content="width=device-width">
<link href="/assets/fonts/fonts.css" rel="stylesheet" type="text/css">
Expand All @@ -33,7 +33,6 @@ var blogHTML = template.Must(template.New("").Parse(`<html>
</style>
<link href="/assets/blog/style.css" rel="stylesheet" type="text/css">
<script async src="/assets/blog/blog.js"></script>
{{if .Production}}` + googleAnalytics + `{{end}}
</head>
<body>`))

Expand All @@ -47,7 +46,7 @@ func initBlog(mux *http.ServeMux, issuesService issues.Service, blog issues.Repo
opt := issuesapp.Options{
Notifications: notifications,

HeadPre: `<title>Dmitri Shuralyov - Blog</title>
HeadPre: analyticsHTML + `<title>Dmitri Shuralyov - Blog</title>
<link href="/icon.png" rel="icon" type="image/png">
<meta name="viewport" content="width=device-width">
<link href="/assets/fonts/fonts.css" rel="stylesheet" type="text/css">
Expand Down Expand Up @@ -137,9 +136,6 @@ func initBlog(mux *http.ServeMux, issuesService issues.Service, blog issues.Repo
<div style="max-width: 800px; margin: 0 auto 100px auto;">`,
}
if *productionFlag {
opt.HeadPre += "\n\t\t" + googleAnalytics
}
opt.BodyTop = func(req *http.Request) ([]htmlg.Component, error) {
authenticatedUser, err := users.GetAuthenticated(req.Context())
if err != nil {
Expand Down Expand Up @@ -208,7 +204,7 @@ func initBlog(mux *http.ServeMux, issuesService issues.Service, blog issues.Repo
}

w.Header().Set("Content-Type", "text/html; charset=utf-8")
data := struct{ Production bool }{*productionFlag}
data := struct{ AnalyticsHTML template.HTML }{analyticsHTML}
err := blogHTML.Execute(w, data)
if err != nil {
return err
Expand Down
5 changes: 1 addition & 4 deletions changes.go
Expand Up @@ -74,7 +74,7 @@ func initChanges(mux *http.ServeMux, changeService change.Service, issueCounter
opt := changes.Options{
Notifications: notifications,

HeadPre: `<link href="/icon.png" rel="icon" type="image/png">
HeadPre: analyticsHTML + `<link href="/icon.png" rel="icon" type="image/png">
<meta name="viewport" content="width=device-width">
<link href="/assets/fonts/fonts.css" rel="stylesheet" type="text/css">
<style type="text/css">
Expand Down Expand Up @@ -119,9 +119,6 @@ func initChanges(mux *http.ServeMux, changeService change.Service, issueCounter
</style>`,
BodyPre: `<div style="max-width: 800px; margin: 0 auto 100px auto;">`,
}
if *productionFlag {
opt.HeadPre += "\n\t\t" + googleAnalytics
}
opt.BodyTop = func(req *http.Request, st common.State) ([]htmlg.Component, error) {
authenticatedUser, err := users.GetAuthenticated(req.Context())
if err != nil {
Expand Down
27 changes: 13 additions & 14 deletions commit.go
Expand Up @@ -47,12 +47,11 @@ type commitHandler struct {

var commitHTML = template.Must(template.New("").Parse(`<html>
<head>
<title>{{.FullName}} - Commit {{.Hash}}</title>
{{.AnalyticsHTML}} <title>{{.FullName}} - Commit {{.Hash}}</title>
<link href="/icon.png" rel="icon" type="image/png">
<meta name="viewport" content="width=device-width">
<link href="/assets/fonts/fonts.css" rel="stylesheet" type="text/css">
<link href="/assets/commit/style.css" rel="stylesheet" type="text/css">
{{if .Production}}` + googleAnalytics + `{{end}}
</head>
<body>
Expand Down Expand Up @@ -129,13 +128,13 @@ func (h *commitHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) erro

w.Header().Set("Content-Type", "text/html; charset=utf-8")
err = commitHTML.Execute(w, struct {
Production bool
FullName string
Hash string
AnalyticsHTML template.HTML
FullName string
Hash string
}{
Production: *productionFlag,
FullName: "Repository " + path.Base(h.Repo.Spec),
Hash: shortSHA(c.CommitHash),
AnalyticsHTML: analyticsHTML,
FullName: "Repository " + path.Base(h.Repo.Spec),
Hash: shortSHA(c.CommitHash),
})
if err != nil {
return err
Expand Down Expand Up @@ -258,13 +257,13 @@ func (h *commitHandlerPkg) ServeHTTP(w http.ResponseWriter, req *http.Request) e
fullName = "Package " + h.Dir.Package.Name
}
err = commitHTML.Execute(w, struct {
Production bool
FullName string
Hash string
AnalyticsHTML template.HTML
FullName string
Hash string
}{
Production: *productionFlag,
FullName: fullName,
Hash: shortSHA(c.CommitHash),
AnalyticsHTML: analyticsHTML,
FullName: fullName,
Hash: shortSHA(c.CommitHash),
})
if err != nil {
return err
Expand Down
19 changes: 9 additions & 10 deletions commits.go
Expand Up @@ -43,13 +43,12 @@ type commitsHandler struct {

var commitsHTML = template.Must(template.New("").Parse(`<html>
<head>
<title>{{.FullName}} - History</title>
{{.AnalyticsHTML}} <title>{{.FullName}} - History</title>
<link href="/icon.png" rel="icon" type="image/png">
<meta name="viewport" content="width=device-width">
<link href="/assets/fonts/fonts.css" rel="stylesheet" type="text/css">
<link href="/assets/commits/style.css" rel="stylesheet" type="text/css">
<script async src="/assets/commits/commits.js"></script>
{{if .Production}}` + googleAnalytics + `{{end}}
</head>
<body>`))

Expand Down Expand Up @@ -90,11 +89,11 @@ func (h *commitsHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) err

w.Header().Set("Content-Type", "text/html; charset=utf-8")
err = commitsHTML.Execute(w, struct {
Production bool
FullName string
AnalyticsHTML template.HTML
FullName string
}{
Production: *productionFlag,
FullName: "Repository " + path.Base(h.Repo.Spec),
AnalyticsHTML: analyticsHTML,
FullName: "Repository " + path.Base(h.Repo.Spec),
})
if err != nil {
return err
Expand Down Expand Up @@ -187,11 +186,11 @@ func (h *commitsHandlerPkg) ServeHTTP(w http.ResponseWriter, req *http.Request)
fullName = "Package " + h.Dir.Package.Name
}
err = commitsHTML.Execute(w, struct {
Production bool
FullName string
AnalyticsHTML template.HTML
FullName string
}{
Production: *productionFlag,
FullName: fullName,
AnalyticsHTML: analyticsHTML,
FullName: fullName,
})
if err != nil {
return err
Expand Down
5 changes: 2 additions & 3 deletions idiomaticgo.go
Expand Up @@ -16,7 +16,7 @@ import (

var idiomaticGoHTML = template.Must(template.New("").Parse(`<html>
<head>
<title>Idiomatic Go</title>
{{.AnalyticsHTML}} <title>Idiomatic Go</title>
<link href="/icon.png" rel="icon" type="image/png">
<meta name="viewport" content="width=device-width">
<link href="/assets/fonts/fonts.css" rel="stylesheet" type="text/css">
Expand All @@ -27,7 +27,6 @@ var idiomaticGoHTML = template.Must(template.New("").Parse(`<html>
</style>
<link href="/assets/idiomaticgo/style.css" rel="stylesheet" type="text/css">
<script async src="/assets/idiomaticgo/idiomaticgo.js"></script>
{{if .Production}}` + googleAnalytics + `{{end}}
</head>
<body>`))

Expand All @@ -38,7 +37,7 @@ func initIdiomaticGo(issues issues.Service, notifications notifications.Service,
}

w.Header().Set("Content-Type", "text/html; charset=utf-8")
data := struct{ Production bool }{*productionFlag}
data := struct{ AnalyticsHTML template.HTML }{analyticsHTML}
err := idiomaticGoHTML.Execute(w, data)
if err != nil {
return err
Expand Down
5 changes: 2 additions & 3 deletions index.go
Expand Up @@ -29,13 +29,12 @@ import (

var indexHTML = template.Must(template.New("").Parse(`<html>
<head>
<title>Dmitri Shuralyov</title>
{{.AnalyticsHTML}} <title>Dmitri Shuralyov</title>
<link href="/icon.png" rel="icon" type="image/png">
<meta name="viewport" content="width=device-width">
<link href="/assets/fonts/fonts.css" rel="stylesheet" type="text/css">
<link href="/assets/index/style.css" rel="stylesheet" type="text/css">
<link href="https://github.com/dmitshur" rel="me">
{{if .Production}}` + googleAnalytics + `{{end}}
</head>
<body>
<div style="max-width: 800px; margin: 0 auto 100px auto;">`))
Expand Down Expand Up @@ -65,7 +64,7 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) error
return nil
}

data := struct{ Production bool }{*productionFlag}
data := struct{ AnalyticsHTML template.HTML }{analyticsHTML}
err := indexHTML.Execute(w, data)
if err != nil {
return err
Expand Down
5 changes: 1 addition & 4 deletions issues.go
Expand Up @@ -70,7 +70,7 @@ func initIssues(mux *http.ServeMux, issuesService issues.Service, changeCounter
opt := issuesapp.Options{
Notifications: notifications,

HeadPre: `<link href="/icon.png" rel="icon" type="image/png">
HeadPre: analyticsHTML + `<link href="/icon.png" rel="icon" type="image/png">
<meta name="viewport" content="width=device-width">
<link href="/assets/fonts/fonts.css" rel="stylesheet" type="text/css">
<style type="text/css">
Expand Down Expand Up @@ -114,9 +114,6 @@ func initIssues(mux *http.ServeMux, issuesService issues.Service, changeCounter
</style>`,
BodyPre: `<div style="max-width: 800px; margin: 0 auto 100px auto;">`,
}
if *productionFlag {
opt.HeadPre += "\n\t\t" + googleAnalytics
}
opt.BodyTop = func(req *http.Request) ([]htmlg.Component, error) {
authenticatedUser, err := users.GetAuthenticated(req.Context())
if err != nil {
Expand Down
35 changes: 23 additions & 12 deletions main.go
Expand Up @@ -5,6 +5,8 @@ import (
"context"
"flag"
"fmt"
"html/template"
"io/ioutil"
"log"
"mime"
"net/http"
Expand All @@ -26,9 +28,15 @@ import (
)

var (
httpFlag = flag.String("http", ":8080", "Listen for HTTP connections on this address.")
productionFlag = flag.Bool("production", false, "Production mode.")
statefileFlag = flag.String("statefile", "", "File to save/load state (file is deleted after loading).")
httpFlag = flag.String("http", ":8080", "Listen for HTTP connections on this address.")
secureCookieFlag = flag.Bool("secure-cookie", false, "Value of cookie attribute Secure.")
storeDirFlag = flag.String("store-dir", filepath.Join(os.TempDir(), "home-store"), "Directory of home store (required).")
stateFileFlag = flag.String("state-file", "", "Optional path to file to save/load state (file is deleted after loading).")
analyticsFileFlag = flag.String("analytics-file", "", "Optional path to file containing analytics HTML to insert at the beginning of <head>.")
)

var (
analyticsHTML template.HTML // Set early in run.
)

func main() {
Expand All @@ -42,23 +50,26 @@ func main() {
cancel()
}()

err := run(ctx)
err := run(ctx, *storeDirFlag, *stateFileFlag, *analyticsFileFlag)
if err != nil {
log.Fatalln(err)
}
}

func run(ctx context.Context) error {
func run(ctx context.Context, storeDir, stateFile, analyticsFile string) error {
if err := mime.AddExtensionType(".md", "text/markdown"); err != nil {
return err
}
if err := mime.AddExtensionType(".woff2", "font/woff2"); err != nil {
return err
}

storeDir := filepath.Join(os.Getenv("HOME"), "Dropbox", "Store")
if !*productionFlag {
storeDir = filepath.Join(os.TempDir(), "home-store")
if analyticsFile != "" {
b, err := ioutil.ReadFile(analyticsFile)
if err != nil {
return err
}
analyticsHTML = template.HTML(b)
}

initStores := func(storeDir string) error {
Expand Down Expand Up @@ -244,8 +255,8 @@ func run(ctx context.Context) error {
staticFiles.ServeHTTP(w, req)
})

if *statefileFlag != "" {
err := global.LoadAndRemove(*statefileFlag)
if stateFile != "" {
err := global.LoadAndRemove(stateFile)
global.mu.Lock()
n := len(global.sessions)
global.mu.Unlock()
Expand All @@ -271,8 +282,8 @@ func run(ctx context.Context) error {

log.Println("Ended HTTP server.")

if *statefileFlag != "" {
err := global.Save(*statefileFlag)
if stateFile != "" {
err := global.Save(stateFile)
log.Println("sessions.Save:", err)
}

Expand Down
5 changes: 1 addition & 4 deletions notifications.go
Expand Up @@ -62,7 +62,7 @@ func initNotifications(mux *http.ServeMux, root webdav.FileSystem, users users.S

// Register notifications app endpoints.
opt := notificationsapp.Options{
HeadPre: `<title>Notifications</title>
HeadPre: analyticsHTML + `<title>Notifications</title>
<link href="/icon.png" rel="icon" type="image/png">
<meta name="viewport" content="width=device-width">
<link href="/assets/fonts/fonts.css" rel="stylesheet" type="text/css">
Expand All @@ -78,9 +78,6 @@ func initNotifications(mux *http.ServeMux, root webdav.FileSystem, users users.S
}
</style>`,
}
if *productionFlag {
opt.HeadPre += "\n\t\t" + googleAnalytics
}
opt.BodyPre = `<div style="max-width: 800px; margin: 0 auto 100px auto;">`
opt.BodyTop = func(req *http.Request) ([]htmlg.Component, error) {
authenticatedUser, err := users.GetAuthenticated(req.Context())
Expand Down
11 changes: 5 additions & 6 deletions package.go
Expand Up @@ -35,12 +35,11 @@ type packageHandler struct {

var packageHTML = template.Must(template.New("").Parse(`<html>
<head>
<title>{{.FullName}}</title>
{{.AnalyticsHTML}} <title>{{.FullName}}</title>
<link href="/icon.png" rel="icon" type="image/png">
<meta name="viewport" content="width=device-width">
<link href="/assets/fonts/fonts.css" rel="stylesheet" type="text/css">
<link href="/assets/package/style.css" rel="stylesheet" type="text/css">
{{if .Production}}` + googleAnalytics + `{{end}}
</head>
<body>`))

Expand Down Expand Up @@ -68,11 +67,11 @@ func (h *packageHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) err
fullName = "Package " + h.Pkg.Name
}
err = packageHTML.Execute(w, struct {
Production bool
FullName string
AnalyticsHTML template.HTML
FullName string
}{
Production: *productionFlag,
FullName: fullName,
AnalyticsHTML: analyticsHTML,
FullName: fullName,
})
if err != nil {
return err
Expand Down

0 comments on commit 2ffc368

Please sign in to comment.