Compare commits

..

No commits in common. "6cb4833dbd81696e8564b4f34a4a46183d798f6a" and "48e07c15d43c5c3b59e004476b864bdc3b7cea93" have entirely different histories.

8 changed files with 120 additions and 213 deletions

View File

@ -1,36 +0,0 @@
# Run from scratch
## Database
Create an empty database. The configured user needs to be able to create and alter stuff.
## Configuration
Create a configuration file `$HOME/.config/notes.yaml` with the following content:
```yaml
websocket:
domains:
- notes.com
database:
host: 127.0.0.1
port: 5432
name: notes.com
username: notes
password: SomePassword
application:
directories:
static: /usr/local/share/notes
upload: /usr/local/lib/notes/upload
session:
daysvalid: 31
```
## User
Create a user by running
```
notes --createuser
```

1
TODO
View File

@ -4,6 +4,7 @@
- per file - per file
- when deleting node and child nodes - when deleting node and child nodes
* Move node * Move node
* Tree titles should be user-select none
Long term Long term

4
db.go
View File

@ -6,6 +6,7 @@ import (
_ "github.com/lib/pq" _ "github.com/lib/pq"
// Standard // Standard
"embed"
"errors" "errors"
"fmt" "fmt"
"log" "log"
@ -15,6 +16,9 @@ import (
var ( var (
dbConn string dbConn string
db *sqlx.DB db *sqlx.DB
//go:embed sql/*
embedded embed.FS
) )
func dbInit() (err error) {// {{{ func dbInit() (err error) {// {{{

57
main.go
View File

@ -3,8 +3,8 @@ package main
import ( import (
// Standard // Standard
"crypto/md5" "crypto/md5"
"embed"
"encoding/hex" "encoding/hex"
"path/filepath"
"flag" "flag"
"fmt" "fmt"
"html/template" "html/template"
@ -13,40 +13,34 @@ import (
"net/http" "net/http"
"os" "os"
"path" "path"
"path/filepath"
"regexp" "regexp"
"strconv"
"strings" "strings"
"strconv"
"time" "time"
_ "embed"
) )
const LISTEN_HOST = "0.0.0.0" const VERSION = "v13";
const LISTEN_HOST = "0.0.0.0";
const DB_SCHEMA = 13 const DB_SCHEMA = 13
var ( var (
flagPort int flagPort int
flagVersion bool flagVersion bool
flagCreateUser bool
flagConfig string
connectionManager ConnectionManager connectionManager ConnectionManager
static http.Handler static http.Handler
config Config config Config
VERSION string
//go:embed version sql/*
embedded embed.FS
) )
func init() {// {{{ func init() {// {{{
version, _ := embedded.ReadFile("version") flag.IntVar(
VERSION = strings.TrimSpace(string(version)) &flagPort,
"port",
configFilename := os.Getenv("HOME") + "/.config/notes.yaml" 1371,
flag.IntVar(&flagPort, "port", 1371, "TCP port to listen on") "TCP port to listen on",
)
flag.BoolVar(&flagVersion, "version", false, "Shows Notes version and exists") flag.BoolVar(&flagVersion, "version", false, "Shows Notes version and exists")
flag.BoolVar(&flagCreateUser, "createuser", false, "Create a user and exit")
flag.StringVar(&flagConfig, "config", configFilename, "Filename of configuration file")
flag.Parse() flag.Parse()
if false { if false {
@ -63,7 +57,7 @@ func main() { // {{{
log.Printf("\x1b[32mNotes\x1b[0m %s\n", VERSION) log.Printf("\x1b[32mNotes\x1b[0m %s\n", VERSION)
config, err = ConfigRead(flagConfig) config, err = ConfigRead(os.Getenv("HOME")+"/.config/notes.yaml")
if err != nil { if err != nil {
fmt.Printf("%s\n", err) fmt.Printf("%s\n", err)
os.Exit(1) os.Exit(1)
@ -74,15 +68,6 @@ func main() { // {{{
os.Exit(1) os.Exit(1)
} }
if flagCreateUser {
err = createUser()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
os.Exit(0)
}
connectionManager = NewConnectionManager() connectionManager = NewConnectionManager()
go connectionManager.BroadcastLoop() go connectionManager.BroadcastLoop()
@ -115,11 +100,7 @@ func main() { // {{{
func cssUpdateHandler(w http.ResponseWriter, r *http.Request) {// {{{ func cssUpdateHandler(w http.ResponseWriter, r *http.Request) {// {{{
log.Println("[BROADCAST] CSS updated") log.Println("[BROADCAST] CSS updated")
connectionManager.Broadcast(struct { connectionManager.Broadcast(struct{ Ok bool; ID string; Op string }{ Ok: true, Op: "css_reload" })
Ok bool
ID string
Op string
}{Ok: true, Op: "css_reload"})
}// }}} }// }}}
func websocketHandler(w http.ResponseWriter, r *http.Request) {// {{{ func websocketHandler(w http.ResponseWriter, r *http.Request) {// {{{
var err error var err error
@ -211,10 +192,7 @@ func sessionAuthenticate(w http.ResponseWriter, r *http.Request) { // {{{
return return
} }
req := struct { req := struct{ Username string; Password string }{}
Username string
Password string
}{}
if err = parseRequest(r, &req); err != nil { if err = parseRequest(r, &req); err != nil {
responseError(w, err) responseError(w, err)
return return
@ -466,6 +444,7 @@ func nodeUpload(w http.ResponseWriter, r *http.Request) { // {{{
md5sumBytes := md5.Sum(fileBytes) md5sumBytes := md5.Sum(fileBytes)
md5sum := hex.EncodeToString(md5sumBytes[:]) md5sum := hex.EncodeToString(md5sumBytes[:])
var nodeID int var nodeID int
if nodeID, err = strconv.Atoi(r.PostFormValue("NodeID")); err != nil { if nodeID, err = strconv.Atoi(r.PostFormValue("NodeID")); err != nil {
responseError(w, err) responseError(w, err)
@ -725,9 +704,7 @@ func newTemplate(requestPath string) (tmpl *template.Template, err error) { // {
p = config.Application.Directories.Static + p p = config.Application.Directories.Static + p
base := path.Base(p) base := path.Base(p)
if tmpl, err = template.New(base).ParseFiles(p); err != nil { if tmpl, err = template.New(base).ParseFiles(p); err != nil { return }
return
}
return return
}// }}} }// }}}

View File

@ -205,7 +205,6 @@ header .menu {
#tree .node .name { #tree .node .name {
white-space: nowrap; white-space: nowrap;
cursor: pointer; cursor: pointer;
user-select: none;
} }
#tree .node .name:hover { #tree .node .name:hover {
color: #ecbf00; color: #ecbf00;

View File

@ -232,7 +232,6 @@ header {
.name { .name {
white-space: nowrap; white-space: nowrap;
cursor: pointer; cursor: pointer;
user-select: none;
&:hover { &:hover {
color: @accent_2; color: @accent_2;

36
user.go
View File

@ -3,7 +3,6 @@ package main
import ( import (
// Standard // Standard
"database/sql" "database/sql"
"fmt"
) )
func (session Session) UpdatePassword(currPass, newPass string) (ok bool, err error) { func (session Session) UpdatePassword(currPass, newPass string) (ok bool, err error) {
@ -36,38 +35,3 @@ func (session Session) UpdatePassword(currPass, newPass string) (ok bool, err er
return rowsAffected > 0, nil return rowsAffected > 0, nil
} }
func createUser() (err error) {
var username, password string
fmt.Printf("Username: ")
fmt.Scanln(&username)
fmt.Printf("Password: ")
fmt.Scanln(&password)
err = CreateDbUser(username, password)
return
}
func CreateDbUser(username string, password string) (err error) {
var result sql.Result
result, err = db.Exec(`
INSERT INTO public.user(username, password)
VALUES(
$1,
password_hash(
/* salt in hex */
ENCODE(gen_random_bytes(16), 'hex'),
/* password */
$2::bytea
)
)
`,
username,
password,
)
_, err = result.RowsAffected()
return
}

View File

@ -1 +0,0 @@
v14