mirror of
https://github.com/osmarks/mycorrhiza.git
synced 2025-04-26 04:23:10 +00:00
Create a package for config
This commit is contained in:
parent
279a14cb07
commit
962ace319a
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
hypha
|
75
cfg/config.go
Normal file
75
cfg/config.go
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
package cfg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
WikiDir string
|
||||||
|
TemplatesDir string
|
||||||
|
configJsonPath string
|
||||||
|
|
||||||
|
// Default values that can be overriden in config.json
|
||||||
|
Address = "127.0.0.1:80"
|
||||||
|
TitleEditTemplate = `Edit %s`
|
||||||
|
TitleTemplate = `%s`
|
||||||
|
GenericErrorMsg = `<b>Sorry, something went wrong</b>`
|
||||||
|
|
||||||
|
DefaultTitle = "MycorrhizaWiki"
|
||||||
|
DefaultHeaderText = `MycorrhizaWiki 🍄`
|
||||||
|
DefaultFooterText = `This website runs <a href="https://github.com/bouncepaw/mycorrhiza">MycorrhizaWiki</a>.`
|
||||||
|
DefaultSidebar = ""
|
||||||
|
DefaultBodyBottom = ""
|
||||||
|
DefaultContent = "It is empty here"
|
||||||
|
DefaultStyles = `
|
||||||
|
<link rel="stylesheet" href="/sys/main.css?action=raw">
|
||||||
|
`
|
||||||
|
)
|
||||||
|
|
||||||
|
func InitConfig(wd string) bool {
|
||||||
|
log.Println("WikiDir is", wd)
|
||||||
|
WikiDir = wd
|
||||||
|
TemplatesDir = filepath.Join(filepath.Dir(WikiDir), "templates")
|
||||||
|
configJsonPath = filepath.Join(filepath.Dir(WikiDir), "config.json")
|
||||||
|
|
||||||
|
if _, err := os.Stat(configJsonPath); os.IsNotExist(err) {
|
||||||
|
log.Println("config.json not found, using default values")
|
||||||
|
} else {
|
||||||
|
log.Println("config.json found, overriding default values...")
|
||||||
|
return readConfig()
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func readConfig() bool {
|
||||||
|
configJsonContents, err := ioutil.ReadFile(configJsonPath)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Error when reading config.json:", err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg := struct {
|
||||||
|
Address string `json:"address"`
|
||||||
|
TitleTemplates struct {
|
||||||
|
EditHypha string `json:"edit-hypha"`
|
||||||
|
ViewHypha string `json:"view-hypha"`
|
||||||
|
} `json:"title-templates"`
|
||||||
|
}{}
|
||||||
|
|
||||||
|
err = json.Unmarshal(configJsonContents, &cfg)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Error when parsing config.json:", err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
Address = cfg.Address
|
||||||
|
TitleEditTemplate = cfg.TitleTemplates.EditHypha
|
||||||
|
TitleTemplate = cfg.TitleTemplates.ViewHypha
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
16
config.go
16
config.go
@ -1,16 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
const (
|
|
||||||
TitleEditTemplate = `Edit %s at MycorrhizaWiki`
|
|
||||||
TitleTemplate = `%s at MycorrhizaWiki`
|
|
||||||
DefaultTitle = "MycorrhizaWiki"
|
|
||||||
DefaultHeaderText = `MycorrhizaWiki 🍄`
|
|
||||||
DefaultFooterText = `This website runs <a href="https://github.com/bouncepaw/mycorrhiza">MycorrhizaWiki</a>.`
|
|
||||||
DefaultSidebar = ""
|
|
||||||
DefaultBodyBottom = ""
|
|
||||||
DefaultContent = "It is empty here"
|
|
||||||
DefaultStyles = `
|
|
||||||
<link rel="stylesheet" href="/sys/main.css?action=raw">
|
|
||||||
`
|
|
||||||
GenericErrorMsg = `<b>Sorry, something went wrong</b>`
|
|
||||||
)
|
|
@ -9,6 +9,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/bouncepaw/mycorrhiza/cfg"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -95,7 +96,7 @@ func getHypha(name string) (*Hypha, bool) {
|
|||||||
log.Println("Create hypha", name)
|
log.Println("Create hypha", name)
|
||||||
h := &Hypha{
|
h := &Hypha{
|
||||||
FullName: name,
|
FullName: name,
|
||||||
Path: filepath.Join(rootWikiDir, name),
|
Path: filepath.Join(cfg.WikiDir, name),
|
||||||
Revisions: make(map[string]*Revision),
|
Revisions: make(map[string]*Revision),
|
||||||
parentName: filepath.Dir(name),
|
parentName: filepath.Dir(name),
|
||||||
}
|
}
|
||||||
@ -177,7 +178,7 @@ func HandlerUpdate(w http.ResponseWriter, rq *http.Request) {
|
|||||||
} else {
|
} else {
|
||||||
h = &Hypha{
|
h = &Hypha{
|
||||||
FullName: vars["hypha"],
|
FullName: vars["hypha"],
|
||||||
Path: filepath.Join(rootWikiDir, vars["hypha"]),
|
Path: filepath.Join(cfg.WikiDir, vars["hypha"]),
|
||||||
Revisions: make(map[string]*Revision),
|
Revisions: make(map[string]*Revision),
|
||||||
parentName: filepath.Dir(vars["hypha"]),
|
parentName: filepath.Dir(vars["hypha"]),
|
||||||
}
|
}
|
||||||
|
4
hypha.go
4
hypha.go
@ -10,6 +10,8 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/bouncepaw/mycorrhiza/cfg"
|
||||||
)
|
)
|
||||||
|
|
||||||
// `Hypha` represents a hypha. It is the thing MycorrhizaWiki generally serves.
|
// `Hypha` represents a hypha. It is the thing MycorrhizaWiki generally serves.
|
||||||
@ -95,7 +97,7 @@ func ActionEdit(hyphaName string, w http.ResponseWriter) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Could not read", newestRev.TextPath)
|
log.Println("Could not read", newestRev.TextPath)
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
w.Write([]byte(GenericErrorMsg))
|
w.Write([]byte(cfg.GenericErrorMsg))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
initContents = string(contents)
|
initContents = string(contents)
|
||||||
|
15
main.go
15
main.go
@ -8,6 +8,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/bouncepaw/mycorrhiza/cfg"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -36,10 +37,6 @@ func RevInMap(m map[string]string) string {
|
|||||||
return "0"
|
return "0"
|
||||||
}
|
}
|
||||||
|
|
||||||
// `rootWikiDir` is a directory where all wiki files reside.
|
|
||||||
// `templatesDir` is where templates are.
|
|
||||||
var rootWikiDir, templatesDir string
|
|
||||||
|
|
||||||
// `hyphae` is a map with all hyphae. Many functions use it.
|
// `hyphae` is a map with all hyphae. Many functions use it.
|
||||||
var hyphae map[string]*Hypha
|
var hyphae map[string]*Hypha
|
||||||
|
|
||||||
@ -47,17 +44,15 @@ func main() {
|
|||||||
if len(os.Args) == 1 {
|
if len(os.Args) == 1 {
|
||||||
panic("Expected a root wiki pages directory")
|
panic("Expected a root wiki pages directory")
|
||||||
}
|
}
|
||||||
// Required so the rootWikiDir hereinbefore does not get redefined.
|
wikiDir, err := filepath.Abs(os.Args[1])
|
||||||
var err error
|
|
||||||
rootWikiDir, err = filepath.Abs(os.Args[1])
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
templatesDir = filepath.Join(filepath.Dir(rootWikiDir), "templates")
|
|
||||||
|
|
||||||
log.Println("Welcome to MycorrhizaWiki α")
|
log.Println("Welcome to MycorrhizaWiki α")
|
||||||
|
cfg.InitConfig(wikiDir)
|
||||||
log.Println("Indexing hyphae...")
|
log.Println("Indexing hyphae...")
|
||||||
hyphae = recurFindHyphae(rootWikiDir)
|
hyphae = recurFindHyphae(wikiDir)
|
||||||
log.Println("Indexed", len(hyphae), "hyphae. Ready to accept requests.")
|
log.Println("Indexed", len(hyphae), "hyphae. Ready to accept requests.")
|
||||||
|
|
||||||
// Start server code. See handlers.go for handlers' implementations.
|
// Start server code. See handlers.go for handlers' implementations.
|
||||||
@ -123,7 +118,7 @@ func main() {
|
|||||||
|
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
Handler: r,
|
Handler: r,
|
||||||
Addr: "127.0.0.1:8000",
|
Addr: cfg.Address,
|
||||||
// Good practice: enforce timeouts for servers you create!
|
// Good practice: enforce timeouts for servers you create!
|
||||||
WriteTimeout: 15 * time.Second,
|
WriteTimeout: 15 * time.Second,
|
||||||
ReadTimeout: 15 * time.Second,
|
ReadTimeout: 15 * time.Second,
|
||||||
|
BIN
mycorrhiza
Executable file
BIN
mycorrhiza
Executable file
Binary file not shown.
26
render.go
26
render.go
@ -5,12 +5,14 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"path"
|
"path"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
|
"github.com/bouncepaw/mycorrhiza/cfg"
|
||||||
)
|
)
|
||||||
|
|
||||||
// EditHyphaPage returns HTML page of hypha editor.
|
// EditHyphaPage returns HTML page of hypha editor.
|
||||||
func EditHyphaPage(name, textMime, content, tags string) string {
|
func EditHyphaPage(name, textMime, content, tags string) string {
|
||||||
keys := map[string]string{
|
keys := map[string]string{
|
||||||
"Title": fmt.Sprintf(TitleEditTemplate, name),
|
"Title": fmt.Sprintf(cfg.TitleEditTemplate, name),
|
||||||
"Header": renderFromString(name, "Hypha/edit/header.html"),
|
"Header": renderFromString(name, "Hypha/edit/header.html"),
|
||||||
}
|
}
|
||||||
page := map[string]string{
|
page := map[string]string{
|
||||||
@ -34,14 +36,14 @@ func HyphaPage(rev Revision, content string) string {
|
|||||||
|
|
||||||
// HyphaGeneric is used when building renderers for all types of hypha pages
|
// HyphaGeneric is used when building renderers for all types of hypha pages
|
||||||
func HyphaGeneric(name string, content, templatePath string) string {
|
func HyphaGeneric(name string, content, templatePath string) string {
|
||||||
sidebar := DefaultSidebar
|
sidebar := cfg.DefaultSidebar
|
||||||
if bside := renderFromMap(map[string]string{
|
if bside := renderFromMap(map[string]string{
|
||||||
"Tree": GetTree(name, true).AsHtml(),
|
"Tree": GetTree(name, true).AsHtml(),
|
||||||
}, "Hypha/view/sidebar.html"); bside != "" {
|
}, "Hypha/view/sidebar.html"); bside != "" {
|
||||||
sidebar = bside
|
sidebar = bside
|
||||||
}
|
}
|
||||||
keys := map[string]string{
|
keys := map[string]string{
|
||||||
"Title": fmt.Sprintf(TitleTemplate, name),
|
"Title": fmt.Sprintf(cfg.TitleTemplate, name),
|
||||||
"Sidebar": sidebar,
|
"Sidebar": sidebar,
|
||||||
}
|
}
|
||||||
return renderBase(renderFromString(content, templatePath), keys)
|
return renderBase(renderFromString(content, templatePath), keys)
|
||||||
@ -53,13 +55,13 @@ func HyphaGeneric(name string, content, templatePath string) string {
|
|||||||
// keys: map with replaced standart fields
|
// keys: map with replaced standart fields
|
||||||
func renderBase(content string, keys map[string]string) string {
|
func renderBase(content string, keys map[string]string) string {
|
||||||
page := map[string]string{
|
page := map[string]string{
|
||||||
"Title": DefaultTitle,
|
"Title": cfg.DefaultTitle,
|
||||||
"Head": DefaultStyles,
|
"Head": cfg.DefaultStyles,
|
||||||
"Sidebar": DefaultSidebar,
|
"Sidebar": cfg.DefaultSidebar,
|
||||||
"Main": DefaultContent,
|
"Main": cfg.DefaultContent,
|
||||||
"BodyBottom": DefaultBodyBottom,
|
"BodyBottom": cfg.DefaultBodyBottom,
|
||||||
"Header": renderFromString(DefaultHeaderText, "header.html"),
|
"Header": renderFromString(cfg.DefaultHeaderText, "header.html"),
|
||||||
"Footer": renderFromString(DefaultFooterText, "footer.html"),
|
"Footer": renderFromString(cfg.DefaultFooterText, "footer.html"),
|
||||||
}
|
}
|
||||||
for key, val := range keys {
|
for key, val := range keys {
|
||||||
page[key] = val
|
page[key] = val
|
||||||
@ -70,7 +72,7 @@ func renderBase(content string, keys map[string]string) string {
|
|||||||
|
|
||||||
// renderFromMap applies `data` map to template in `templatePath` and returns the result.
|
// renderFromMap applies `data` map to template in `templatePath` and returns the result.
|
||||||
func renderFromMap(data map[string]string, templatePath string) string {
|
func renderFromMap(data map[string]string, templatePath string) string {
|
||||||
filePath := path.Join(templatesDir, templatePath)
|
filePath := path.Join(cfg.TemplatesDir, templatePath)
|
||||||
tmpl, err := template.ParseFiles(filePath)
|
tmpl, err := template.ParseFiles(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err.Error()
|
return err.Error()
|
||||||
@ -84,7 +86,7 @@ func renderFromMap(data map[string]string, templatePath string) string {
|
|||||||
|
|
||||||
// renderFromMap applies `data` string to template in `templatePath` and returns the result.
|
// renderFromMap applies `data` string to template in `templatePath` and returns the result.
|
||||||
func renderFromString(data string, templatePath string) string {
|
func renderFromString(data string, templatePath string) string {
|
||||||
filePath := path.Join(templatesDir, templatePath)
|
filePath := path.Join(cfg.TemplatesDir, templatePath)
|
||||||
tmpl, err := template.ParseFiles(filePath)
|
tmpl, err := template.ParseFiles(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err.Error()
|
return err.Error()
|
||||||
|
7
w/config.json
Normal file
7
w/config.json
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"address": "127.0.0.1:1737",
|
||||||
|
"title-templates": {
|
||||||
|
"edit-hypha": "Edit %s at MycorrhizaWiki",
|
||||||
|
"view-hypha": "%s at MycorrhizaWiki"
|
||||||
|
}
|
||||||
|
}
|
Before Width: | Height: | Size: 608 KiB After Width: | Height: | Size: 608 KiB |
6
walk.go
6
walk.go
@ -7,6 +7,8 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/bouncepaw/mycorrhiza/cfg"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -58,8 +60,8 @@ func scanHyphaDir(fullPath string) (valid bool, possibleSubhyphae []string, meta
|
|||||||
|
|
||||||
// hyphaName gets name of a hypha by stripping path to the hypha in `fullPath`
|
// hyphaName gets name of a hypha by stripping path to the hypha in `fullPath`
|
||||||
func hyphaName(fullPath string) string {
|
func hyphaName(fullPath string) string {
|
||||||
// {rootWikiDir}/{the name}
|
// {cfg.WikiDir}/{the name}
|
||||||
return fullPath[len(rootWikiDir)+1:]
|
return fullPath[len(cfg.WikiDir)+1:]
|
||||||
}
|
}
|
||||||
|
|
||||||
// recurFindHyphae recursively searches for hyphae in passed directory path.
|
// recurFindHyphae recursively searches for hyphae in passed directory path.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user