Begin adding a basic config

This commit is contained in:
Andrew-71 2024-03-17 17:12:41 +03:00
parent 7ddaacc415
commit 2565a6f397
6 changed files with 89 additions and 8 deletions

View file

@ -2,13 +2,12 @@
Simple plaintext diary.
This project is *very* highly opinionated and minimal.
This project is *very* opinionated and minimal. It is designed primarily for myself.
## Features:
* Each day, you get a txt file. You have until 23:59 of that very day to finalise it.
* At any moment, you can log a single line to the log file
* At any moment, you can log a single line to the log
* You can save named notes to document milestones, big events, or just a nice game you played this month
* You can export everything in a zip file. Emergency export code for other people will be supported
* You can easily export everything in a zip file for backups
* No extra features. No encryption, OAuth, or anything fancy. Even the password is plain te- wait is this a feature?
* Intention to eventually *Go* dependency-less (get it?). Sorry chi, I still like you!
* Everything is in plain text. No databases, encryption, OAuth, or anything fancy. Even the password is plain te- wait is this a feature?

15
TODO.md Normal file
View file

@ -0,0 +1,15 @@
# Crucial
* Load username&password, port from config.txt
* Add export feature
* Add missing frontend pages
# Short term
* Custom CSS, pico is too big for this project
* Organise .go files better
# Long-medium term
* *Go* dependency-less
* Improve logging, log to files
* Make the CLI better

63
config.go Normal file
View file

@ -0,0 +1,63 @@
package main
import (
"bufio"
"errors"
"log"
"os"
"strconv"
"strings"
)
type Config struct {
Username string
Password string
Port int
}
func CreateConfig(config Config) {
}
func LoadConfig() (Config, error) {
filename := "config/config.txt"
if _, err := os.Stat(filename); errors.Is(err, os.ErrNotExist) {
CreateConfig(Config{})
return Config{}, err
}
cfg := Config{Port: 7101}
file, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
entry := strings.Split(strings.Replace(scanner.Text(), " ", "", -1), "=")
if len(entry) != 2 {
continue
}
key := entry[0]
value := entry[1]
if key == "username" {
cfg.Username = value
} else if key == "password" {
cfg.Password = value
} else if key == "port" {
numVal, err := strconv.Atoi(value)
if err == nil {
cfg.Port = numVal
}
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return cfg, nil
}

View file

@ -11,6 +11,7 @@ import (
"strings"
)
// GetFile returns raw contents of a txt file in data directory
func GetFile(filename string, w http.ResponseWriter, r *http.Request) {
path := "data/" + filename + ".txt" // Can we and should we sanitize this?
@ -31,7 +32,8 @@ func GetFile(filename string, w http.ResponseWriter, r *http.Request) {
}
}
// PostFile TODO: Save to trash to prevent malicious/accidental ovverrides?
// PostFile Writes request's contents to a txt file in data directory
// TODO: Save to trash to prevent malicious/accidental overrides?
func PostFile(filename string, w http.ResponseWriter, r *http.Request) {
body, err := io.ReadAll(r.Body)
if err != nil {

View file

@ -1,5 +1,7 @@
package main
var Cfg, _ = LoadConfig()
func main() {
Serve()
}

View file

@ -44,6 +44,6 @@ func Serve() {
fs := http.FileServer(http.Dir("public"))
r.Handle("/public/*", http.StripPrefix("/public/", fs))
fmt.Println("Website working on port: ", PORT)
_ = http.ListenAndServe(":"+strconv.Itoa(PORT), r)
fmt.Println("Website working on port: ", Cfg.Port)
_ = http.ListenAndServe(":"+strconv.Itoa(Cfg.Port), r)
}