1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2026-04-22 06:31:24 +00:00
Files
mycorrhiza/main.go

172 lines
4.8 KiB
Go
Raw Normal View History

2020-08-31 22:52:26 +05:00
//go:generate go get -u github.com/valyala/quicktemplate/qtc
//go:generate qtc -dir=assets
//go:generate qtc -dir=views
//go:generate qtc -dir=tree
2020-06-12 21:22:02 +05:00
package main
import (
"fmt"
"github.com/bouncepaw/mycorrhiza/cfg"
"io"
2020-12-15 23:59:36 +05:00
"io/ioutil"
2020-06-13 16:18:11 +05:00
"log"
"net/http"
2021-05-09 13:34:14 +05:00
"net/url"
2020-06-12 21:22:02 +05:00
"os"
2020-12-15 23:59:36 +05:00
"strings"
2020-08-09 01:10:28 +05:00
"github.com/bouncepaw/mycorrhiza/assets"
2021-04-28 17:12:05 +07:00
"github.com/bouncepaw/mycorrhiza/files"
2020-08-09 01:10:28 +05:00
"github.com/bouncepaw/mycorrhiza/history"
"github.com/bouncepaw/mycorrhiza/hyphae"
2021-02-20 19:03:54 +05:00
"github.com/bouncepaw/mycorrhiza/shroom"
"github.com/bouncepaw/mycorrhiza/user"
"github.com/bouncepaw/mycorrhiza/views"
2020-06-12 21:22:02 +05:00
)
// WikiDir is a rooted path to the wiki storage directory.
var WikiDir string
// HttpErr is used by many handlers to signal errors in a compact way.
func HttpErr(w http.ResponseWriter, status int, name, title, errMsg string) {
log.Println(errMsg, "for", name)
w.Header().Set("Content-Type", "text/html;charset=utf-8")
w.WriteHeader(status)
2021-01-24 12:30:14 +05:00
fmt.Fprint(
w,
base(
title,
fmt.Sprintf(
2021-02-19 21:56:31 +05:00
`<main class="main-width"><p>%s. <a href="/page/%s">Go back to the hypha.<a></p></main>`,
2021-01-24 12:30:14 +05:00
errMsg,
name,
),
user.EmptyUser(),
),
)
}
// This part is present in all html documents.
var base = views.BaseHTML
2020-10-25 23:02:52 +05:00
func handlerStyle(w http.ResponseWriter, rq *http.Request) {
prepareRq(rq)
if _, err := os.Stat(cfg.WikiDir + "/static/common.css"); err == nil {
http.ServeFile(w, rq, cfg.WikiDir+"/static/common.css")
2020-10-25 23:02:52 +05:00
} else {
w.Header().Set("Content-Type", "text/css;charset=utf-8")
w.Write([]byte(assets.DefaultCSS()))
2020-10-25 23:02:52 +05:00
}
if bytes, err := ioutil.ReadFile(cfg.WikiDir + "/static/custom.css"); err == nil {
w.Write(bytes)
}
2020-10-25 23:02:52 +05:00
}
2021-03-20 18:55:00 +05:00
func handlerToolbar(w http.ResponseWriter, rq *http.Request) {
prepareRq(rq)
2021-03-20 19:21:27 +05:00
w.Header().Set("Content-Type", "text/javascript;charset=utf-8")
2021-03-20 18:55:00 +05:00
w.Write([]byte(assets.ToolbarJS()))
}
// handlerIcon serves the requested icon. All icons are distributed as part of the Mycorrhiza binary.
//
// See assets/assets/icon/ for icons themselves, see assets/assets.qtpl for their sources.
2020-12-15 23:59:36 +05:00
func handlerIcon(w http.ResponseWriter, rq *http.Request) {
iconName := strings.TrimPrefix(rq.URL.Path, "/assets/icon/")
2020-12-15 23:59:36 +05:00
if iconName == "https" {
iconName = "http"
}
w.Header().Set("Content-Type", "image/svg+xml")
icon := func() string {
switch iconName {
case "gemini":
return assets.IconGemini()
case "mailto":
return assets.IconMailto()
case "gopher":
return assets.IconGopher()
case "feed":
return assets.IconFeed()
default:
return assets.IconHTTP()
}
}()
_, err := io.WriteString(w, icon)
if err != nil {
log.Println(err)
2020-12-15 23:59:36 +05:00
}
2020-12-15 23:59:36 +05:00
}
2021-01-30 23:21:50 +05:00
func handlerUserList(w http.ResponseWriter, rq *http.Request) {
w.Header().Set("Content-Type", "text/html;charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write([]byte(base("User list", views.UserListHTML(), user.FromRequest(rq))))
2021-01-30 23:21:50 +05:00
}
2020-12-29 13:10:11 +05:00
func handlerRobotsTxt(w http.ResponseWriter, rq *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
w.Write([]byte(
`User-agent: *
Allow: /page/
Allow: /recent-changes
Disallow: /
Crawl-delay: 5`))
}
func prepareRq(rq *http.Request) {
log.Println(rq.RequestURI)
rq.URL.Path = strings.TrimSuffix(rq.URL.Path, "/")
}
func main() {
2020-10-25 20:06:51 +05:00
parseCliArgs()
2021-04-28 17:12:36 +07:00
// It is ok if the path is ""
cfg.ReadConfigFile(cfg.ConfigFilePath)
2021-04-28 17:12:36 +07:00
if err := files.CalculatePaths(); err != nil {
log.Fatal(err)
}
log.Println("Running MycorrhizaWiki")
2020-08-19 23:54:23 +05:00
if err := os.Chdir(WikiDir); err != nil {
log.Fatal(err)
}
log.Println("Wiki storage directory is", WikiDir)
2021-02-17 23:41:35 +05:00
hyphae.Index(WikiDir)
log.Println("Indexed", hyphae.Count(), "hyphae")
2021-04-28 17:12:05 +07:00
// Initialize user database
user.InitUserDatabase()
2020-08-09 01:10:28 +05:00
history.Start(WikiDir)
2021-02-20 19:03:54 +05:00
shroom.SetHeaderLinks()
2020-08-09 01:10:28 +05:00
go handleGemini()
// See http_admin.go for /admin, /admin/*
initAdmin()
2021-02-24 22:34:42 +05:00
// See http_readers.go for /page/, /hypha/, /text/, /binary/, /attachment/
2021-01-19 23:08:59 +05:00
// See http_mutators.go for /upload-binary/, /upload-text/, /edit/, /delete-ask/, /delete-confirm/, /rename-ask/, /rename-confirm/, /unattach-ask/, /unattach-confirm/
2020-11-14 18:03:06 +05:00
// See http_auth.go for /login, /login-data, /logout, /logout-confirm
http.HandleFunc("/user-list/", handlerUserList)
2021-05-09 14:18:21 +05:00
// See http_history.go for /history/, /recent-changes
// See http_stuff.go for list, reindex, update-header-links, random, about
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")
})
2020-10-25 23:02:52 +05:00
http.HandleFunc("/static/common.css", handlerStyle)
2021-03-20 18:55:00 +05:00
http.HandleFunc("/static/toolbar.js", handlerToolbar)
http.HandleFunc("/assets/icon/", handlerIcon)
http.HandleFunc("/robots.txt", handlerRobotsTxt)
http.HandleFunc("/", func(w http.ResponseWriter, rq *http.Request) {
addr, _ := url.Parse("/hypha/" + cfg.HomeHypha) // Let's pray it never fails
2021-05-09 13:34:14 +05:00
rq.URL = addr
handlerHypha(w, rq)
})
log.Fatal(http.ListenAndServe("0.0.0.0:"+cfg.HTTPPort, nil))
2020-06-12 21:22:02 +05:00
}