Skip to content

Initial Backend #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
22 changes: 22 additions & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## Contributing

1. Install go (on arch: `pacman -S go`)
2. In the root directory of the repo, run `podman compose up -d`
3. Obtain [golang migrate](https://github.com/golang-migrate/migrate). You may need to download the CLI directly from a release
4. In `backend`, run `migrate -source file://$(pwd)/migrations -database postgres://postgres:test@localhost:5432/postgres?sslmode=disable up`, on windows run `migrate -path ./migrations -database postgres://postgres:test@localhost:5432/postgres?sslmode=disable up`
5. In `backend`, run `go run .`

### Version tagging
If compiled with `-buildvcs`, traces and logs will include a commit hash. You can keep this set by running `go env -w GOFLAGS=-buildvcs`

### Adding new dependencies

`go mod tidy`

### Formatting

`gofmt -w -s .`

### Creating new DB migrations

Create a new file with an incremented prefix number, a human readable string, and ending with `.up.sql`, with a corresponding `.down.sql` that has the same number prefix to revert your change. Apply these with golang-migrate
4 changes: 4 additions & 0 deletions backend/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
tracing:
enabled: false
environment: "local"
version: "local-dev"
23 changes: 23 additions & 0 deletions backend/config/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package config

import (
"context"

"github.com/spf13/viper"
)

type configContextKey string

const contextKey configContextKey = "config"

func ContextWithConfig(ctx context.Context, config *viper.Viper) context.Context {
return context.WithValue(ctx, contextKey, config)
}

func FromContext(ctx context.Context) *viper.Viper {
v := ctx.Value(contextKey)
if v == nil {
panic("config not available in context, somewhere it got lost")
}
return v.(*viper.Viper)
}
63 changes: 63 additions & 0 deletions backend/config/default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package config

import (
"context"
"fmt"
"runtime/debug"
"sync"

"github.com/spf13/viper"
)

var once sync.Once

// If compiled with -buildvcs, this will be populated with the commit hash
// This maybe isn't the nicest versioning scheme, but it's _something_
var version = func() string {
if buildInfo, ok := debug.ReadBuildInfo(); ok {
for _, setting := range buildInfo.Settings {
if setting.Key == "vcs.revision" {
return setting.Value
}
}
}
return ""
}()

/*
* GetConfig determines what config file to look at based on the environment, and constructs a config to use
*/
func GetConfig(_ context.Context) *viper.Viper {
config := viper.New()
config.SetEnvPrefix("cluesheet")

config.BindEnv("config_path")
config.BindEnv("environment")
config.SetDefault("environment", "local")
config.SetDefault("version", version)

if config.IsSet("config_path") {
config.SetConfigFile(config.GetString("config_path"))
} else {
config.SetConfigType("yaml")
config.AddConfigPath(".")
config.AddConfigPath("/opt/cluesheet")

switch config.GetString("environment") {
case "local":
config.SetConfigName("config.yaml")
case "develop":
config.SetConfigName("config.dev")
case "prod":
config.SetConfigName("config.prod")
default:
panic(fmt.Sprintf("unknown environment '%s', can't look for config", config.GetString("environment")))
}
}
err := config.ReadInConfig()
if err != nil {
panic(fmt.Sprintf(err.Error()))
}

return config
}
98 changes: 98 additions & 0 deletions backend/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
module csh/cluesheet

go 1.24.2

require (
github.com/DataDog/dd-trace-go/contrib/gorilla/mux/v2 v2.1.0-dev.1
github.com/DataDog/dd-trace-go/contrib/jackc/pgx.v5/v2 v2.1.0-dev.1
github.com/DataDog/dd-trace-go/v2 v2.1.0-dev.1
github.com/google/uuid v1.6.0
github.com/gorilla/mux v1.8.1
github.com/jackc/pgx/v5 v5.6.0
github.com/spf13/viper v1.20.1
github.com/stretchr/testify v1.10.0
go.uber.org/zap v1.27.0
)

require (
github.com/DataDog/appsec-internal-go v1.11.2 // indirect
github.com/DataDog/datadog-agent/comp/core/tagger/origindetection v0.64.0-rc.1 // indirect
github.com/DataDog/datadog-agent/pkg/obfuscate v0.64.0-rc.1 // indirect
github.com/DataDog/datadog-agent/pkg/proto v0.64.0-rc.1 // indirect
github.com/DataDog/datadog-agent/pkg/remoteconfig/state v0.64.0-rc.1 // indirect
github.com/DataDog/datadog-agent/pkg/trace v0.64.0-rc.1 // indirect
github.com/DataDog/datadog-agent/pkg/util/log v0.64.0-rc.1 // indirect
github.com/DataDog/datadog-agent/pkg/util/scrubber v0.64.0-rc.1 // indirect
github.com/DataDog/datadog-agent/pkg/version v0.64.0-rc.1 // indirect
github.com/DataDog/datadog-go/v5 v5.6.0 // indirect
github.com/DataDog/dd-trace-go/contrib/net/http/v2 v2.1.0-dev.1 // indirect
github.com/DataDog/go-libddwaf/v3 v3.5.4 // indirect
github.com/DataDog/go-runtime-metrics-internal v0.0.4-0.20241206090539-a14610dc22b6 // indirect
github.com/DataDog/go-sqllexer v0.1.0 // indirect
github.com/DataDog/go-tuf v1.1.0-0.5.2 // indirect
github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/attributes v0.26.0 // indirect
github.com/DataDog/sketches-go v1.4.7 // indirect
github.com/Masterminds/semver/v3 v3.3.1 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cihub/seelog v0.0.0-20170130134532-f561c5e57575 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/eapache/queue/v2 v2.0.0-20230407133247-75960ed334e4 // indirect
github.com/ebitengine/purego v0.8.2 // indirect
github.com/fsnotify/fsnotify v1.8.0 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/lufia/plan9stats v0.0.0-20240226150601-1dcf7310316a // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/outcaste-io/ristretto v0.2.3 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/philhofer/fwd v1.1.3-0.20240916144458-20a13a1f6b7c // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
github.com/puzpuzpuz/xsync/v3 v3.5.1 // indirect
github.com/sagikazarmark/locafero v0.7.0 // indirect
github.com/secure-systems-lab/go-securesystemslib v0.9.0 // indirect
github.com/shirou/gopsutil/v4 v4.25.1 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.12.0 // indirect
github.com/spf13/cast v1.7.1 // indirect
github.com/spf13/pflag v1.0.6 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/tinylib/msgp v1.2.5 // indirect
github.com/tklauser/go-sysconf v0.3.14 // indirect
github.com/tklauser/numcpus v0.8.0 // indirect
github.com/yusufpapurcu/wmi v1.2.4 // indirect
go.opentelemetry.io/collector/component v0.120.0 // indirect
go.opentelemetry.io/collector/pdata v1.26.0 // indirect
go.opentelemetry.io/collector/pdata/pprofile v0.120.0 // indirect
go.opentelemetry.io/collector/semconv v0.120.0 // indirect
go.opentelemetry.io/otel v1.34.0 // indirect
go.opentelemetry.io/otel/metric v1.34.0 // indirect
go.opentelemetry.io/otel/trace v1.34.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.33.0 // indirect
golang.org/x/exp v0.0.0-20250210185358-939b2ce775ac // indirect
golang.org/x/mod v0.23.0 // indirect
golang.org/x/net v0.35.0 // indirect
golang.org/x/sync v0.11.0 // indirect
golang.org/x/sys v0.30.0 // indirect
golang.org/x/text v0.22.0 // indirect
golang.org/x/time v0.9.0 // indirect
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250219182151-9fdb1cabc7b2 // indirect
google.golang.org/grpc v1.70.0 // indirect
google.golang.org/protobuf v1.36.5 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading