build: add makefile, dockerfile, ci and development stack
- build: add makefile, dockerfile, ci and development stack - feat(config): add environment configuration and logger - feat(server): add server lifecycle with health and version routes - feat(db): add connection, contract check, migrations and stores - feat(cli): add command dispatch and user management - test(db): add integration tests against mysql
This commit is contained in:
1 parent
45d1e307c6
commit
7089a6f408
46 files changed
+8456
No files matched your search
@@ -0,0 +1,106 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
// Copyright (C) 2026 Bryan Joshua Pedini
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
const (
|
||||
exitOK = 0
|
||||
exitFailure = 1
|
||||
exitUsage = 2
|
||||
exitProblems = 3
|
||||
)
|
||||
|
||||
var (
|
||||
errUsage = errors.New("usage error")
|
||||
errCheckProblems = errors.New("check found problems")
|
||||
)
|
||||
|
||||
type app struct {
|
||||
lookupEnv func(string) (string, bool)
|
||||
stdin io.Reader
|
||||
stdout io.Writer
|
||||
stderr io.Writer
|
||||
stdinIsTerminal func() bool
|
||||
readTerminalPassword func() ([]byte, error)
|
||||
now func() time.Time
|
||||
hashPassword func(string) (string, error)
|
||||
normalizeZone func(string) (string, error)
|
||||
listen func(network, address string) (net.Listener, error)
|
||||
openDB func(cfg Config, log *slog.Logger) (*sql.DB, error)
|
||||
prepareDB func(ctx context.Context, cfg Config, log *slog.Logger) (*sql.DB, tableNames, error)
|
||||
newUserBackend func(db *sql.DB, t tableNames) cliUserBackend
|
||||
pingBackoff backoff
|
||||
healthClient *http.Client
|
||||
serverStarted func(*WebServer, net.Listener)
|
||||
}
|
||||
|
||||
func newApp(lookupEnv func(string) (string, bool), stdin *os.File, stdout, stderr io.Writer) *app {
|
||||
a := &app{
|
||||
lookupEnv: lookupEnv,
|
||||
stdin: stdin,
|
||||
stdout: stdout,
|
||||
stderr: stderr,
|
||||
stdinIsTerminal: func() bool { return term.IsTerminal(int(stdin.Fd())) },
|
||||
readTerminalPassword: func() ([]byte, error) {
|
||||
return term.ReadPassword(int(stdin.Fd()))
|
||||
},
|
||||
now: time.Now,
|
||||
hashPassword: stubHashPassword,
|
||||
normalizeZone: stubNormalizeZone,
|
||||
listen: net.Listen,
|
||||
openDB: openDatabase,
|
||||
newUserBackend: newSQLUserBackend,
|
||||
pingBackoff: defaultPingBackoff(),
|
||||
healthClient: newHealthClient(),
|
||||
}
|
||||
a.prepareDB = a.connectDatabase
|
||||
return a
|
||||
}
|
||||
|
||||
func main() {
|
||||
os.Exit(run(os.Args[1:], newApp(os.LookupEnv, os.Stdin, os.Stdout, os.Stderr)))
|
||||
}
|
||||
|
||||
func run(args []string, a *app) int {
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer stop()
|
||||
return a.exitCode(a.execute(ctx, args))
|
||||
}
|
||||
|
||||
func (a *app) exitCode(err error) int {
|
||||
if err == nil {
|
||||
return exitOK
|
||||
}
|
||||
if errors.Is(err, errCheckProblems) {
|
||||
return exitProblems
|
||||
}
|
||||
msg := strings.NewReplacer("\r", " ", "\n", " ").Replace(err.Error())
|
||||
fmt.Fprintf(a.stderr, "coredns-web-ui: %s\n", msg)
|
||||
switch {
|
||||
case errors.Is(err, errUsage):
|
||||
fmt.Fprint(a.stderr, usageText)
|
||||
return exitUsage
|
||||
case errors.Is(err, errConfig):
|
||||
return exitUsage
|
||||
default:
|
||||
return exitFailure
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user