From e72ca863e3a420937faa16f9633c623b84dc6a74 Mon Sep 17 00:00:00 2001 From: bouncepaw Date: Sat, 23 Jan 2021 18:45:17 +0500 Subject: [PATCH] Add /about page, remove -title option, add -name and -icon options --- README.md | 6 +- flag.go | 3 +- main.go | 7 + name.go | 2 +- templates/http_stuff.qtpl | 25 ++++ templates/http_stuff.qtpl.go | 244 +++++++++++++++++++++++++---------- user/users.go | 22 ++++ util/util.go | 3 +- 8 files changed, 237 insertions(+), 75 deletions(-) diff --git a/README.md b/README.md index 69e179b..cc79e78 100644 --- a/README.md +++ b/README.md @@ -24,10 +24,12 @@ Options: Used when -auth-method=fixed. Path to file with user credentials. (default "mycocredentials.json") -home string The home page (default "home") + -icon string + What to show in the navititle in the beginning, before the colon (default "🍄") + -name string + What is the name of your wiki (default "wiki") -port string Port to serve the wiki at (default "1737") - -title string - How to call your wiki in the navititle (default "🍄") -url string URL at which your wiki can be found. Used to generate feeds (default "http://0.0.0.0:$port") -user-tree string diff --git a/flag.go b/flag.go index c2cb461..e7ef67d 100644 --- a/flag.go +++ b/flag.go @@ -13,7 +13,8 @@ func init() { flag.StringVar(&util.URL, "url", "http://0.0.0.0:$port", "URL at which your wiki can be found. Used to generate feeds and social media previews") flag.StringVar(&util.ServerPort, "port", "1737", "Port to serve the wiki at using HTTP") flag.StringVar(&util.HomePage, "home", "home", "The home page name") - flag.StringVar(&util.SiteTitle, "title", "🍄", "How to call your wiki in the navititle") + flag.StringVar(&util.SiteNavIcon, "icon", "🍄", "What to show in the navititle in the beginning, before the colon") + flag.StringVar(&util.SiteName, "name", "wiki", "What is the name of your wiki") flag.StringVar(&util.UserTree, "user-tree", "u", "Hypha which is a superhypha of all user pages") flag.StringVar(&util.AuthMethod, "auth-method", "none", "What auth method to use. Variants: \"none\", \"fixed\"") flag.StringVar(&util.FixedCredentialsPath, "fixed-credentials-path", "mycocredentials.json", "Used when -auth-method=fixed. Path to file with user credentials.") diff --git a/main.go b/main.go index 6a4c26a..700adad 100644 --- a/main.go +++ b/main.go @@ -129,6 +129,12 @@ func handlerIcon(w http.ResponseWriter, rq *http.Request) { } } +func handlerAbout(w http.ResponseWriter, rq *http.Request) { + w.Header().Set("Content-Type", "text/html;charset=utf-8") + w.WriteHeader(http.StatusOK) + w.Write([]byte(base("About "+util.SiteName, templates.AboutHTML()))) +} + func handlerRobotsTxt(w http.ResponseWriter, rq *http.Request) { w.Header().Set("Content-Type", "text/plain") w.WriteHeader(http.StatusOK) @@ -160,6 +166,7 @@ func main() { http.HandleFunc("/list", handlerList) http.HandleFunc("/reindex", handlerReindex) http.HandleFunc("/random", handlerRandom) + http.HandleFunc("/about", handlerAbout) http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir(WikiDir+"/static")))) http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, rq *http.Request) { http.ServeFile(w, rq, WikiDir+"/static/favicon.ico") diff --git a/name.go b/name.go index 9d0bf08..2a859d8 100644 --- a/name.go +++ b/name.go @@ -23,7 +23,7 @@ func CanonicalName(name string) string { func naviTitle(canonicalName string) string { var ( html = fmt.Sprintf(`

- %s`, util.HomePage, util.SiteTitle) + %s`, util.HomePage, util.SiteNavIcon) prevAcc = `/page/` parts = strings.Split(canonicalName, "/") rel = "up" diff --git a/templates/http_stuff.qtpl b/templates/http_stuff.qtpl index 3a88e2f..4d10a9b 100644 --- a/templates/http_stuff.qtpl +++ b/templates/http_stuff.qtpl @@ -1,3 +1,6 @@ +{% import "github.com/bouncepaw/mycorrhiza/util" %} +{% import "github.com/bouncepaw/mycorrhiza/user" %} + {% func BaseHTML(title, body string, headElements ...string) %} @@ -41,3 +44,25 @@ {% endif %} {% endfunc %} + +{% func AboutHTML() %} +
+
+

About {%s util.SiteName %}

+
    +
  • MycorrhizaWiki version: β 0.12 indev
  • + {%- if user.AuthUsed -%} +
  • User count: {%d user.Count() %}
  • +
  • Home page: {%s util.HomePage %}
  • +
  • Administrators: {%- for i, username := range user.ListUsersWithGroup("admin") -%} + {%- if i > 0 -%} + {%- endif -%} + {%s username %}{%- endfor -%}
  • + {%- else -%} +
  • This wiki does not use authorization
  • + {%- endif -%} +
+

See /list for information about hyphae on this wiki.

+
+
+{% endfunc %} diff --git a/templates/http_stuff.qtpl.go b/templates/http_stuff.qtpl.go index 1e2bd99..b2b286a 100644 --- a/templates/http_stuff.qtpl.go +++ b/templates/http_stuff.qtpl.go @@ -5,21 +5,27 @@ package templates //line templates/http_stuff.qtpl:1 +import "github.com/bouncepaw/mycorrhiza/util" + +//line templates/http_stuff.qtpl:2 +import "github.com/bouncepaw/mycorrhiza/user" + +//line templates/http_stuff.qtpl:4 import ( qtio422016 "io" qt422016 "github.com/valyala/quicktemplate" ) -//line templates/http_stuff.qtpl:1 +//line templates/http_stuff.qtpl:4 var ( _ = qtio422016.Copy _ = qt422016.AcquireByteBuffer ) -//line templates/http_stuff.qtpl:1 +//line templates/http_stuff.qtpl:4 func StreamBaseHTML(qw422016 *qt422016.Writer, title, body string, headElements ...string) { -//line templates/http_stuff.qtpl:1 +//line templates/http_stuff.qtpl:4 qw422016.N().S(` @@ -27,68 +33,68 @@ func StreamBaseHTML(qw422016 *qt422016.Writer, title, body string, headElements `) -//line templates/http_stuff.qtpl:7 +//line templates/http_stuff.qtpl:10 qw422016.E().S(title) -//line templates/http_stuff.qtpl:7 +//line templates/http_stuff.qtpl:10 qw422016.N().S(` `) -//line templates/http_stuff.qtpl:8 +//line templates/http_stuff.qtpl:11 for _, el := range headElements { -//line templates/http_stuff.qtpl:8 +//line templates/http_stuff.qtpl:11 qw422016.N().S(el) -//line templates/http_stuff.qtpl:8 +//line templates/http_stuff.qtpl:11 } -//line templates/http_stuff.qtpl:8 +//line templates/http_stuff.qtpl:11 qw422016.N().S(` `) -//line templates/http_stuff.qtpl:11 +//line templates/http_stuff.qtpl:14 qw422016.N().S(body) -//line templates/http_stuff.qtpl:11 +//line templates/http_stuff.qtpl:14 qw422016.N().S(` `) -//line templates/http_stuff.qtpl:14 +//line templates/http_stuff.qtpl:17 } -//line templates/http_stuff.qtpl:14 +//line templates/http_stuff.qtpl:17 func WriteBaseHTML(qq422016 qtio422016.Writer, title, body string, headElements ...string) { -//line templates/http_stuff.qtpl:14 +//line templates/http_stuff.qtpl:17 qw422016 := qt422016.AcquireWriter(qq422016) -//line templates/http_stuff.qtpl:14 +//line templates/http_stuff.qtpl:17 StreamBaseHTML(qw422016, title, body, headElements...) -//line templates/http_stuff.qtpl:14 +//line templates/http_stuff.qtpl:17 qt422016.ReleaseWriter(qw422016) -//line templates/http_stuff.qtpl:14 +//line templates/http_stuff.qtpl:17 } -//line templates/http_stuff.qtpl:14 +//line templates/http_stuff.qtpl:17 func BaseHTML(title, body string, headElements ...string) string { -//line templates/http_stuff.qtpl:14 +//line templates/http_stuff.qtpl:17 qb422016 := qt422016.AcquireByteBuffer() -//line templates/http_stuff.qtpl:14 +//line templates/http_stuff.qtpl:17 WriteBaseHTML(qb422016, title, body, headElements...) -//line templates/http_stuff.qtpl:14 +//line templates/http_stuff.qtpl:17 qs422016 := string(qb422016.B) -//line templates/http_stuff.qtpl:14 +//line templates/http_stuff.qtpl:17 qt422016.ReleaseByteBuffer(qb422016) -//line templates/http_stuff.qtpl:14 +//line templates/http_stuff.qtpl:17 return qs422016 -//line templates/http_stuff.qtpl:14 +//line templates/http_stuff.qtpl:17 } -//line templates/http_stuff.qtpl:16 +//line templates/http_stuff.qtpl:19 func StreamHyphaListHTML(qw422016 *qt422016.Writer, tbody string, pageCount int) { -//line templates/http_stuff.qtpl:16 +//line templates/http_stuff.qtpl:19 qw422016.N().S(`

List of hyphae

This wiki has `) -//line templates/http_stuff.qtpl:19 +//line templates/http_stuff.qtpl:22 qw422016.N().D(pageCount) -//line templates/http_stuff.qtpl:19 +//line templates/http_stuff.qtpl:22 qw422016.N().S(` hyphae.

@@ -99,105 +105,203 @@ func StreamHyphaListHTML(qw422016 *qt422016.Writer, tbody string, pageCount int) `) -//line templates/http_stuff.qtpl:28 +//line templates/http_stuff.qtpl:31 qw422016.N().S(tbody) -//line templates/http_stuff.qtpl:28 +//line templates/http_stuff.qtpl:31 qw422016.N().S(`
`) -//line templates/http_stuff.qtpl:32 +//line templates/http_stuff.qtpl:35 } -//line templates/http_stuff.qtpl:32 +//line templates/http_stuff.qtpl:35 func WriteHyphaListHTML(qq422016 qtio422016.Writer, tbody string, pageCount int) { -//line templates/http_stuff.qtpl:32 +//line templates/http_stuff.qtpl:35 qw422016 := qt422016.AcquireWriter(qq422016) -//line templates/http_stuff.qtpl:32 +//line templates/http_stuff.qtpl:35 StreamHyphaListHTML(qw422016, tbody, pageCount) -//line templates/http_stuff.qtpl:32 +//line templates/http_stuff.qtpl:35 qt422016.ReleaseWriter(qw422016) -//line templates/http_stuff.qtpl:32 +//line templates/http_stuff.qtpl:35 } -//line templates/http_stuff.qtpl:32 +//line templates/http_stuff.qtpl:35 func HyphaListHTML(tbody string, pageCount int) string { -//line templates/http_stuff.qtpl:32 +//line templates/http_stuff.qtpl:35 qb422016 := qt422016.AcquireByteBuffer() -//line templates/http_stuff.qtpl:32 +//line templates/http_stuff.qtpl:35 WriteHyphaListHTML(qb422016, tbody, pageCount) -//line templates/http_stuff.qtpl:32 +//line templates/http_stuff.qtpl:35 qs422016 := string(qb422016.B) -//line templates/http_stuff.qtpl:32 +//line templates/http_stuff.qtpl:35 qt422016.ReleaseByteBuffer(qb422016) -//line templates/http_stuff.qtpl:32 +//line templates/http_stuff.qtpl:35 return qs422016 -//line templates/http_stuff.qtpl:32 +//line templates/http_stuff.qtpl:35 } -//line templates/http_stuff.qtpl:34 +//line templates/http_stuff.qtpl:37 func StreamHyphaListRowHTML(qw422016 *qt422016.Writer, hyphaName, binaryMime string, binaryPresent bool) { -//line templates/http_stuff.qtpl:34 +//line templates/http_stuff.qtpl:37 qw422016.N().S(` `) -//line templates/http_stuff.qtpl:36 +//line templates/http_stuff.qtpl:39 qw422016.E().S(hyphaName) -//line templates/http_stuff.qtpl:36 +//line templates/http_stuff.qtpl:39 qw422016.N().S(` `) -//line templates/http_stuff.qtpl:37 +//line templates/http_stuff.qtpl:40 if binaryPresent { -//line templates/http_stuff.qtpl:37 +//line templates/http_stuff.qtpl:40 qw422016.N().S(` `) -//line templates/http_stuff.qtpl:38 +//line templates/http_stuff.qtpl:41 qw422016.E().S(binaryMime) -//line templates/http_stuff.qtpl:38 +//line templates/http_stuff.qtpl:41 qw422016.N().S(` `) -//line templates/http_stuff.qtpl:39 +//line templates/http_stuff.qtpl:42 } else { -//line templates/http_stuff.qtpl:39 +//line templates/http_stuff.qtpl:42 qw422016.N().S(` `) -//line templates/http_stuff.qtpl:41 +//line templates/http_stuff.qtpl:44 } -//line templates/http_stuff.qtpl:41 +//line templates/http_stuff.qtpl:44 qw422016.N().S(` `) -//line templates/http_stuff.qtpl:43 +//line templates/http_stuff.qtpl:46 } -//line templates/http_stuff.qtpl:43 +//line templates/http_stuff.qtpl:46 func WriteHyphaListRowHTML(qq422016 qtio422016.Writer, hyphaName, binaryMime string, binaryPresent bool) { -//line templates/http_stuff.qtpl:43 +//line templates/http_stuff.qtpl:46 qw422016 := qt422016.AcquireWriter(qq422016) -//line templates/http_stuff.qtpl:43 +//line templates/http_stuff.qtpl:46 StreamHyphaListRowHTML(qw422016, hyphaName, binaryMime, binaryPresent) -//line templates/http_stuff.qtpl:43 +//line templates/http_stuff.qtpl:46 qt422016.ReleaseWriter(qw422016) -//line templates/http_stuff.qtpl:43 +//line templates/http_stuff.qtpl:46 } -//line templates/http_stuff.qtpl:43 +//line templates/http_stuff.qtpl:46 func HyphaListRowHTML(hyphaName, binaryMime string, binaryPresent bool) string { -//line templates/http_stuff.qtpl:43 +//line templates/http_stuff.qtpl:46 qb422016 := qt422016.AcquireByteBuffer() -//line templates/http_stuff.qtpl:43 +//line templates/http_stuff.qtpl:46 WriteHyphaListRowHTML(qb422016, hyphaName, binaryMime, binaryPresent) -//line templates/http_stuff.qtpl:43 +//line templates/http_stuff.qtpl:46 qs422016 := string(qb422016.B) -//line templates/http_stuff.qtpl:43 +//line templates/http_stuff.qtpl:46 qt422016.ReleaseByteBuffer(qb422016) -//line templates/http_stuff.qtpl:43 +//line templates/http_stuff.qtpl:46 return qs422016 -//line templates/http_stuff.qtpl:43 +//line templates/http_stuff.qtpl:46 +} + +//line templates/http_stuff.qtpl:48 +func StreamAboutHTML(qw422016 *qt422016.Writer) { +//line templates/http_stuff.qtpl:48 + qw422016.N().S(` +
+
+

About `) +//line templates/http_stuff.qtpl:51 + qw422016.E().S(util.SiteName) +//line templates/http_stuff.qtpl:51 + qw422016.N().S(`

+ +

See /list for information about hyphae on this wiki.

+
+
+`) +//line templates/http_stuff.qtpl:68 +} + +//line templates/http_stuff.qtpl:68 +func WriteAboutHTML(qq422016 qtio422016.Writer) { +//line templates/http_stuff.qtpl:68 + qw422016 := qt422016.AcquireWriter(qq422016) +//line templates/http_stuff.qtpl:68 + StreamAboutHTML(qw422016) +//line templates/http_stuff.qtpl:68 + qt422016.ReleaseWriter(qw422016) +//line templates/http_stuff.qtpl:68 +} + +//line templates/http_stuff.qtpl:68 +func AboutHTML() string { +//line templates/http_stuff.qtpl:68 + qb422016 := qt422016.AcquireByteBuffer() +//line templates/http_stuff.qtpl:68 + WriteAboutHTML(qb422016) +//line templates/http_stuff.qtpl:68 + qs422016 := string(qb422016.B) +//line templates/http_stuff.qtpl:68 + qt422016.ReleaseByteBuffer(qb422016) +//line templates/http_stuff.qtpl:68 + return qs422016 +//line templates/http_stuff.qtpl:68 } diff --git a/user/users.go b/user/users.go index 58a174d..397ecf0 100644 --- a/user/users.go +++ b/user/users.go @@ -8,6 +8,28 @@ var AuthUsed bool var users sync.Map var tokens sync.Map +func ListUsersWithGroup(group string) []string { + usersWithTheGroup := []string{} + users.Range(func(_, v interface{}) bool { + userobj := v.(*User) + + if userobj.Group == group { + usersWithTheGroup = append(usersWithTheGroup, userobj.Name) + } + return true + }) + return usersWithTheGroup +} + +func Count() int { + i := 0 + users.Range(func(k, v interface{}) bool { + i++ + return true + }) + return i +} + func HasUsername(username string) bool { _, has := users.Load(username) return has diff --git a/util/util.go b/util/util.go index e593f70..5bbf9d7 100644 --- a/util/util.go +++ b/util/util.go @@ -11,7 +11,8 @@ var ( URL string ServerPort string HomePage string - SiteTitle string + SiteNavIcon string + SiteName string WikiDir string UserTree string AuthMethod string