This commit is contained in:
Iris Lightshard 2022-05-29 00:26:36 -06:00
parent b7e2dc3ab7
commit bea90e0e1c
Signed by: Iris Lightshard
GPG key ID: 3B7FBC22144E6398
6 changed files with 249 additions and 252 deletions

View file

@ -1,16 +1,16 @@
package adapter
import (
"nilfm.cc/git/nirvash/page"
"nilfm.cc/git/nirvash/page"
)
type Adapter interface {
Name() string
GetConfig(key string) (interface{}, error)
SetConfig(key string, value interface{}) error
ListPages() map[string]string
GetPage(string) page.Page
FormatPage(string) string
FormattingHelp() string
Build()
}
Name() string
GetConfig(key string) (interface{}, error)
SetConfig(key string, value interface{}) error
ListPages() map[string]string
GetPage(string) page.Page
FormatPage(string) string
FormattingHelp() string
Build()
}

View file

@ -1,41 +1,41 @@
package adapter
import (
"nilfm.cc/git/nirvash/page"
"nilfm.cc/git/nirvash/page"
)
type EurekaAdapter struct {
Root string
Root string
}
func (self *EurekaAdapter) Name() string {
return "eureka"
return "eureka"
}
func (self *EurekaAdapter) GetConfig(key string) (interface{}, error) {
return nil, nil
return nil, nil
}
func (self *EurekaAdapter) SetConfig(key string, value interface{}) error {
return nil
return nil
}
func (self *EurekaAdapter) ListPages() map[string]string {
return map[string]string{}
return map[string]string{}
}
func (self *EurekaAdapter) GetPage(path string) page.Page {
return page.Page{}
return page.Page{}
}
func (self *EurekaAdapter) FormatPage(raw string) string {
return raw
return raw
}
func (self *EurekaAdapter) FormattingHelp() string {
return "help!"
return "help!"
}
func (self *EurekaAdapter) Build() {
return
}
return
}

View file

@ -1,61 +1,61 @@
package cmd
import (
"fmt"
"strings"
"nilfm.cc/git/quartzgun/auth"
"nilfm.cc/git/nirvash/config"
"fmt"
"nilfm.cc/git/nirvash/config"
"nilfm.cc/git/quartzgun/auth"
"strings"
)
func Process(args []string, userStore auth.UserStore, cfg *config.Config) bool {
if len(args) == 1 {
return false
}
switch args[1] {
case "adduser":
if len(args) < 4 {
return help()
}
userStore.AddUser(args[2], args[3])
case "rmuser":
if len(args) < 3 {
return help()
}
userStore.DeleteUser(args[2])
case "passwd":
if len(args) < 5 {
return help()
}
userStore.ChangePassword(args[2], args[3], args[4])
case "configure":
fmt.Printf("configuring\n")
for _, token := range args[2:] {
kvp := strings.Split(token, "=")
k := kvp[0]
v := kvp[1]
fmt.Printf("%s = %s\n", k, v)
switch k {
case "adapter":
config.SetAdapter(cfg, v)
case "root":
cfg.Root = v
case "assetRoot":
cfg.AssetRoot = v
case "staticRoot":
cfg.StaticRoot = v
case "plugins":
// handle plugins later
default:
panic("unknown configuration option: " + v)
}
}
config.Write(cfg)
default:
help()
}
return true
if len(args) == 1 {
return false
}
switch args[1] {
case "adduser":
if len(args) < 4 {
return help()
}
userStore.AddUser(args[2], args[3])
case "rmuser":
if len(args) < 3 {
return help()
}
userStore.DeleteUser(args[2])
case "passwd":
if len(args) < 5 {
return help()
}
userStore.ChangePassword(args[2], args[3], args[4])
case "configure":
fmt.Printf("configuring\n")
for _, token := range args[2:] {
kvp := strings.Split(token, "=")
k := kvp[0]
v := kvp[1]
fmt.Printf("%s = %s\n", k, v)
switch k {
case "adapter":
config.SetAdapter(cfg, v)
case "root":
cfg.Root = v
case "assetRoot":
cfg.AssetRoot = v
case "staticRoot":
cfg.StaticRoot = v
case "plugins":
// handle plugins later
default:
panic("unknown configuration option: " + v)
}
}
config.Write(cfg)
default:
help()
}
return true
}
func help() bool {
return true
return true
}

