config file reading logic implemented, project structure refined

This commit is contained in:
Maximilian Wagner
2025-12-24 00:26:32 +01:00
parent d30757808d
commit 2d50286efa
13 changed files with 181 additions and 3 deletions

15
.gitignore vendored Normal file
View File

@@ -0,0 +1,15 @@
# https://mrkandreev.name/snippets/gitignore-generator/#Go
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, built with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out

81
backend/backend.go Normal file
View File

@@ -0,0 +1,81 @@
package backend
import (
"os"
"path/filepath"
"encoding/json"
"errors"
"fmt"
"git.noctra.dev/noctra/servtex/globals"
)
// Helper for configReader() that does the actual reading
func configReaderParse(filePath string, configOptionStorage *globals.Config) error {
jsonData, err := os.ReadFile(filePath)
if err == nil {
err = json.Unmarshal(jsonData, &configOptionStorage)
if err != nil {
// log error unmarshal failure
return errors.New("Config file could not be read")
}
} else {
return errors.New("Config file does not exist")
}
return nil
}
// Reads config file and stores the options in configOptionStorage
// Tries executable/path/configFileName
// %LOCALAPPDATA%\servtex\configFileName
// ~/.config/servtex/configFileName
func ConfigReader(configFileName string, configOptionStorage *globals.Config) error {
exePath, err := os.Executable()
if err == nil {
path := filepath.Join(filepath.Dir(exePath), configFileName)
err = configReaderParse(path, configOptionStorage)
if err == nil { return nil }
}
localappdata := os.Getenv("LOCALAPPDATA")
if localappdata != "" {
path := filepath.Join(localappdata, "servtex", configFileName)
err = configReaderParse(path, configOptionStorage)
if err == nil { return nil }
}
path := filepath.Join("~", ".config", "servtex", configFileName)
err = configReaderParse(path, configOptionStorage)
if err == nil { return nil }
// log error no config file found
return errors.New("Configuration file not found or json parsing error")
}
// Watches a specified directory and calls a function on change
// Optionally, a minimum time to wait for consecutive changes can be specified
func ChangeWatch(path string, callOnChange func()) {
}
// Intended to be run as goroutine
func LatexCompile(config *globals.Config) error {
if !config.ExecutionLock.TryLock() {
return errors.New("Execution already in progress")
}
var latexCommand string
switch config.LatexEngine {
case "lualatex":
latexCommand = fmt.Sprintf(
"lualatex -output-directory=%s -jobname=servtex %s",
config.LatexOutputPath,
config.LatexSourceFilePath,
)
}
fmt.Sprintf(latexCommand) // placeholder for compilation
config.ExecutionLock.Unlock()
return nil
}

13
config.json Normal file
View File

@@ -0,0 +1,13 @@
{
"logLevel": "debug",
"logFilePath": "./servtex.log",
"latexEngine": "lualatex",
"latexSourceFilePath": "",
"latexOutputPath": "./output",
"webserverDomain": "localhost:9876",
"webserverSecure": false,
"certificatePath": "",
"certificateKeyPath": ""
}

6
frontend/jscss/bootstrap.min.css vendored Normal file

File diff suppressed because one or more lines are too long

1
frontend/jscss/htmx.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,13 @@
{{ define "head" }}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Todo</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="/jscss/bootstrap.min.css">
<script src="/jscss/htmx.min.js"></script>
</head>
{{ end }}

2
frontend/webserver.go Normal file
View File

@@ -0,0 +1,2 @@
package frontend

25
globals/config.go Normal file
View File

@@ -0,0 +1,25 @@
package globals
import "sync"
type Config struct {
// general
LogLevel string `json:"logLevel"`
LogFilePath string `json:"logFilePath"`
// latex
LatexEngine string `json:"latexEngine"`
LatexSourceFilePath string `json:"latexSourceFilePath"`
LatexOutputPath string `json:"latexOutputPath"`
// webserver
WebserverDomain string `json:"webserverDomain"`
WebserverSecure bool `json:"webserverSecure"`
CertificatePath string `json:"certificatePath"`
CertificateKeyPath string `json:"certificateKeyPath"`
// to prevent competing compiles, not actual configuration
ExecutionLock sync.Mutex
}
var AppConfig Config

2
go.mod
View File

@@ -1,3 +1,3 @@
module noctra.dev/servtex
module git.noctra.dev/noctra/servtex
go 1.25.5

21
main.go Normal file
View File

@@ -0,0 +1,21 @@
package main
import (
"os"
//"log/slog"
"git.noctra.dev/noctra/servtex/globals"
"git.noctra.dev/noctra/servtex/backend"
//"net/http"
)
// Exit Codes:
// 0 - ok
// 1 - config file could not be read
func main() {
err := backend.ConfigReader("config.json", &globals.AppConfig)
if err != nil {
os.Exit(1)
}
}

3
makefile Normal file
View File

@@ -0,0 +1,3 @@
default:
go build
./servtex

BIN
servtex Executable file

Binary file not shown.

View File

@@ -1,2 +0,0 @@
package servtex