Browse Source

Added createpost using femto, added beginnings of mongodb, fleshed out config

Signed-off-by: gryffyn <me@neveris.one>
main
gryffyn 5 months ago
parent
commit
1f2c9d0bbd
Signed by: gryffyn GPG Key ID: 6948DD6514D02BEF
  1. 7
      ghcms.go
  2. 9
      go.mod
  3. 87
      ui/createpost.go
  4. 34
      ui/ui.go
  5. 63
      utils/config.go
  6. 23
      utils/mongodb.go
  7. 39
      utils/sqlite.go
  8. 5
      utils/user.go

7
ghcms.go

@ -2,9 +2,14 @@ package main
import (
_ "fmt"
"git.micah.mp/gryffyn/ghcms/ui"
"git.neveris.one/gryffyn/ghcms/ui"
"git.neveris.one/gryffyn/ghcms/utils"
)
func main() {
if !utils.FirstRun() {
utils.Setup()
}
ui.Run()
}

9
go.mod

@ -1,8 +1,13 @@
module git.micah.mp/gryffyn/ghcms
module git.neveris.one/gryffyn/ghcms
go 1.15
require (
git.neveris.one/gryffyn/femto v0.0.0-20210112104532-ddd6b1243814
github.com/andskur/argon2-hashing v0.1.3
github.com/rivo/tview v0.0.0-20201118063654-f007e9ad3893
github.com/gdamore/tcell/v2 v2.1.0
gitlab.com/tslocum/cview v1.5.2
go.mongodb.org/mongo-driver v1.4.4
gopkg.in/yaml.v2 v2.4.0
modernc.org/sqlite v1.7.5
)

87
ui/createpost.go

@ -0,0 +1,87 @@
package ui
import (
"io/ioutil"
"log"
"os"
"git.neveris.one/gryffyn/femto"
"git.neveris.one/gryffyn/femto/runtime"
"github.com/gdamore/tcell/v2"
"gitlab.com/tslocum/cview"
)
func saveBuffer(b *femto.Buffer, path string) error {
return ioutil.WriteFile(path, []byte(b.String()), 0600)
}
func main() {
path := os.Args[1]
content, err := ioutil.ReadFile(path)
if err != nil {
log.Fatalf("could not read %v: %v", path, err)
}
app := cview.NewApplication()
app.EnableMouse(true)
newPrimitive := func(text string) cview.Primitive {
tv := cview.NewTextView()
tv.SetTextAlign(cview.AlignCenter)
tv.SetText(text)
return tv
}
// main := newPrimitive("Main content")
buffer := femto.NewBufferFromString(string(content), path)
editor := femto.NewView(buffer)
editor.SetRuntimeFiles(runtime.Files)
editor.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyCtrlS:
_ = saveBuffer(buffer, path)
return nil
case tcell.KeyCtrlQ:
app.Stop()
return nil
}
return event
})
var colorscheme femto.Colorscheme
if monokai := runtime.Files.FindFile(femto.RTColorscheme, "monokai"); monokai != nil {
if data, err := monokai.Data(); err == nil {
colorscheme = femto.ParseColorscheme(string(data))
}
}
editor.SetColorscheme(colorscheme)
grid := cview.NewGrid()
grid.SetRows(9, 0, 3)
grid.SetColumns(0, 100)
grid.SetBorders(true)
grid.AddItem(newPrimitive("Footer"), 2, 0, 1, 3, 0, 0, false)
form := cview.NewForm()
form.AddInputField("title", "", 35, nil, nil)
form.AddInputField("author", "", 20, nil, nil)
form.AddInputField("description", "", 35, nil, nil)
form.AddCheckBox("show metadata", "", true, nil)
form.AddInputField("tags (comma sep)", "", 35, nil, nil)
form.AddButton("Save", nil)
form.AddButton("Quit", func() {
app.Stop()
})
form.SetTitle("new post")
form.SetBorder(false)
// Layout for screens wider than 100 cells.
grid.AddItem(editor, 0, 0, 2, 2, 0, 0, false)
grid.AddItem(form, 0, 2, 2, 1, 0, 0, false)
app.SetRoot(grid, true)
app.SetFocus(editor)
if err := app.Run(); err != nil {
panic(err)
}
}

34
ui/ui.go