View file

@ -1,173 +1,170 @@
package config
import (
"fmt"
"runtime"
"os"
"strings"
"path/filepath"
"nilfm.cc/git/nirvash/adapter"
"fmt"
"nilfm.cc/git/nirvash/adapter"
"os"
"path/filepath"
"runtime"
"strings"
)
type Config struct {
Adapter adapter.Adapter // adapter for this instance
Root string // root of the site data
StaticRoot string // root of static files for StaticFileManager
AssetRoot string // root of Nirvash dist files (CSS, images)
Plugins map[string]interface{}
Adapter adapter.Adapter // adapter for this instance
Root string // root of the site data
StaticRoot string // root of static files for StaticFileManager
AssetRoot string // root of Nirvash dist files (CSS, images)
Plugins map[string]interface{}
}
func GetConfigLocation() string {
home := os.Getenv("HOME")
appdata := os.Getenv("APPDATA")
switch (runtime.GOOS) {
case "windows":
return filepath.Join(appdata, "nirvash")
case "darwin":
return filepath.Join(home, "Library", "Application Support", "nirvash")
case "plan9":
return filepath.Join(home, "lib", "nirvash")
default:
return filepath.Join(home, ".config", "nirvash")
}
home := os.Getenv("HOME")
appdata := os.Getenv("APPDATA")
switch runtime.GOOS {
case "windows":
return filepath.Join(appdata, "nirvash")
case "darwin":
return filepath.Join(home, "Library", "Application Support", "nirvash")
case "plan9":
return filepath.Join(home, "lib", "nirvash")
default:
return filepath.Join(home, ".config", "nirvash")
}
}
func ensureConfigLocationExists() {
_, err := os.Stat(GetConfigLocation())
if os.IsNotExist(err) {
os.MkdirAll(GetConfigLocation(), os.ModePerm)
}
_, err := os.Stat(GetConfigLocation())
if os.IsNotExist(err) {
os.MkdirAll(GetConfigLocation(), os.ModePerm)
}
}
func Read() *Config {
ensureConfigLocationExists()
ensureConfigLocationExists()
return parseConfig(filepath.Join(GetConfigLocation(), "nirvash.conf"))
}
func Write(cfg *Config) error {
ensureConfigLocationExists()
return writeConfig(cfg, filepath.Join(GetConfigLocation(), "nirvash.conf"))
ensureConfigLocationExists()
return writeConfig(cfg, filepath.Join(GetConfigLocation(), "nirvash.conf"))
}
func SetAdapter(cfg *Config, adptr string) {
switch adptr {
case "eureka":
cfg.Adapter = &adapter.EurekaAdapter{}
default:
panic("Unsupported adapter! Try one of [ eureka ]")
}
switch adptr {
case "eureka":
cfg.Adapter = &adapter.EurekaAdapter{}
default:
panic("Unsupported adapter! Try one of [ eureka ]")
}
}
func IsNull(cfg *Config) bool {
return cfg.Adapter == nil || len(cfg.Root) == 0 || len(cfg.StaticRoot) == 0 || len(cfg.AssetRoot) == 0
return cfg.Adapter == nil || len(cfg.Root) == 0 || len(cfg.StaticRoot) == 0 || len(cfg.AssetRoot) == 0
}
func RunWizard(cfg *Config) {
fmt.Printf("All options are required.\n")
defer func(cfg *Config) {
if r := recover(); r != nil {
fmt.Printf("Invalid selection, starting over...")
RunWizard(cfg)
}
}(cfg)
inputBuf := ""
fmt.Printf("adapter? (eureka) [eureka] ")
fmt.Scanln(&inputBuf)
if len(strings.TrimSpace(inputBuf)) == 0 {
inputBuf = "eureka"
}
SetAdapter(cfg, inputBuf)
inputBuf = ""
fmt.Printf("site data root? ")
ensureNonEmptyOption(&inputBuf)
cfg.Root = inputBuf
inputBuf = ""
fmt.Printf("static file root? ")
ensureNonEmptyOption(&inputBuf)
cfg.StaticRoot = inputBuf
inputBuf = ""
fmt.Printf("nirvash asset root? ")
ensureNonEmptyOption(&inputBuf)
cfg.AssetRoot = inputBuf
inputBuf = ""
fmt.Printf("plugins? (not implemented yet) ")
ensureNonEmptyOption(&inputBuf)
//cfg.Plugins = processPlugins(inputBuf)
fmt.Printf("Configuration complete!\n")
Write(cfg)
fmt.Printf("All options are required.\n")
defer func(cfg *Config) {
if r := recover(); r != nil {
fmt.Printf("Invalid selection, starting over...")
RunWizard(cfg)
}
}(cfg)
inputBuf := ""
fmt.Printf("adapter? (eureka) [eureka] ")
fmt.Scanln(&inputBuf)
if len(strings.TrimSpace(inputBuf)) == 0 {
inputBuf = "eureka"
}
SetAdapter(cfg, inputBuf)
inputBuf = ""
fmt.Printf("site data root? ")
ensureNonEmptyOption(&inputBuf)
cfg.Root = inputBuf
inputBuf = ""
fmt.Printf("static file root? ")
ensureNonEmptyOption(&inputBuf)
cfg.StaticRoot = inputBuf
inputBuf = ""
fmt.Printf("nirvash asset root? ")
ensureNonEmptyOption(&inputBuf)
cfg.AssetRoot = inputBuf
inputBuf = ""
fmt.Printf("plugins? (not implemented yet) ")
ensureNonEmptyOption(&inputBuf)
//cfg.Plugins = processPlugins(inputBuf)
fmt.Printf("Configuration complete!\n")
Write(cfg)
}
func ensureNonEmptyOption(buffer *string) {
for ;; {
fmt.Scanln(buffer)
if len(strings.TrimSpace(*buffer)) != 0 {
break
}
}
for {
fmt.Scanln(buffer)
if len(strings.TrimSpace(*buffer)) != 0 {
break
}
}
}
func writeConfig(cfg *Config, configFile string) error {
f, err := os.Create(configFile)
if err != nil {
return err
}
defer f.Close()
f.WriteString("root=" + cfg.Root + "\n")
f.WriteString("staticRoot=" + cfg.StaticRoot + "\n")
f.WriteString("assetRoot=" + cfg.AssetRoot + "\n")
f.WriteString("adapter=" + cfg.Adapter.Name() + "\n")
f.WriteString("plugins=\n")
return nil
f, err := os.Create(configFile)
if err != nil {
return err
}
defer f.Close()
f.WriteString("root=" + cfg.Root + "\n")
f.WriteString("staticRoot=" + cfg.StaticRoot + "\n")
f.WriteString("assetRoot=" + cfg.AssetRoot + "\n")
f.WriteString("adapter=" + cfg.Adapter.Name() + "\n")
f.WriteString("plugins=\n")
return nil
}
func parseConfig(configFile string) *Config {
f, err := os.ReadFile(configFile)
cfg := &Config{}
if err != nil {
return cfg
}
fileData := string(f[:])
lines := strings.Split(fileData, "\n")
for _, l := range lines {
if len(l) == 0 {
continue
}
if !strings.Contains(l, "=") {
panic("Malformed config not in INI format")
}
kvp := strings.Split(l, "=")
k := strings.TrimSpace(kvp[0])
v := strings.TrimSpace(kvp[1])
switch k {
case "root":
cfg.Root = v
case "staticRoot":
cfg.StaticRoot = v
case "assetRoot":
cfg.AssetRoot = v
case "plugins":
// not implemented
case "adapter":
SetAdapter(cfg, v)
default:
panic("Unrecognized config option: " + k)
}
}
return cfg
f, err := os.ReadFile(configFile)
cfg := &Config{}
if err != nil {
return cfg
}
fileData := string(f[:])
lines := strings.Split(fileData, "\n")
for _, l := range lines {
if len(l) == 0 {
continue
}
if !strings.Contains(l, "=") {
panic("Malformed config not in INI format")
}
kvp := strings.Split(l, "=")
k := strings.TrimSpace(kvp[0])
v := strings.TrimSpace(kvp[1])
switch k {
case "root":
cfg.Root = v
case "staticRoot":
cfg.StaticRoot = v
case "assetRoot":
cfg.AssetRoot = v
case "plugins":
// not implemented
case "adapter":
SetAdapter(cfg, v)
default:
panic("Unrecognized config option: " + k)
}
}
return cfg
}

