From 72e1e4e2f35ee7f24ec11959f155a619f8f9bb39 Mon Sep 17 00:00:00 2001 From: Tristan Partin Date: Tue, 27 Aug 2024 15:26:31 -0500 Subject: [PATCH] feat(server): create a minimal server Signed-off-by: Tristan Partin --- .github/workflows/build-server.yaml | 41 ++++++++++ .github/workflows/golangci-lint.yaml | 32 ++++++++ .github/workflows/reuse.yaml | 2 +- .github/workflows/vacuum.yaml | 62 ++++++++++++++ .gitignore | 4 + README.md | 11 +++ REUSE.toml | 16 ++-- VERSION | 1 + cmd/root.go | 118 +++++++++++++++++++++++++++ cmd/server.go | 46 +++++++++++ cmd/version.go | 26 ++++++ docs/gosplitsies.example.yaml | 12 +++ docs/server-configuration.md | 43 ++++++++++ go.mod | 32 ++++++++ go.sum | 80 ++++++++++++++++++ justfile | 19 +++++ main.go | 14 ++++ middleware/logger.go | 27 ++++++ openapi.yaml | 94 +++++++++++++++++++++ vacuum.conf.yaml | 10 +++ 20 files changed, 684 insertions(+), 6 deletions(-) create mode 100644 .github/workflows/build-server.yaml create mode 100644 .github/workflows/golangci-lint.yaml create mode 100644 .github/workflows/vacuum.yaml create mode 100644 VERSION create mode 100644 cmd/root.go create mode 100644 cmd/server.go create mode 100644 cmd/version.go create mode 100644 docs/gosplitsies.example.yaml create mode 100644 docs/server-configuration.md create mode 100644 go.mod create mode 100644 go.sum create mode 100644 justfile create mode 100644 main.go create mode 100644 middleware/logger.go create mode 100644 openapi.yaml create mode 100644 vacuum.conf.yaml diff --git a/.github/workflows/build-server.yaml b/.github/workflows/build-server.yaml new file mode 100644 index 0000000..a46b4e6 --- /dev/null +++ b/.github/workflows/build-server.yaml @@ -0,0 +1,41 @@ +# 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 + + steps: + - name: Checkout gosplitsies + uses: actions/checkout@v4 + + - name: Setup Go + uses: actions/setup-go@v5 + with: + check-latest: true + go-version: stable + + - name: Install Just + run: | + curl --proto '=https' --tlsv1.2 -sSf https://just.systems/install.sh | + bash -s -- --to /usr/local/bin + + - 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..ce8d61b --- /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: stable + + - name: Lint + uses: golangci/golangci-lint-action@v6 + with: + version: latest diff --git a/.github/workflows/reuse.yaml b/.github/workflows/reuse.yaml index 394ff3e..9bf5515 100644 --- a/.github/workflows/reuse.yaml +++ b/.github/workflows/reuse.yaml @@ -20,7 +20,7 @@ jobs: runs-on: ubuntu-latest steps: - - name: Checkout splitsies + - name: Checkout gosplitsies uses: actions/checkout@v4 - name: Lint diff --git a/.github/workflows/vacuum.yaml b/.github/workflows/vacuum.yaml new file mode 100644 index 0000000..e30fb5b --- /dev/null +++ b/.github/workflows/vacuum.yaml @@ -0,0 +1,62 @@ +# 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: + vacuum: + runs-on: ubuntu-latest + env: + VACUUM_VERSION: "v0.14.3" + + steps: + - name: Checkout gosplitsies + uses: actions/checkout@v4 + + - 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" + + - name: Cache Go Modules + uses: actions/cache@v4 + with: + path: | + ${{ steps.gocache.outputs.GOCACHE }} + ${{ needs.gomodcache.outputs.GOMODCACHE }} + key: ${{ env.VACUUM_VERSION }} + + - name: Setup Go + uses: actions/setup-go@v5 + with: + cache: false + check-latest: true + go-version: stable + + - 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..cab3292 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,7 @@ go.work.sum .env !.vscode/ + +# Go ignores +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 f4b774d..44e14c7 100644 --- a/REUSE.toml +++ b/REUSE.toml @@ -1,17 +1,23 @@ -# SPDX-License-Identifier: AGPL-3.0-or-later -# -# SPDX-FileCopyrightText: 2024 Tristan Partin - version = 1 SPDX-HomePage = "https://github.com/return0software/gosplitsies" SPDX-PackageName = "gosplitsies" [[annotations]] path = [ - ".vscode/copyright.code-snippets", "ui/package.json", "ui/package-lock.json", ] precedence = "override" SPDX-FileCopyrightText = "2024 Joseph Martinsen " SPDX-License-Identifier = "AGPL-3.0-or-later" + +[[annotations]] +path = [ + ".vscode/copyright.code-snippets", + "go.mod", + "go.sum", + "VERSION", +] +precedence = "override" +SPDX-FileCopyrightText = "2024 Tristan Partin " +SPDX-License-Identifier = "AGPL-3.0-or-later" 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..86b4792 --- /dev/null +++ b/cmd/server.go @@ -0,0 +1,46 @@ +/* SPDX-License-Identifier: AGPL-3.0-or-later + * + * SPDX-FileCopyrightText: 2024 Tristan Partin + */ + +package cmd + +import ( + "fmt" + "net" + "net/http" + "os" + + "github.com/Return0Software/gosplitsies/middleware" + "github.com/spf13/cobra" + "github.com/spf13/viper" + "github.com/tristan957/go-sd-notify" + "go.uber.org/zap" +) + +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) + + listener, err := net.Listen("tcp", fmt.Sprintf(":%d", port)) + if err != nil { + zap.S().Fatalf("failed to begin listening on port %d: %s", port, err) + os.Exit(1) + } + + _ = notify.Ready() + + if err := http.Serve(listener, middleware.NewLogger(mux)); err != nil { + zap.S().Fatalf("failed to start the server: %s", err) + _ = notify.Stopping() + os.Exit(1) + } + }, +} diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 0000000..01466c4 --- /dev/null +++ b/cmd/version.go @@ -0,0 +1,26 @@ +/* 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) { + if debug == "true" { + fmt.Println(version) + } else { + fmt.Printf("%s debug\n", 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..eeda8f0 --- /dev/null +++ b/docs/server-configuration.md @@ -0,0 +1,43 @@ + + +# Server Configuration + +Refer to the [example] for a list of all available settings. + +[example]: ./gosplitsies.example.yaml + +## Logging + +If you want to log to a file, set `log.filepath` to a non-empty string, and set +`log.location` to `file`. + +## Change Configuration via Files + +Configuration files can be passed on the command line via the root +`-c`/`--config` option. + +## Change Configuration via 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`. + +## Change Configuration via 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. + +## Change Configuration via 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..b0f6a17 --- /dev/null +++ b/go.mod @@ -0,0 +1,32 @@ +module github.com/Return0Software/gosplitsies + +go 1.22.6 + +require ( + github.com/spf13/cobra v1.8.1 + github.com/spf13/pflag v1.0.5 + github.com/spf13/viper v1.19.0 + go.uber.org/zap v1.27.0 +) + +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/subosito/gotenv v1.6.0 // indirect + github.com/tristan957/go-sd-notify v0.0.0-20241129070729-7b194d5e70bd // indirect + go.uber.org/multierr v1.11.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..c2eafc3 --- /dev/null +++ b/go.sum @@ -0,0 +1,80 @@ +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/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= +github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= +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/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +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/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +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/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= +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 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +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= +github.com/tristan957/go-sd-notify v0.0.0-20241129070729-7b194d5e70bd h1:y8ThvQm1xmR9s11R+bFq+vkGSnZH4OzATfrOjPxsR0o= +github.com/tristan957/go-sd-notify v0.0.0-20241129070729-7b194d5e70bd/go.mod h1:nGsh9+UW4oyaMf1c1Sdw7au+SSC+WCdvNq3EIBop/ho= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= +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/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/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