@ -1,8 +1,8 @@
package ui
import (
"git.micah.mp/gryffyn/ghcms/utils"
"github.com/rivo/tview"
"git.neveris.one/gryffyn/ghcms/utils"
"gitlab.com/tslocum/cview"
)
func Run() { loginUi() }
@ -14,19 +14,23 @@ type Login struct {
func loginUi() Login {
login := Login{}
app := tview.NewApplication()
form := tview.NewForm().
AddInputField("username", "", 20, nil, func(s string) { login.username = s }).
AddPasswordField("Password", "", 20, '*', func(s string) {
h := utils.Hashpw(s)
login.password = h
}).
AddButton("login", nil).
AddButton("exit", func() {
app.Stop()
})
form.SetBorder(true).SetTitle("Paperchain Login").SetTitleAlign(tview.AlignLeft)
if err := app.SetRoot(form, true).SetFocus(form).Run(); err != nil {
app := cview.NewApplication()
form := cview.NewForm()
form.AddInputField("username", "", 20, nil, func(s string) { login.username = s })
form.AddPasswordField("Password", "", 20, '*', func(s string) {
h := utils.HashPw(s)
login.password = h
})
form.AddButton("login", nil)
form.AddButton("exit", func() {
app.Stop()
})
form.SetBorder(true)
form.SetTitle("Paperchain Login")
form.SetTitleAlign(cview.AlignLeft)
app.SetRoot(form, true)
app.SetFocus(form)
if err := app.Run(); err != nil {
panic(err)
}
return login

63
utils/config.go

@ -0,0 +1,63 @@
package utils
import (
"io/ioutil"
"log"
"os"
"gopkg.in/yaml.v2"
)
type Config struct {
Test string `yaml:"test"`
}
func ifile(filename string) ([]byte, error) {
content, err := ioutil.ReadFile(filename)
str := content
return str, err
}
func ofile(filename string, data []byte) error {
err := ioutil.WriteFile(filename, data, os.FileMode.Perm(0644))
return err
}
var home, _ = os.UserHomeDir()
var cdir = home + "/.config/ghcms"
var ddir = home + "/.local/share/ghcms"
var cfile = cdir + "/config.yml"
func Setup() {
if _, err := os.Stat(home); os.IsExist(err) {
if err := os.Mkdir(cdir, 0775); err != nil {
log.Fatal(err)
}
if err := os.Mkdir(ddir, 0775); err != nil {
log.Fatal(err)
}
}
}
func FirstRun() bool {
if _, err := os.Stat(cdir); os.IsNotExist(err) {
return true
}
return false
}
func WriteConfig() {
conf := Config{}
confm, _ := yaml.Marshal(&conf)
if FirstRun() {
Setup()
}
_ = ofile(cfile, confm)
}
func GetConfig() (Config, error) {
conf := Config{}
in, err := ifile(cfile)
yaml.Unmarshal(in, conf)
return conf, err
}

23
utils/mongodb.go

@ -0,0 +1,23 @@
package utils
import (
"context"
"log"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func InitMongo() {
client, err := mongo.NewClient(options.Client().ApplyURI("mongodb://localhost"))
if err != nil {
log.Fatal(err)
}
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
err = client.Connect(ctx)
if err != nil {
log.Fatal(err)
}
defer client.Disconnect(ctx)
}

39
utils/sqlite.go

@ -0,0 +1,39 @@
package utils
import (
"database/sql"
"log"
"os"
_ "modernc.org/sqlite"
)
func InitSqlite() {
loc, _ := os.UserHomeDir()
db, err := sql.Open("sqlite", loc+"/.local/share/ghcms/data.db")
if err != nil {
log.Fatal(err)
}
_, _ = db.Exec("PRAGMA foreign_keys = ON;")
createTables(db)
}
func createTables(db *sql.DB) {
c_users, _ := db.Prepare(
"CREATE TABLE IF NOT EXISTS users (user_id INTEGER PRIMARY KEY, username TEXT NOT NULL, firstname TEXT, " +
"lastname TEXT NOT NULL, email TEXT, password TEXT NOT NULL)")
c_sites, _ := db.Prepare(
"CREATE TABLE IF NOT EXISTS sites (site_id INTEGER PRIMARY KEY, sitename TEXT NOT NULL, desc TEXT, " +
"last_accessed TEXT, first_accessed TEXT, git_url TEXT, local_path TEXT)")
c_usersite, _ := db.Prepare(
"CREATE TABLE IF NOT EXISTS u_users_sites (id INTEGER PRIMARY KEY, user_id INTEGER NOT NULL, " +
"site_id NOT NULL, FOREIGN KEY (user_id) REFERENCES users (user_id) ON UPDATE CASCADE ON DELETE CASCADE, " +
"FOREIGN KEY (site_id) REFERENCES sites (site_id) ON UPDATE CASCADE ON DELETE CASCADE)")
_, _ = c_users.Exec()
_, _ = c_sites.Exec()
_, _ = c_usersite.Exec()
}
func Finalize(db *sql.DB) {
db.Close()
}

5
utils/user.go

@ -1,11 +1,12 @@
package utils
import (
ar2pw "github.com/andskur/argon2-hashing"
"log"
ar2pw "github.com/andskur/argon2-hashing"
)
func Hashpw(input string) string {
func HashPw(input string) string {
hash, err := ar2pw.GenerateFromPassword([]byte(input), ar2pw.DefaultParams)
if err != nil {
log.Fatal(err)

Loading…
Cancel
Save