Version handling from file

This commit is contained in:
Magnus Åhall 2023-12-28 07:47:23 +01:00
parent ac58fa0fc8
commit 392aff5956
4 changed files with 148 additions and 115 deletions

24
README.md Normal file
View File

@ -0,0 +1,24 @@
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
```
Create an empty database.

4
db.go
View File

@ -6,7 +6,6 @@ import (
_ "github.com/lib/pq" _ "github.com/lib/pq"
// Standard // Standard
"embed"
"errors" "errors"
"fmt" "fmt"
"log" "log"
@ -16,9 +15,6 @@ 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) {// {{{

46
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,34 +13,38 @@ import (
"net/http" "net/http"
"os" "os"
"path" "path"
"path/filepath"
"regexp" "regexp"
"strings"
"strconv" "strconv"
"strings"
"time" "time"
_ "embed"
) )
const VERSION = "v13"; const LISTEN_HOST = "0.0.0.0"
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
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() { // {{{
flag.IntVar( version, _ := embedded.ReadFile("version")
&flagPort, VERSION = strings.TrimSpace(string(version))
"port",
1371,
"TCP port to listen on",
)
flag.BoolVar(&flagVersion, "version", false, "Shows Notes version and exists")
configFilename := os.Getenv("HOME") + "/.config/notes.yaml"
flag.IntVar(&flagPort, "port", 1371, "TCP port to listen on")
flag.BoolVar(&flagVersion, "version", false, "Shows Notes version and exists")
flag.StringVar(&flagConfig, "config", configFilename, "Filename of configuration file")
flag.Parse() flag.Parse()
if false { if false {
@ -57,7 +61,7 @@ func main() {// {{{
log.Printf("\x1b[32mNotes\x1b[0m %s\n", VERSION) log.Printf("\x1b[32mNotes\x1b[0m %s\n", VERSION)
config, err = ConfigRead(os.Getenv("HOME")+"/.config/notes.yaml") config, err = ConfigRead(flagConfig)
if err != nil { if err != nil {
fmt.Printf("%s\n", err) fmt.Printf("%s\n", err)
os.Exit(1) os.Exit(1)
@ -100,7 +104,11 @@ 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{ Ok bool; ID string; Op string }{ Ok: true, Op: "css_reload" }) connectionManager.Broadcast(struct {
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
@ -192,7 +200,10 @@ func sessionAuthenticate(w http.ResponseWriter, r *http.Request) {// {{{
return return
} }
req := struct{ Username string; Password string }{} req := struct {
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
@ -444,7 +455,6 @@ 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)
@ -704,7 +714,9 @@ 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 { return } if tmpl, err = template.New(base).ParseFiles(p); err != nil {
return
}
return return
} // }}} } // }}}

1
version Normal file
View File

@ -0,0 +1 @@
v14