diff --git a/.github/workflows/build-server.yaml b/.github/workflows/build-server.yaml new file mode 100644 index 0000000..8f3bc70 --- /dev/null +++ b/.github/workflows/build-server.yaml @@ -0,0 +1,42 @@ +# SPDX-License-Identifier: AGPL-3.0-or-later +# +# SPDX-FileCopyrightText: 2024 Tristan Partin + +name: Build Server + +on: + pull_request: + types: [opened, synchronize, reopened] + paths: + - "**.go" + +permissions: + contents: read + +concurrency: + group: ${{ github.workflow }}-${{ github.head_ref }} + cancel-in-progress: true + +jobs: + build-server: + runs-on: ubuntu-latest + container: + image: docker.io/fedora:40 + + steps: + - name: Checkout splitsies + uses: actions/checkout@v4 + + - name: Setup Go + uses: actions/setup-go@v5 + with: + check-latest: true + go-version: "1.22" + + - name: Install just + run: | + dnf install --assumeyes --setopt=install_weak_deps=False just + + - name: Build the Server + run: | + just debug diff --git a/.github/workflows/golangci-lint.yaml b/.github/workflows/golangci-lint.yaml new file mode 100644 index 0000000..e1e73da --- /dev/null +++ b/.github/workflows/golangci-lint.yaml @@ -0,0 +1,32 @@ +# SPDX-License-Identifier: AGPL-3.0-or-later +# +# SPDX-FileCopyrightText: 2024 Tristan Partin + +name: golangci-lint + +on: + pull_request: + types: [opened, synchronize, reopened] + paths: + - "**.go" + +permissions: + contents: read + +jobs: + golangci-lint: + runs-on: ubuntu-latest + + steps: + - name: Checkout gosplitsies + uses: actions/checkout@v4 + + - name: Setup Go + uses: actions/setup-go@v5 + with: + go-version: "1.22" + + - name: golangci-lint + uses: golangci/golangci-lint-action@v6 + with: + version: "v1.60" diff --git a/.github/workflows/vacuum.yaml b/.github/workflows/vacuum.yaml new file mode 100644 index 0000000..034c8ac --- /dev/null +++ b/.github/workflows/vacuum.yaml @@ -0,0 +1,79 @@ +# SPDX-License-Identifier: AGPL-3.0-or-later +# +# SPDX-FileCopyrightText: 2024 Tristan Partin + +name: vacuum + +on: + pull_request: + types: [opened, synchronize, reopened] + paths: + - "openapi.yaml" + - "vacuum.conf.yaml" + +permissions: + contents: read + +concurrency: + group: ${{ github.workflow }}-${{ github.head_ref }} + cancel-in-progress: true + +jobs: + go-cache: + runs-on: ubuntu-latest + outputs: + GOCACHE: ${{ steps.gocache.outputs.GOCACHE }} + GOMODCACHE: ${{ steps.gomodcache.outputs.GOMODCACHE }} + + steps: + - name: Setup Go + uses: actions/setup-go@v5 + with: + cache: false + check-latest: true + go-version: "1.22" + + - name: Get the GOCACHE Environment Variable + id: gocache + run: | + echo "GOCACHE=$(go env GOCACHE)" >> "$GITHUB_OUTPUT" + + - name: Get the GOMODCACHE Environment Variable + id: gomodcache + run: | + echo "GOMODCACHE=$(go env GOMODCACHE)" >> "$GITHUB_OUTPUT" + + + vacuum: + runs-on: ubuntu-latest + needs: + - go-cache + env: + VACUUM_VERSION: "v0.12.1" + + steps: + - name: Checkout splitsies + uses: actions/checkout@v4 + + - name: Cache Go Modules + uses: actions/cache@v4 + with: + path: | + ${{ needs.go-cache.outputs.GOCACHE }} + ${{ needs.go-cache.outputs.GOMODCACHE }} + key: ${{ env.VACUUM_VERSION }} + + - name: Setup Go + uses: actions/setup-go@v5 + with: + cache: false + check-latest: true + go-version: "1.22" + + - name: Install vacuum + run: | + go install "github.com/daveshanley/vacuum@$VACUUM_VERSION" + + - name: Lint the OpenAPI Description + run: | + vacuum lint --no-style openapi.yaml diff --git a/.gitignore b/.gitignore index 1085ef4..e0867c6 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,6 @@ go.work.sum .env !.vscode/ + +gsplit +gsplit.debug diff --git a/README.md b/README.md index f17187f..55327eb 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,17 @@ SPDX-FileCopyrightText: 2024 Tristan Partin # gosplitsies +## server + +### Building + +```shell +# Release +just build +# Debug +just debug +``` + ## ui ### Setup diff --git a/REUSE.toml b/REUSE.toml index f9dc160..8e477ca 100644 --- a/REUSE.toml +++ b/REUSE.toml @@ -9,8 +9,11 @@ SPDX-PackageName = "splitsies" [[annotations]] path = [ ".vscode/copyright.code-snippets", + "go.mod", + "go.sum", "ui/package.json", "ui/package-lock.json", + "VERSION", ] precedence = "override" SPDX-FileCopyrightText = "2024 Joseph Martinsen " diff --git a/VERSION b/VERSION new file mode 100644 index 0000000..77d6f4c --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +0.0.0 diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 0000000..971c3e5 --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,118 @@ +/* SPDX-License-Identifier: AGPL-3.0-or-later + * + * SPDX-FileCopyrightText: 2024 Tristan Partin + */ + +package cmd + +import ( + "fmt" + "os" + "strings" + + "github.com/spf13/cobra" + "github.com/spf13/pflag" + "github.com/spf13/viper" + "go.uber.org/zap" + "go.uber.org/zap/zapcore" +) + +var debug string +var configFile string +var zapConfig zap.Config + +var rootCmd = &cobra.Command{ + Use: "gsplit", + Short: "GoSplitsies is a pay splitting application", +} + +func init() { + cobra.OnInitialize(initConfig) + + rootCmd.AddCommand(versionCmd) + rootCmd.AddCommand(serverCmd) + + rootCmd.PersistentFlags().StringVarP(&configFile, "config", "c", "", "Path to a config file") + rootCmd.PersistentFlags().StringP("log-filepath", "l", "", "Path to log to if log-location is file") + rootCmd.PersistentFlags().String("log-location", "stderr", "Location to send logs to") + + serverCmd.Flags().Uint16P("port", "p", 5431, "Port to start the server on") + + flags := map[string]*pflag.Flag{ + "log.filepath": rootCmd.PersistentFlags().Lookup("log-filepath"), + "log.location": rootCmd.PersistentFlags().Lookup("log-location"), + + "server.port": serverCmd.Flags().Lookup("port"), + } + for key, flag := range flags { + if err := viper.BindPFlag(key, flag); err != nil { + fmt.Fprintf(os.Stderr, "failed to bind flags for configuration purposes: %s\n", err) + os.Exit(1) + } + } +} + +func initConfig() { + if configFile != "" { + viper.SetConfigFile(configFile) + } else { + viper.SetConfigName("gosplitsies") + viper.SetConfigType("yaml") + + // TODO: Windows and macOS? + viper.AddConfigPath("/etc") + } + + viper.SetEnvPrefix("gsplit") + viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_", ".", "_")) + viper.AutomaticEnv() + + if err := viper.ReadInConfig(); err != nil { + if _, ok := err.(viper.ConfigFileNotFoundError); !ok && configFile != "" { + fmt.Fprintf(os.Stderr, "failed to read in config: %s\n", err) + os.Exit(1) + } + } + + if debug == "true" { + zapConfig = zap.NewDevelopmentConfig() + zapConfig.Level.SetLevel(zapcore.DebugLevel) + } else { + zapConfig = zap.NewProductionConfig() + zapConfig.Level.SetLevel(zapcore.InfoLevel) + } + + logLocation := viper.GetString("log.location") + if logLocation == "file" { + logFilepath := viper.GetString("log.filepath") + if logFilepath == "" { + fmt.Fprintln(os.Stderr, "no log filepath provided") + os.Exit(1) + } + + zapConfig.OutputPaths = []string{logFilepath} + } else if logLocation == "stdout" { + zapConfig.OutputPaths = []string{"stdout"} + } else if logLocation == "stderr" { + // Nothing to do since this is the default + } else if logLocation == "syslog" { //nolint:staticcheck + // TODO: Fail this if macOS or Windows + // TODO: Also, actually implement this + } + + logger, err := zapConfig.Build() + if err != nil { + fmt.Fprintln(os.Stderr, "failed to initialize logger") + } + //nolint:errcheck + defer logger.Sync() + + zap.ReplaceGlobals(logger) +} + +func Execute() { + if err := rootCmd.Execute(); err != nil { + zap.S().Error("%s", err) + os.Exit(1) + } +} diff --git a/cmd/server.go b/cmd/server.go new file mode 100644 index 0000000..b407939 --- /dev/null +++ b/cmd/server.go @@ -0,0 +1,36 @@ +/* SPDX-License-Identifier: AGPL-3.0-or-later + * + * SPDX-FileCopyrightText: 2024 Tristan Partin + */ + +package cmd + +import ( + "fmt" + "net/http" + "os" + + "github.com/Return0Software/gosplitsies/middleware" + "github.com/spf13/cobra" + "github.com/spf13/viper" + "go.uber.org/zap" +) + +var serverPort uint16 + +var serverCmd = &cobra.Command{ + Use: "server", + Short: "Start the server", + Run: func(cmd *cobra.Command, args []string) { + mux := http.NewServeMux() + + mux.HandleFunc("/logs/level", zapConfig.Level.ServeHTTP) + + port := viper.GetUint16("server.port") + zap.S().Infof("Starting server on port %d", port) + if err := http.ListenAndServe(fmt.Sprintf(":%d", port), middleware.NewLogger(mux)); err != nil { + zap.S().Fatalf("failed to start the server on port %d: %s", serverPort, err) + os.Exit(1) + } + }, +} diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 0000000..a17f798 --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: AGPL-3.0-or-later + * + * SPDX-FileCopyrightText: 2024 Tristan Partin + */ + +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +var version string + +var versionCmd = &cobra.Command{ + Use: "version", + Short: "Print the version", + Run: func(cmd *cobra.Command, args []string) { + fmt.Println(version) + }, +} diff --git a/docs/gosplitsies.example.yaml b/docs/gosplitsies.example.yaml new file mode 100644 index 0000000..671071c --- /dev/null +++ b/docs/gosplitsies.example.yaml @@ -0,0 +1,12 @@ +# SPDX-License-Identifier: AGPL-3.0-or-later +# +# SPDX-FileCopyrightText: 2024 Tristan Partin + +log: + # Path to log to if log-location is file + filename: "" + # Location to send logs to (file, stderr, stdout, syslog) + location: "stderr" +server: + # Port to start the server on + port: 5431 diff --git a/docs/server-configuration.md b/docs/server-configuration.md new file mode 100644 index 0000000..c1ec5c6 --- /dev/null +++ b/docs/server-configuration.md @@ -0,0 +1,38 @@ + + +# Server Configuration + +Refer to the [example] for a list of all available settings. + +[example]: ./gosplitsies.example.yaml + +## Files + +Configuration files can be passed on the command line via the root +`-c`/`--config` option. + +## Environment Variables + +All environment variables are prefixed with `GSPLIT_`. For example, if you want +to set the setting, `server.port`, via an environment variable, you can set the +`GSPLIT_SERVER_PORT` environment variable. `xxx-yyy` would translate to +`GSPLIT_XXX_YYY`. + +## Command Line Options + +Please refer to the `-h`/`--help` documentation of the relevant commands. +Command line options take precedence over environment variables and config +files. + +## HTTP Requests + +### Logging + +The logging level defaults to `DEBUG` in debug mode and `INFO` in production +mode. It can be changed at [runtime]. + +[runtime]: https://pkg.go.dev/go.uber.org/zap#AtomicLevel.ServeHTTP diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b380744 --- /dev/null +++ b/go.mod @@ -0,0 +1,28 @@ +module github.com/Return0Software/gosplitsies + +go 1.22.6 + +require ( + github.com/fsnotify/fsnotify v1.7.0 // indirect + github.com/hashicorp/hcl v1.0.0 // indirect + github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/magiconair/properties v1.8.7 // indirect + github.com/mitchellh/mapstructure v1.5.0 // indirect + github.com/pelletier/go-toml/v2 v2.2.2 // indirect + github.com/sagikazarmark/locafero v0.4.0 // indirect + github.com/sagikazarmark/slog-shim v0.1.0 // indirect + github.com/sourcegraph/conc v0.3.0 // indirect + github.com/spf13/afero v1.11.0 // indirect + github.com/spf13/cast v1.6.0 // indirect + github.com/spf13/cobra v1.8.1 // indirect + github.com/spf13/pflag v1.0.5 // indirect + github.com/spf13/viper v1.19.0 // indirect + github.com/subosito/gotenv v1.6.0 // indirect + go.uber.org/multierr v1.11.0 // indirect + go.uber.org/zap v1.27.0 // indirect + golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect + golang.org/x/sys v0.18.0 // indirect + golang.org/x/text v0.14.0 // indirect + gopkg.in/ini.v1 v1.67.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..7889293 --- /dev/null +++ b/go.sum @@ -0,0 +1,59 @@ +github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= +github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= +github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= +github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= +github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= +github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= +github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= +github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ= +github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4= +github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE= +github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= +github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= +github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= +github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8= +github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY= +github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0= +github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= +github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= +github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/spf13/viper v1.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI= +github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+Ntkg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= +github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= +go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= +go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8= +go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= +golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g= +golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k= +golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= +golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= +gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/justfile b/justfile new file mode 100644 index 0000000..95936ba --- /dev/null +++ b/justfile @@ -0,0 +1,19 @@ +# SPDX-License-Identifier: AGPL-3.0-or-later +# +# SPDX-FileCopyrightText: 2024 Tristan Partin + +alias b := build +alias d := debug + +version := `git describe --always --long --dirty || cat VERSION` + +build: + go build -o gsplit -ldflags \ + "-X github.com/Return0Software/gosplitsies/cmd.debug=false \ + -X github.com/Return0Software/gosplitsies/cmd.version={{version}}" + +debug: + go build -o gsplit.debug -ldflags \ + "-X github.com/Return0Software/gosplitsies/cmd.debug=true \ + -X github.com/Return0Software/gosplitsies/cmd.version={{version}}" + @echo 'Attach to the server with `dlv attach $(pidof gsplit.debug)`' diff --git a/main.go b/main.go new file mode 100644 index 0000000..1c66282 --- /dev/null +++ b/main.go @@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: AGPL-3.0-or-later + * + * SPDX-FileCopyrightText: 2024 Tristan Partin + */ + +package main + +import ( + "github.com/Return0Software/gosplitsies/cmd" +) + +func main() { + cmd.Execute() +} diff --git a/middleware/logger.go b/middleware/logger.go new file mode 100644 index 0000000..43b546c --- /dev/null +++ b/middleware/logger.go @@ -0,0 +1,27 @@ +/* SPDX-License-Identifier: AGPL-3.0-or-later + * + * SPDX-FileCopyrightText: 2024 Tristan Partin + */ + +package middleware + +import ( + "net/http" + "time" + + "go.uber.org/zap" +) + +type Logger struct { + handler http.Handler +} + +func NewLogger(handlerToWrap http.Handler) *Logger { + return &Logger{handlerToWrap} +} + +func (l *Logger) ServeHTTP(w http.ResponseWriter, r *http.Request) { + start := time.Now() + l.handler.ServeHTTP(w, r) + zap.S().Infof("%s %s %v", r.Method, r.URL.Path, time.Since(start)) +} diff --git a/openapi.yaml b/openapi.yaml new file mode 100644 index 0000000..5113ce0 --- /dev/null +++ b/openapi.yaml @@ -0,0 +1,94 @@ +# SPDX-License-Identifier: AGPL-3.0-or-later +# +# SPDX-FileCopyrightText: 2024 Tristan Partin + +# https://swagger.io/specification/ +openapi: "3.1.0" +info: + title: "GoSplitsies" + description: "GoSplitsies is a service for splitting payments" + version: "0" + contact: + name: "Return0Software" + url: "https://github.com/Return0Software/gosplitsies" + license: + name: "AGPLv3" + identifier: "AGPL-3.0-or-later" + url: "https://www.gnu.org/licenses/agpl-3.0.en.html" +tags: + - name: admin + description: "Administrative Routes" +paths: + "/logs/level": + get: + operationId: "get-log-level" + summary: "Get the current log level" + description: "Get the current log level. This endpoint is provided by zap." + tags: + - admin + responses: + "200": + description: "Return the current log level" + content: + "application/json": + examples: + debug: + "$ref": "#/components/examples/LogLevel" + schema: + "$ref": "#/components/schemas/LogLevel" + externalDocs: + url: "https://pkg.go.dev/go.uber.org/zap#AtomicLevel.ServeHTTP" + put: + operationId: "change-log-level" + summary: "Set the current log level" + description: "Set the current logging level. This endpoint is provided by zap." + tags: + - admin + parameters: + - name: "level" + in: query + description: "The log level to set." + examples: + debug: + "$ref": "#/components/examples/LogLevel" + schema: + "$ref": "#/components/schemas/LogLevel" + requestBody: + description: "Description of the log level to set." + content: + "application/json": + examples: + debug: + externalValue: "#/components/examples/LogLevel" + schema: + "$ref": "#/components/schemas/LogLevel" + "application/x-www-form-urlencoded": + examples: + debug: + "$ref": "#/components/examples/LogLevel" + schema: + "$ref": "#/components/schemas/LogLevel" + externalDocs: + url: "https://pkg.go.dev/go.uber.org/zap#AtomicLevel.ServeHTTP" +components: + schemas: + LogLevel: + summary: "Log level" + description: "Description of the log level." + type: object + properties: + level: + enum: + - "debug" + - "info" + - "warning" + - "error" + - "fatal" + required: + - "level" + example: + level: "debug" + examples: + LogLevel: + description: "How to set the log level to debug." + level: "debug" diff --git a/vacuum.conf.yaml b/vacuum.conf.yaml new file mode 100644 index 0000000..4320710 --- /dev/null +++ b/vacuum.conf.yaml @@ -0,0 +1,10 @@ +# SPDX-License-Identifier: AGPL-3.0-or-later +# +# SPDX-FileCopyrightText: 2024 Tristan Partin + +lint: + details: true + fail-severity: "warn" + no-banner: true + no-clip: true + snippets: true