Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
4f1cdc2
test code
bengarrett Jul 25, 2025
c8edde2
test code
bengarrett Jul 26, 2025
f50e0ef
test code
bengarrett Jul 26, 2025
0165600
test code
bengarrett Jul 27, 2025
a01faf7
print strings
bengarrett Jul 28, 2025
00231b3
pre mass removal of methods
bengarrett Jul 28, 2025
ffa795b
currently broken with a panic
bengarrett Jul 28, 2025
8ce79a3
currently broken with a panic
bengarrett Jul 28, 2025
84827c4
more zap to slog replacements, plus removed unused consts from intern…
bengarrett Jul 29, 2025
a406b26
better error handling for nil args
bengarrett Jul 29, 2025
bf02a80
using slog in config/fix[archive] packages
bengarrett Jul 29, 2025
95c6247
using slog with the handler but these are untested
bengarrett Jul 30, 2025
d9024f3
new internal panics package to unify the checks of nil pointers as ar…
bengarrett Jul 30, 2025
e8b9745
incomplete slog replacement for html3
bengarrett Jul 30, 2025
bd20c92
incomplete slog replacement routing
bengarrett Jul 30, 2025
5448271
server with slog startsup
bengarrett Jul 30, 2025
0b967f0
breakout of config.go into sep files
bengarrett Jul 31, 2025
6345fd2
removed checks.go
bengarrett Jul 31, 2025
9016bba
printing of startup config is functional
bengarrett Jul 31, 2025
9649b4b
startup now using slog
bengarrett Jul 31, 2025
79303fc
removed zaplog pkgs
bengarrett Jul 31, 2025
c15f611
rename internal/out to internal/logs
bengarrett Jul 31, 2025
8e3fa92
unit tests are functional again
bengarrett Jul 31, 2025
6150535
panics tests for handler/app/
bengarrett Jul 31, 2025
04efcbf
panics tests for handlers
bengarrett Jul 31, 2025
54e4067
more arguments checks
bengarrett Aug 1, 2025
cf443b0
model args checks
bengarrett Aug 1, 2025
7d2e710
test removals and fixes
bengarrett Aug 1, 2025
b7d01e0
removed uses of fprint for console output
bengarrett Aug 1, 2025
1cd043e
removed the use of os.stdout and stderr
bengarrett Aug 1, 2025
4759e31
removed hidden debugging statements and replaced with slog
bengarrett Aug 1, 2025
dbb27db
removed unused errs
bengarrett Aug 1, 2025
5bfe16a
removed unused errs for internal
bengarrett Aug 1, 2025
431aa6b
removed unused errs for model
bengarrett Aug 1, 2025
9697382
removed unused tools
bengarrett Aug 1, 2025
017f150
removed unused unused js tools
bengarrett Aug 1, 2025
0aa860e
fix gitignore issue for logs pkg
bengarrett Aug 1, 2025
3d4dcd9
logging mostly there, still needs some tweaking and save to file
bengarrett Aug 2, 2025
9682e87
fixed the output of c and a command flags
bengarrett Aug 3, 2025
6e39a17
tweaking info texts
bengarrett Aug 3, 2025
77a2b02
tweaking repair texts
bengarrett Aug 4, 2025
839a4e6
middleware is fixed
bengarrett Aug 4, 2025
ec4220b
placeholder slog-multi that needs to be fleshed out
bengarrett Aug 4, 2025
329f8b9
slog implementation and our logs package is mostly complete.
bengarrett Aug 5, 2025
9ebcfe6
using root to openfiles.
bengarrett Aug 5, 2025
d313612
fix to use abslog config
bengarrett Aug 5, 2025
07b0ab4
go mod update and comment fixes
bengarrett Aug 6, 2025
459fea1
fixed some potential nil panics
bengarrett Aug 6, 2025
0e7460c
lint corrections.
bengarrett Aug 6, 2025
535d2e6
more lint fixes but still plenty to go
bengarrett Aug 6, 2025
6e02cd1
finished golangci linter fixes
bengarrett Aug 6, 2025
e7dfb6d
fix for 500 template crash.
bengarrett Aug 6, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ server

# debug and log files
profile*.*
defacto2_serve_*.log

# temp directories
dist/
Expand All @@ -25,4 +26,4 @@ tmp/

# macOS
*.DS_Store
logs/
/logs/
65 changes: 0 additions & 65 deletions .spelling

This file was deleted.