View file

@ -1,46 +1,46 @@
package main
import (
"os"
"path/filepath"
"net/http"
"nilfm.cc/git/quartzgun/indentalUserDB"
"nilfm.cc/git/quartzgun/router"
"nilfm.cc/git/quartzgun/renderer"
"nilfm.cc/git/quartzgun/middleware"
"nilfm.cc/git/nirvash/cmd"
"nilfm.cc/git/nirvash/config"
"net/http"
"nilfm.cc/git/nirvash/cmd"
"nilfm.cc/git/nirvash/config"
"nilfm.cc/git/quartzgun/indentalUserDB"
"nilfm.cc/git/quartzgun/middleware"
"nilfm.cc/git/quartzgun/renderer"
"nilfm.cc/git/quartzgun/router"
"os"
"path/filepath"
)
func main() {
cfg := config.Read()
udb := indentalUserDB.CreateIndentalUserDB(
filepath.Join(
config.GetConfigLocation(),
"user.db"))
if cmd.Process(os.Args, udb, cfg) {
os.Exit(0)
}
if config.IsNull(cfg) {
config.RunWizard(cfg)
}
rtr := &router.Router{
StaticPaths: map[string]string{
"/static": cfg.AssetRoot,
},
}
rtr.Get("/login", renderer.Template(
"templates/login.html"))
rtr.Post("/login", middleware.Authorize("/", udb, "/login?tryagain=1"))
rtr.Get("/", middleware.Protected(
renderer.Template(
"templates/cms_list.html",
"templates/header.html",
"templates/footer.html"), http.MethodGet, udb, "/login"))
http.ListenAndServe(":8080", rtr)
}
cfg := config.Read()
udb := indentalUserDB.CreateIndentalUserDB(
filepath.Join(
config.GetConfigLocation(),
"user.db"))
if cmd.Process(os.Args, udb, cfg) {
os.Exit(0)
}
if config.IsNull(cfg) {
config.RunWizard(cfg)
}
rtr := &router.Router{
StaticPaths: map[string]string{
"/static": cfg.AssetRoot,
},
}
rtr.Get("/login", renderer.Template(
"templates/login.html"))
rtr.Post("/login", middleware.Authorize("/", udb, "/login?tryagain=1"))
rtr.Get("/", middleware.Protected(
renderer.Template(
"templates/cms_list.html",
"templates/header.html",
"templates/footer.html"), http.MethodGet, udb, "/login"))
http.ListenAndServe(":8080", rtr)
}

View file

@ -1,11 +1,11 @@
package page
import (
"time"
"time"
)
type Page struct {
Title string
Content string
Edited time.Time
}
Title string
Content string
Edited time.Time
}