mirror of
https://github.com/osmarks/mycorrhiza.git
synced 2025-01-05 17:40:26 +00:00
New structure
This commit is contained in:
parent
b90c288549
commit
7b2423ec40
@ -25,9 +25,7 @@ var (
|
||||
GeminiCertificatePath string
|
||||
|
||||
UseFixedAuth bool
|
||||
FixedAuthCredentialsPath string
|
||||
UseRegistration bool
|
||||
RegistrationCredentialsPath string
|
||||
LimitRegistration int
|
||||
|
||||
OmnipresentScripts []string
|
||||
@ -80,10 +78,8 @@ type CustomScripts struct {
|
||||
// Authorization is a section of Config that has fields related to authorization and authentication.
|
||||
type Authorization struct {
|
||||
UseFixedAuth bool
|
||||
FixedAuthCredentialsPath string
|
||||
|
||||
UseRegistration bool
|
||||
RegistrationCredentialsPath string
|
||||
LimitRegistration uint64
|
||||
}
|
||||
|
||||
@ -106,10 +102,8 @@ func ReadConfigFile() {
|
||||
},
|
||||
Authorization: Authorization{
|
||||
UseFixedAuth: false,
|
||||
FixedAuthCredentialsPath: "",
|
||||
|
||||
UseRegistration: false,
|
||||
RegistrationCredentialsPath: "",
|
||||
LimitRegistration: 0,
|
||||
},
|
||||
CustomScripts: CustomScripts{
|
||||
@ -142,9 +136,7 @@ func ReadConfigFile() {
|
||||
URL = cfg.URL
|
||||
GeminiCertificatePath = cfg.GeminiCertificatePath
|
||||
UseFixedAuth = cfg.UseFixedAuth
|
||||
FixedAuthCredentialsPath = cfg.FixedAuthCredentialsPath
|
||||
UseRegistration = cfg.UseRegistration
|
||||
RegistrationCredentialsPath = cfg.RegistrationCredentialsPath
|
||||
LimitRegistration = int(cfg.LimitRegistration)
|
||||
OmnipresentScripts = cfg.OmnipresentScripts
|
||||
ViewScripts = cfg.ViewScripts
|
||||
|
119
files/files.go
119
files/files.go
@ -2,109 +2,68 @@
|
||||
package files
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/bouncepaw/mycorrhiza/cfg"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"github.com/adrg/xdg"
|
||||
"github.com/mitchellh/go-homedir"
|
||||
"github.com/bouncepaw/mycorrhiza/cfg"
|
||||
)
|
||||
|
||||
var paths struct {
|
||||
gitRepo string
|
||||
cacheDir string
|
||||
staticFiles string
|
||||
tokensJSON string
|
||||
registrationCredentialsJSON string
|
||||
fixedCredentialsJSON string
|
||||
}
|
||||
|
||||
// TokensJSON returns a path to the JSON file where users' tokens are stored.
|
||||
//
|
||||
// Default path: $XDG_DATA_HOME/mycorrhiza/tokens.json
|
||||
// HyphaeDir returns the path to hyphae storage.
|
||||
// A separate function is needed to easily know where a general storage path is
|
||||
// needed rather than a concrete Git or the whole wiki storage path, so that we
|
||||
// could easily refactor things later if we'll ever support different storages.
|
||||
func HyphaeDir() string { return paths.gitRepo }
|
||||
|
||||
// GitRepo returns the path to the Git repository of the wiki.
|
||||
func GitRepo() string { return paths.gitRepo }
|
||||
|
||||
// StaticFiles returns the path to static files directory
|
||||
func StaticFiles() string { return paths.staticFiles }
|
||||
|
||||
// TokensJSON returns the path to the JSON user tokens storage.
|
||||
func TokensJSON() string { return paths.tokensJSON }
|
||||
|
||||
// RegistrationCredentialsJSON returns a path to the JSON file where registration credentials are stored.
|
||||
//
|
||||
// Default path: $XDG_DATA_HOME/mycorrhiza/registration.json
|
||||
// RegistrationCredentialsJSON returns the path to the JSON registration
|
||||
// credentials storage.
|
||||
func RegistrationCredentialsJSON() string { return paths.registrationCredentialsJSON }
|
||||
|
||||
// FixedCredentialsJSON returns a path to the JSON file where fixed credentials are stored.
|
||||
//
|
||||
// There is no default path.
|
||||
// FixedCredentialsJSON returns the path to the JSON fixed credentials storage.
|
||||
func FixedCredentialsJSON() string { return paths.fixedCredentialsJSON }
|
||||
|
||||
// CalculatePaths looks for all external paths and stores them. Tries its best to find any errors. It is safe it to call it multiple times in order to save new paths.
|
||||
func CalculatePaths() error {
|
||||
if dir, err := registrationCredentialsPath(); err != nil {
|
||||
// PrepareWikiRoot ensures all needed directories and files exist and have
|
||||
// correct permissions.
|
||||
func PrepareWikiRoot() error {
|
||||
if err := os.MkdirAll(cfg.WikiDir, os.ModeDir|0777); err != nil {
|
||||
return err
|
||||
} else {
|
||||
paths.registrationCredentialsJSON = dir
|
||||
}
|
||||
|
||||
if dir, err := tokenStoragePath(); err != nil {
|
||||
paths.cacheDir = path.Join(cfg.WikiDir, "cache")
|
||||
if err := os.MkdirAll(paths.cacheDir, os.ModeDir|0777); err != nil {
|
||||
return err
|
||||
} else {
|
||||
paths.tokensJSON = dir
|
||||
}
|
||||
|
||||
if dir, err := fixedCredentialsPath(); err != nil {
|
||||
paths.gitRepo = path.Join(cfg.WikiDir, "wiki.git")
|
||||
if err := os.MkdirAll(paths.gitRepo, os.ModeDir|0777); err != nil {
|
||||
return err
|
||||
} else {
|
||||
paths.fixedCredentialsJSON = dir
|
||||
}
|
||||
|
||||
paths.staticFiles = path.Join(cfg.WikiDir, "static")
|
||||
if err := os.MkdirAll(paths.staticFiles, os.ModeDir|0777); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
paths.tokensJSON = path.Join(paths.cacheDir, "tokens.json")
|
||||
paths.fixedCredentialsJSON = path.Join(cfg.WikiDir, "fixed-users.json")
|
||||
paths.registrationCredentialsJSON = path.Join(paths.cacheDir, "registered-users.json")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func tokenStoragePath() (string, error) {
|
||||
dir, err := xdg.DataFile("mycorrhiza/tokens.json")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if strings.HasPrefix(dir, cfg.WikiDir) {
|
||||
return "", errors.New("wiki storage directory includes private config files")
|
||||
}
|
||||
return dir, nil
|
||||
}
|
||||
|
||||
func registrationCredentialsPath() (string, error) {
|
||||
var err error
|
||||
path := cfg.RegistrationCredentialsPath
|
||||
|
||||
if len(path) == 0 {
|
||||
path, err = xdg.DataFile("mycorrhiza/registration.json")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot get a file to registration credentials, so no registered users will be saved: %w", err)
|
||||
}
|
||||
} else {
|
||||
path, err = homedir.Expand(path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot expand RegistrationCredentialsPath: %w", err)
|
||||
}
|
||||
|
||||
path, err = filepath.Abs(path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot expand RegistrationCredentialsPath: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return path, nil
|
||||
}
|
||||
|
||||
func fixedCredentialsPath() (string, error) {
|
||||
var err error
|
||||
path := cfg.FixedAuthCredentialsPath
|
||||
|
||||
if len(path) > 0 {
|
||||
path, err = homedir.Expand(path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot expand FixedAuthCredentialsPath: %w", err)
|
||||
}
|
||||
|
||||
path, err = filepath.Abs(path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot expand FixedAuthCredentialsPath: %w", err)
|
||||
}
|
||||
}
|
||||
return path, nil
|
||||
}
|
||||
|
3
flag.go
3
flag.go
@ -51,8 +51,9 @@ func parseCliArgs() {
|
||||
}
|
||||
|
||||
wikiDir, err := filepath.Abs(args[0])
|
||||
cfg.WikiDir = wikiDir
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
cfg.WikiDir = wikiDir
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/bouncepaw/mycorrhiza/cfg"
|
||||
"github.com/bouncepaw/mycorrhiza/files"
|
||||
"github.com/bouncepaw/mycorrhiza/util"
|
||||
)
|
||||
|
||||
@ -162,7 +162,7 @@ func (rev *Revision) bestLink() string {
|
||||
func gitsh(args ...string) (out bytes.Buffer, err error) {
|
||||
fmt.Printf("$ %v\n", args)
|
||||
cmd := exec.Command(gitpath, args...)
|
||||
cmd.Dir = cfg.WikiDir
|
||||
cmd.Dir = files.HyphaeDir()
|
||||
cmd.Env = gitEnv
|
||||
|
||||
b, err := cmd.CombinedOutput()
|
||||
@ -175,7 +175,7 @@ func gitsh(args ...string) (out bytes.Buffer, err error) {
|
||||
// silentGitsh is like gitsh, except it writes less to the stdout.
|
||||
func silentGitsh(args ...string) (out bytes.Buffer, err error) {
|
||||
cmd := exec.Command(gitpath, args...)
|
||||
cmd.Dir = cfg.WikiDir
|
||||
cmd.Dir = files.HyphaeDir()
|
||||
cmd.Env = gitEnv
|
||||
|
||||
b, err := cmd.CombinedOutput()
|
||||
|
@ -4,13 +4,15 @@ package history
|
||||
// Things related to gathering existing information.
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/bouncepaw/mycorrhiza/cfg"
|
||||
"log"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/bouncepaw/mycorrhiza/cfg"
|
||||
"github.com/bouncepaw/mycorrhiza/files"
|
||||
|
||||
"github.com/gorilla/feeds"
|
||||
)
|
||||
|
||||
@ -176,7 +178,7 @@ func parseRevisionLine(line string) Revision {
|
||||
|
||||
// FileAtRevision shows how the file with the given file path looked at the commit with the hash. It may return an error if git fails.
|
||||
func FileAtRevision(filepath, hash string) (string, error) {
|
||||
out, err := gitsh("show", hash+":"+strings.TrimPrefix(filepath, cfg.WikiDir+"/"))
|
||||
out, err := gitsh("show", hash+":"+strings.TrimPrefix(filepath, files.HyphaeDir()+"/"))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
18
main.go
18
main.go
@ -6,6 +6,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/bouncepaw/mycorrhiza/cfg"
|
||||
"github.com/bouncepaw/mycorrhiza/files"
|
||||
"github.com/bouncepaw/mycorrhiza/history"
|
||||
@ -14,9 +18,6 @@ import (
|
||||
"github.com/bouncepaw/mycorrhiza/static"
|
||||
"github.com/bouncepaw/mycorrhiza/user"
|
||||
"github.com/bouncepaw/mycorrhiza/web"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@ -25,24 +26,25 @@ func main() {
|
||||
// It is ok if the path is ""
|
||||
cfg.ReadConfigFile()
|
||||
|
||||
if err := files.CalculatePaths(); err != nil {
|
||||
if err := files.PrepareWikiRoot(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
log.Println("Running Mycorrhiza Wiki 1.2.0 indev")
|
||||
if err := os.Chdir(cfg.WikiDir); err != nil {
|
||||
if err := os.Chdir(files.HyphaeDir()); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.Println("Wiki storage directory is", cfg.WikiDir)
|
||||
log.Println("Wiki directory is", cfg.WikiDir)
|
||||
log.Println("Using Git storage at", files.HyphaeDir())
|
||||
|
||||
// Init the subsystems:
|
||||
hyphae.Index(cfg.WikiDir)
|
||||
hyphae.Index(files.HyphaeDir())
|
||||
user.InitUserDatabase()
|
||||
history.Start()
|
||||
shroom.SetHeaderLinks()
|
||||
|
||||
// Static files:
|
||||
static.InitFS(cfg.WikiDir + "/static")
|
||||
static.InitFS(files.StaticFiles())
|
||||
|
||||
// Network:
|
||||
go handleGemini()
|
||||
|
@ -10,8 +10,7 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/bouncepaw/mycorrhiza/cfg"
|
||||
|
||||
"github.com/bouncepaw/mycorrhiza/files"
|
||||
"github.com/bouncepaw/mycorrhiza/history"
|
||||
"github.com/bouncepaw/mycorrhiza/hyphae"
|
||||
"github.com/bouncepaw/mycorrhiza/mimetype"
|
||||
@ -65,10 +64,11 @@ func UploadBinary(h *hyphae.Hypha, mime string, file multipart.File, u *user.Use
|
||||
// uploadHelp is a helper function for UploadText and UploadBinary
|
||||
func uploadHelp(h *hyphae.Hypha, hop *history.HistoryOp, ext string, data []byte, u *user.User) (*history.HistoryOp, string) {
|
||||
var (
|
||||
fullPath = filepath.Join(cfg.WikiDir, h.Name+ext)
|
||||
fullPath = filepath.Join(files.HyphaeDir(), h.Name+ext)
|
||||
originalFullPath = &h.TextPath
|
||||
)
|
||||
if !strings.HasPrefix(fullPath, cfg.WikiDir) { // If the path somehow got outside the wiki dir
|
||||
// Reject if the path is outside the hyphae dir
|
||||
if !strings.HasPrefix(fullPath, files.HyphaeDir()) {
|
||||
err := errors.New("bad path")
|
||||
return hop.WithErrAbort(err), err.Error()
|
||||
}
|
||||
@ -80,7 +80,7 @@ func uploadHelp(h *hyphae.Hypha, hop *history.HistoryOp, ext string, data []byte
|
||||
return hop.WithErrAbort(err), err.Error()
|
||||
}
|
||||
|
||||
if err := ioutil.WriteFile(fullPath, data, 0644); err != nil {
|
||||
if err := ioutil.WriteFile(fullPath, data, 0666); err != nil {
|
||||
return hop.WithErrAbort(err), err.Error()
|
||||
}
|
||||
|
||||
|
@ -16,7 +16,7 @@ import (
|
||||
func InitUserDatabase() {
|
||||
AuthUsed = cfg.UseFixedAuth || cfg.UseRegistration
|
||||
|
||||
if AuthUsed && (cfg.FixedAuthCredentialsPath != "" || cfg.RegistrationCredentialsPath != "") {
|
||||
if AuthUsed {
|
||||
ReadUsersFromFilesystem()
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package util
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"github.com/bouncepaw/mycorrhiza/files"
|
||||
"log"
|
||||
"net/http"
|
||||
"regexp"
|
||||
@ -17,10 +18,11 @@ func PrepareRq(rq *http.Request) {
|
||||
rq.URL.Path = strings.TrimSuffix(rq.URL.Path, "/")
|
||||
}
|
||||
|
||||
// ShorterPath is used by handlerList to display shorter path to the files. It simply strips WikiDir.
|
||||
// ShorterPath is used by handlerList to display shorter path to the files. It
|
||||
// simply strips the hyphae directory name.
|
||||
func ShorterPath(path string) string {
|
||||
if strings.HasPrefix(path, cfg.WikiDir) {
|
||||
tmp := strings.TrimPrefix(path, cfg.WikiDir)
|
||||
if strings.HasPrefix(path, files.HyphaeDir()) {
|
||||
tmp := strings.TrimPrefix(path, files.HyphaeDir())
|
||||
if tmp == "" {
|
||||
return ""
|
||||
}
|
||||
|
@ -2,12 +2,14 @@ package web
|
||||
|
||||
// stuff.go is used for meta stuff about the wiki or all hyphae at once.
|
||||
import (
|
||||
"github.com/bouncepaw/mycorrhiza/cfg"
|
||||
"io"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
|
||||
"github.com/bouncepaw/mycorrhiza/cfg"
|
||||
"github.com/bouncepaw/mycorrhiza/files"
|
||||
|
||||
"github.com/bouncepaw/mycorrhiza/hyphae"
|
||||
"github.com/bouncepaw/mycorrhiza/shroom"
|
||||
"github.com/bouncepaw/mycorrhiza/user"
|
||||
@ -41,9 +43,8 @@ func handlerReindex(w http.ResponseWriter, rq *http.Request) {
|
||||
return
|
||||
}
|
||||
hyphae.ResetCount()
|
||||
log.Println("Wiki storage directory is", cfg.WikiDir)
|
||||
log.Println("Start indexing hyphae...")
|
||||
hyphae.Index(cfg.WikiDir)
|
||||
log.Println("Reindexing hyphae in", files.HyphaeDir())
|
||||
hyphae.Index(files.HyphaeDir())
|
||||
log.Println("Indexed", hyphae.Count(), "hyphae")
|
||||
http.Redirect(w, rq, "/", http.StatusSeeOther)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user