12 changes: 0 additions & 12 deletions Taskfile.dist.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ tasks:
desc: "Initialize this project for the first time after a git clone."
cmds:
- cmd: go install github.com/goreleaser/goreleaser/v2@latest
#- cmd: go install github.com/evanw/esbuild/cmd/esbuild@latest
- cmd: go install github.com/aarondl/sqlboiler/v4@latest
- cmd: go install github.com/aarondl/sqlboiler/v4/drivers/sqlboiler-psql@latest
- cmd: go mod tidy
Expand Down Expand Up @@ -67,19 +66,10 @@ tasks:
cmds:
- cmd: clear
platforms: [linux, darwin, freebsd]
#- cmd: gci write -s standard -s default .
- cmd: go tool gofumpt -l -w .
- cmd: pnpm exec eslint assets/js/*.js
- cmd: pnpm exec stylelint assets/css/*.css
- cmd: golangci-lint run -c init/.golangci.yaml
lint+:
silent: false
desc: Runs the betteralign linter on the source code.
ignore_error: true
cmds:
- cmd: clear
platforms: [linux, darwin, freebsd]
- cmd: go tool betteralign ./...
nil:
desc: "Run the static analysis techniques to catch Nil dereferences."
cmds:
Expand Down Expand Up @@ -185,5 +175,3 @@ tasks:
- cmd: golangci-lint --version
- cmd: goreleaser --version
- cmd: go tool air -v
#- cmd: esbuild --version
- cmd: go tool betteralign -V=full
104 changes: 55 additions & 49 deletions flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import (
"errors"
"fmt"
"io"
"log"
"log/slog"
"os"
"runtime"
"strconv"
"strings"
"time"

"github.com/Defacto2/server/internal/config"
"github.com/Defacto2/server/internal/logs"
"github.com/carlmjohnson/versioninfo"
"github.com/urfave/cli/v2"
"golang.org/x/text/cases"
Expand All @@ -27,9 +30,13 @@ const (
Author = "Ben Garrett" // Author is the primary programmer of this program.
Email = "contact@defacto2.net" // Email contact for public display.
RecentYear = 2025 // Most recent year of compilation for this program.

wsmsg = "Web Server Configurations"
fxmsg = "Database and Asset Fixes"
admsg = "Web Server Addresses"
)

var ErrCmd = errors.New("cannot run command as config is nil")
var ErrNoConfig = errors.New("cannot run command as config is nil")

// App returns the command line interface for this program.
// It uses the [github.com/urfave.cli/v2] package.
Expand Down Expand Up @@ -63,75 +70,62 @@ func App(w io.Writer, ver string, c *config.Config) *cli.App {
}

// Fix command the database and assets.
func Fix(w io.Writer, c *config.Config) *cli.Command {
if w == nil {
w = io.Discard
}
func Fix(_ io.Writer, c *config.Config) *cli.Command {
const msg = "fix command"
return &cli.Command{
Name: "fix",
Aliases: []string{"f"},
Usage: "fix the database and assets",
Description: "Fix the database entries and file assets by running scans and checks.",
Action: func(_ *cli.Context) error {
cl := stdoutput()
sl := logs.Default()
slog.SetDefault(cl)
d := time.Now()
if err := c.Fixer(w, d); err != nil {
return fmt.Errorf("command fix: %w", err)
log.Printf("%s\n", wsmsg)
c.Print(cl)
newline()
slog.SetDefault(sl)
log.Println(fxmsg)
if err := c.Fixer(sl, d); err != nil {
return fmt.Errorf("%s: %w", msg, err)
}
return nil
},
}
}

// Address command lists the server addresses.
func Address(w io.Writer, c *config.Config) *cli.Command {
if w == nil {
w = io.Discard
}
func Address(_ io.Writer, c *config.Config) *cli.Command {
const msg = "address command"
return &cli.Command{
Name: "address",
Aliases: []string{"a"},
Usage: "list the server addresses",
Description: "List the IP, hostname and port addresses the server is most probably listening on.",
Action: func(_ *cli.Context) error {
s, err := c.Addresses()
sl := stdoutput()
log.Printf("%s\n", admsg)
err := c.Addresses(sl)
if err != nil {
return fmt.Errorf("command address: %w", err)
return fmt.Errorf("%s: %w", msg, err)
}
defer func() {
_, err := fmt.Fprintf(w, "%s\n", s)
if err != nil {
panic(err)
}
}()
return nil
},
}
}

// Config command lists the server configuration.
func Config(w io.Writer, c *config.Config) *cli.Command {
if w == nil {
w = io.Discard
}
func Config(_ io.Writer, c *config.Config) *cli.Command {
return &cli.Command{
Name: "config",
Aliases: []string{"c"},
Usage: "list the server configuration",
Description: "List the available server configuration options and the settings.",
Action: func(_ *cli.Context) error {
defer func() {
_, err := fmt.Fprintf(w, "%s\n", c.String())
if err != nil {
panic(err)
}
}()
defer func() {
b := new(strings.Builder)
_, err := fmt.Fprintf(w, "%s\n", b.String())
if err != nil {
panic(err)
}
}()
sl := stdoutput()
log.Printf("%s\n", wsmsg)
c.Print(sl)
return nil
},
}
Expand All @@ -157,6 +151,7 @@ func Arch() string {
// Commit returns a formatted, git commit description for the repository,
// including git tag version and git commit date.
func Commit(ver string) string {
const msg = "n/a (not a build)"
x := []string{}
s := versioninfo.Short()
if ver != "" {
Expand All @@ -165,7 +160,7 @@ func Commit(ver string) string {
x = append(x, s)
}
if len(x) == 0 || x[0] == "devel" {
return "n/a (not a build)"
return msg
}
return strings.Join(x, ", ")
}
Expand Down Expand Up @@ -227,32 +222,32 @@ func Vers(version string) string {
// Version returns a formatted version string for this program
// including the [Commit], [OS] and CPU [Arch].
func Version(s string) string {
x := []string{Commit(s)}
x = append(x, fmt.Sprintf("%s on %s", OS(), Arch()))
return strings.Join(x, " for ")
elems := []string{Commit(s)}
elems = append(elems, fmt.Sprintf("%s on %s", OS(), Arch()))
return strings.Join(elems, " for ")
}

type ExitCode int // ExitCode is the exit code for this program.

const (
Continue ExitCode = iota - 1 // Continue is a special case to indicate the program should not exit.
ExitOK // ExitOK is the exit code for a successful run.
GenericError // GenericError is the exit code for a generic error.
UsageError // UsageError is the exit code for an incorrect command line argument or usage.
Continue ExitCode = iota - 1 // Continue is a special case to indicate the program should not exit.
ExitOK // ExitOK is the exit code for a successful run.
GenericErr // GenericError represents a generic error.
UsageErr // UsageError is used for incorrect arguments or usage.
)

// Run parses optional command line arguments for this program.
func Run(w io.Writer, ver string, c *config.Config) (ExitCode, error) {
if c == nil {
return UsageError, ErrCmd
return UsageErr, ErrNoConfig
}
const minArgs = 2
if len(os.Args) < minArgs {
return Continue, nil
}
args := os.Args[1:]
useArguments := len(args) > 0
if useArguments {
useArgs := len(args) > 0
if useArgs {
return setup(w, ver, c)
}
return Continue, nil
Expand All @@ -272,17 +267,28 @@ defaults for poor usability. Without the downloads and image directories, the se
will not display any thumbnails or previews or serve the file downloads.`, c.HTTPPort)
}

func newline() {
_, _ = fmt.Fprintln(os.Stdout)
}

func stdoutput() *slog.Logger {
lf := logs.NoFiles()
sl := lf.New(logs.LevelInfo, logs.Flags)
slog.SetDefault(sl)
return sl
}

func setup(w io.Writer, ver string, c *config.Config) (ExitCode, error) {
if c == nil {
return UsageError, ErrCmd
return UsageErr, ErrNoConfig
}
app := App(w, ver, c)
app.EnableBashCompletion = true
app.HideHelpCommand = true
app.HideVersion = false
app.Suggest = true
if err := app.Run(os.Args); err != nil {
return GenericError, fmt.Errorf("application setup and run: %w", err)
return GenericErr, fmt.Errorf("application setup and run: %w", err)
}
return ExitOK, nil
}
4 changes: 2 additions & 2 deletions flags/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ func TestRun(t *testing.T) {
t.Parallel()
ec, err := flags.Run(nil, "", nil)
be.Err(t, err)
be.Equal(t, flags.UsageError, ec)
be.Equal(t, flags.UsageErr, ec)
c := config.Config{}
ec, err = flags.Run(nil, "", &c)
be.Err(t, err)
be.Equal(t, flags.GenericError, ec)
be.Equal(t, flags.GenericErr, ec)
}

func TestVers(t *testing.T) {
Expand Down
Loading