Migrate others
This commit is contained in:
parent
7a33272c59
commit
09764385ac
41
flag.go
41
flag.go
@ -3,19 +3,20 @@ package main
|
|||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
_ "embed"
|
_ "embed"
|
||||||
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"golang.org/x/term"
|
|
||||||
|
|
||||||
"github.com/bouncepaw/mycorrhiza/internal/cfg"
|
"github.com/bouncepaw/mycorrhiza/internal/cfg"
|
||||||
"github.com/bouncepaw/mycorrhiza/internal/files"
|
"github.com/bouncepaw/mycorrhiza/internal/files"
|
||||||
user2 "github.com/bouncepaw/mycorrhiza/internal/user"
|
"github.com/bouncepaw/mycorrhiza/internal/user"
|
||||||
"github.com/bouncepaw/mycorrhiza/internal/version"
|
"github.com/bouncepaw/mycorrhiza/internal/version"
|
||||||
|
|
||||||
|
"golang.org/x/term"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CLI options are read and parsed here.
|
// CLI options are read and parsed here.
|
||||||
@ -31,7 +32,7 @@ func printHelp() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// parseCliArgs parses CLI options and sets several important global variables. Call it early.
|
// parseCliArgs parses CLI options and sets several important global variables. Call it early.
|
||||||
func parseCliArgs() {
|
func parseCliArgs() error {
|
||||||
var createAdminName string
|
var createAdminName string
|
||||||
var versionFlag bool
|
var versionFlag bool
|
||||||
|
|
||||||
@ -42,43 +43,53 @@ func parseCliArgs() {
|
|||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
if versionFlag {
|
if versionFlag {
|
||||||
fmt.Println("Mycorrhiza Wiki", version.Long)
|
slog.Info("Running Mycorrhiza Wiki", "version", version.Long)
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
args := flag.Args()
|
args := flag.Args()
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
log.Fatal("error: pass a wiki directory")
|
slog.Error("Pass a wiki directory")
|
||||||
|
return errors.New("wiki directory not passed")
|
||||||
}
|
}
|
||||||
|
|
||||||
wikiDir, err := filepath.Abs(args[0])
|
wikiDir, err := filepath.Abs(args[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
slog.Error("Failed to take absolute filepath of wiki directory",
|
||||||
|
"path", args[0], "err", err)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg.WikiDir = wikiDir
|
cfg.WikiDir = wikiDir
|
||||||
|
|
||||||
if createAdminName != "" {
|
if createAdminName != "" {
|
||||||
createAdminCommand(createAdminName)
|
if err := createAdminCommand(createAdminName); err != nil {
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func createAdminCommand(name string) {
|
func createAdminCommand(name string) error {
|
||||||
if err := files.PrepareWikiRoot(); err != nil {
|
if err := files.PrepareWikiRoot(); err != nil {
|
||||||
log.Fatal(err)
|
slog.Error("Failed to prepare wiki root", "err", err)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
cfg.UseAuth = true
|
cfg.UseAuth = true
|
||||||
cfg.AllowRegistration = true
|
cfg.AllowRegistration = true
|
||||||
user2.InitUserDatabase()
|
user.InitUserDatabase()
|
||||||
|
|
||||||
password, err := askPass("Password")
|
password, err := askPass("Password")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
slog.Error("Failed to prompt password", "err", err)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
if err := user2.Register(name, password, "admin", "local", true); err != nil {
|
if err := user.Register(name, password, "admin", "local", true); err != nil {
|
||||||
log.Fatal(err)
|
slog.Error("Failed to register admin", "err", err)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func askPass(prompt string) (string, error) {
|
func askPass(prompt string) (string, error) {
|
||||||
|
|||||||
@ -3,7 +3,7 @@ package user
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"log"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/bouncepaw/mycorrhiza/internal/cfg"
|
"github.com/bouncepaw/mycorrhiza/internal/cfg"
|
||||||
@ -32,19 +32,23 @@ func usersFromFile() []*User {
|
|||||||
return users
|
return users
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
slog.Error("Failed to read users.json", "err", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.Unmarshal(contents, &users)
|
err = json.Unmarshal(contents, &users)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
slog.Error("Failed to unmarshal users.json contents", "err", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, u := range users {
|
for _, u := range users {
|
||||||
u.Name = util.CanonicalName(u.Name)
|
u.Name = util.CanonicalName(u.Name)
|
||||||
if u.Source == "" {
|
if u.Source == "" {
|
||||||
u.Source = "local"
|
u.Source = "local"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log.Println("Found", len(users), "users")
|
slog.Info("Indexed users", "n", len(users))
|
||||||
return users
|
return users
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,20 +67,22 @@ func readTokensToUsers() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
slog.Error("Failed to read tokens.json", "err", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
var tmp map[string]string
|
var tmp map[string]string
|
||||||
err = json.Unmarshal(contents, &tmp)
|
err = json.Unmarshal(contents, &tmp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
slog.Error("Failed to unmarshal tokens.json contents", "err", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
for token, username := range tmp {
|
for token, username := range tmp {
|
||||||
tokens.Store(token, username)
|
tokens.Store(token, username)
|
||||||
// commenceSession(username, token)
|
// commenceSession(username, token)
|
||||||
}
|
}
|
||||||
log.Println("Found", len(tmp), "active sessions")
|
slog.Info("Indexed active sessions", "n", len(tmp))
|
||||||
}
|
}
|
||||||
|
|
||||||
// SaveUserDatabase stores current user credentials into JSON file by configured path.
|
// SaveUserDatabase stores current user credentials into JSON file by configured path.
|
||||||
@ -94,13 +100,13 @@ func dumpUserCredentials() error {
|
|||||||
|
|
||||||
blob, err := json.MarshalIndent(userList, "", "\t")
|
blob, err := json.MarshalIndent(userList, "", "\t")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
slog.Error("Failed to marshal users.json", "err", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = os.WriteFile(files.UserCredentialsJSON(), blob, 0666)
|
err = os.WriteFile(files.UserCredentialsJSON(), blob, 0666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
slog.Error("Failed to write users.json", "err", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,11 +125,11 @@ func dumpTokens() {
|
|||||||
|
|
||||||
blob, err := json.MarshalIndent(tmp, "", "\t")
|
blob, err := json.MarshalIndent(tmp, "", "\t")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
slog.Error("Failed to marshal tokens.json", "err", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = os.WriteFile(files.TokensJSON(), blob, 0666)
|
err = os.WriteFile(files.TokensJSON(), blob, 0666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("an error occurred in dumpTokens function:", err)
|
slog.Error("Failed to write tokens.json", "err", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -21,7 +21,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"log"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
@ -78,7 +78,7 @@ func init() {
|
|||||||
|
|
||||||
var strings map[string]string
|
var strings map[string]string
|
||||||
if err := json.Unmarshal(contents, &strings); err != nil {
|
if err := json.Unmarshal(contents, &strings); err != nil {
|
||||||
log.Fatalf("error while parsing %s: %v", path, err)
|
slog.Error("Failed to unmarshal localization file", "path", path, "err", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for key, value := range strings {
|
for key, value := range strings {
|
||||||
|
|||||||
4
main.go
4
main.go
@ -24,7 +24,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
parseCliArgs()
|
if err := parseCliArgs(); err != nil {
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
if err := files.PrepareWikiRoot(); err != nil {
|
if err := files.PrepareWikiRoot(); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
|||||||
@ -3,7 +3,6 @@ package misc
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"log"
|
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"mime"
|
"mime"
|
||||||
@ -74,11 +73,11 @@ func handlerReindex(w http.ResponseWriter, rq *http.Request) {
|
|||||||
if ok := user.CanProceed(rq, "reindex"); !ok {
|
if ok := user.CanProceed(rq, "reindex"); !ok {
|
||||||
var lc = l18n.FromRequest(rq)
|
var lc = l18n.FromRequest(rq)
|
||||||
viewutil.HttpErr(viewutil.MetaFrom(w, rq), http.StatusForbidden, cfg.HomeHypha, lc.Get("ui.reindex_no_rights"))
|
viewutil.HttpErr(viewutil.MetaFrom(w, rq), http.StatusForbidden, cfg.HomeHypha, lc.Get("ui.reindex_no_rights"))
|
||||||
log.Println("Rejected", rq.URL)
|
slog.Info("No rights to reindex")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
hyphae.ResetCount()
|
hyphae.ResetCount()
|
||||||
log.Println("Reindexing hyphae in", files.HyphaeDir())
|
slog.Info("Reindexing hyphae", "hyphaeDir", files.HyphaeDir())
|
||||||
hyphae.Index(files.HyphaeDir())
|
hyphae.Index(files.HyphaeDir())
|
||||||
backlinks.IndexBacklinks()
|
backlinks.IndexBacklinks()
|
||||||
http.Redirect(w, rq, "/", http.StatusSeeOther)
|
http.Redirect(w, rq, "/", http.StatusSeeOther)
|
||||||
@ -90,9 +89,10 @@ func handlerUpdateHeaderLinks(w http.ResponseWriter, rq *http.Request) {
|
|||||||
if ok := user.CanProceed(rq, "update-header-links"); !ok {
|
if ok := user.CanProceed(rq, "update-header-links"); !ok {
|
||||||
var lc = l18n.FromRequest(rq)
|
var lc = l18n.FromRequest(rq)
|
||||||
viewutil.HttpErr(viewutil.MetaFrom(w, rq), http.StatusForbidden, cfg.HomeHypha, lc.Get("ui.header_no_rights"))
|
viewutil.HttpErr(viewutil.MetaFrom(w, rq), http.StatusForbidden, cfg.HomeHypha, lc.Get("ui.header_no_rights"))
|
||||||
log.Println("Rejected", rq.URL)
|
slog.Info("No rights to update header links")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
slog.Info("Updated header links")
|
||||||
shroom.SetHeaderLinks()
|
shroom.SetHeaderLinks()
|
||||||
http.Redirect(w, rq, "/", http.StatusSeeOther)
|
http.Redirect(w, rq, "/", http.StatusSeeOther)
|
||||||
}
|
}
|
||||||
@ -149,7 +149,7 @@ func handlerStyle(w http.ResponseWriter, rq *http.Request) {
|
|||||||
}
|
}
|
||||||
_, err = io.Copy(w, file)
|
_, err = io.Copy(w, file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
slog.Error("Failed to write stylesheet; proceeding anyway", "err", err)
|
||||||
}
|
}
|
||||||
_ = file.Close()
|
_ = file.Close()
|
||||||
}
|
}
|
||||||
@ -164,7 +164,7 @@ func handlerRobotsTxt(w http.ResponseWriter, rq *http.Request) {
|
|||||||
}
|
}
|
||||||
_, err = io.Copy(w, file)
|
_, err = io.Copy(w, file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println()
|
slog.Error("Failed to write robots.txt; proceeding anyway", "err", err)
|
||||||
}
|
}
|
||||||
_ = file.Close()
|
_ = file.Close()
|
||||||
}
|
}
|
||||||
|
|||||||
15
web/web.go
15
web/web.go
@ -4,12 +4,7 @@ package web
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/bouncepaw/mycorrhiza/internal/cfg"
|
|
||||||
"github.com/bouncepaw/mycorrhiza/internal/user"
|
|
||||||
"github.com/bouncepaw/mycorrhiza/l18n"
|
|
||||||
"github.com/bouncepaw/mycorrhiza/web/viewutil"
|
|
||||||
"io"
|
"io"
|
||||||
"log"
|
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"mime"
|
"mime"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -19,11 +14,15 @@ import (
|
|||||||
"github.com/bouncepaw/mycorrhiza/help"
|
"github.com/bouncepaw/mycorrhiza/help"
|
||||||
"github.com/bouncepaw/mycorrhiza/history/histweb"
|
"github.com/bouncepaw/mycorrhiza/history/histweb"
|
||||||
"github.com/bouncepaw/mycorrhiza/hypview"
|
"github.com/bouncepaw/mycorrhiza/hypview"
|
||||||
|
"github.com/bouncepaw/mycorrhiza/internal/cfg"
|
||||||
|
"github.com/bouncepaw/mycorrhiza/internal/user"
|
||||||
"github.com/bouncepaw/mycorrhiza/interwiki"
|
"github.com/bouncepaw/mycorrhiza/interwiki"
|
||||||
|
"github.com/bouncepaw/mycorrhiza/l18n"
|
||||||
"github.com/bouncepaw/mycorrhiza/misc"
|
"github.com/bouncepaw/mycorrhiza/misc"
|
||||||
"github.com/gorilla/mux"
|
|
||||||
|
|
||||||
"github.com/bouncepaw/mycorrhiza/util"
|
"github.com/bouncepaw/mycorrhiza/util"
|
||||||
|
"github.com/bouncepaw/mycorrhiza/web/viewutil"
|
||||||
|
|
||||||
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Handler initializes and returns the HTTP router based on the configuration.
|
// Handler initializes and returns the HTTP router based on the configuration.
|
||||||
@ -308,7 +307,7 @@ func handlerTelegramLogin(w http.ResponseWriter, rq *http.Request) {
|
|||||||
|
|
||||||
errmsg := user.LoginDataHTTP(w, username, "")
|
errmsg := user.LoginDataHTTP(w, username, "")
|
||||||
if errmsg != nil {
|
if errmsg != nil {
|
||||||
log.Printf("Failed to login ‘%s’ using Telegram: %s", username, err.Error())
|
slog.Error("Failed to login using Telegram", "err", err, "username", username)
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
_, _ = io.WriteString(
|
_, _ = io.WriteString(
|
||||||
w,
|
w,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user