Skip to content

Commit

Permalink
refactor: yes
Browse files Browse the repository at this point in the history
  • Loading branch information
Vilsol committed Oct 21, 2023
1 parent 7807b9e commit 66ac612
Show file tree
Hide file tree
Showing 69 changed files with 870 additions and 2,916 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19
go-version: 1.21

- name: Check out code into the Go module directory
uses: actions/checkout@v3
Expand All @@ -36,7 +36,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19
go-version: 1.21

- name: Check out code into the Go module directory
uses: actions/checkout@v3
Expand All @@ -53,7 +53,7 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.49.0
version: v1.54.2
skip-pkg-cache: true
skip-build-cache: true
args: --timeout 5m
Expand All @@ -65,7 +65,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19
go-version: 1.21

- name: Check out code into the Go module directory
uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19
go-version: 1.21

- name: Check out code into the Go module directory
uses: actions/checkout@v3
Expand Down
2 changes: 0 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ linters-settings:
- .Errorf(
- errors.New(
- errors.Unwrap(
- .Wrap(
- .Wrapf(
- .WithMessage(
- .WithMessagef(
- .WithStack(
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.19-alpine3.18 AS builder
FROM golang:1.21-alpine3.18 AS builder

RUN apk add --no-cache git build-base libpng-dev protoc
RUN go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SMR API ![GitHub Workflow Status](https://img.shields.io/github/workflow/status/satisfactorymodding/smr-api/build) ![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/satisfactorymodding/smr-api) [![codecov](https://codecov.io/gh/satisfactorymodding/smr-api/branch/master/graph/badge.svg?token=LFNKYWS0N2)](https://codecov.io/gh/satisfactorymodding/smr-api) ![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/satisfactorymodding/smr-api)
# SMR API [![build](https://github.com/satisfactorymodding/smr-api/actions/workflows/build.yml/badge.svg)](https://github.com/satisfactorymodding/smr-api/actions/workflows/build.yml) ![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/satisfactorymodding/smr-api) [![codecov](https://codecov.io/gh/satisfactorymodding/smr-api/branch/master/graph/badge.svg?token=LFNKYWS0N2)](https://codecov.io/gh/satisfactorymodding/smr-api) ![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/satisfactorymodding/smr-api)

The Satisfactory Mod Repository backend API

Expand Down
65 changes: 36 additions & 29 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package smr
import (
"context"
"fmt"
"log/slog"
"os"
"os/signal"
"runtime"
Expand All @@ -14,12 +15,11 @@ import (
"github.com/99designs/gqlgen/graphql/handler/lru"
"github.com/99designs/gqlgen/graphql/handler/transport"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/Vilsol/slox"
"github.com/felixge/fgprof"
"github.com/labstack/echo-contrib/pprof"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
echoSwagger "github.com/swaggo/echo-swagger"
"go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho"
Expand Down Expand Up @@ -59,7 +59,10 @@ type CustomValidator struct {
}

func (cv *CustomValidator) Validate(i interface{}) error {
return errors.Wrap(cv.validator.Struct(i), "validation error")
if err := cv.validator.Struct(i); err != nil {
return fmt.Errorf("validation error: %w", err)
}
return nil
}

func Initialize(baseCtx context.Context) context.Context {
Expand All @@ -79,7 +82,7 @@ func Initialize(baseCtx context.Context) context.Context {
auth.InitializeAuth()
jobs.InitializeJobs(ctx)
validation.InitializeVirusTotal()
util.PrintFeatureFlags()
util.PrintFeatureFlags(ctx)

return ctx
}
Expand All @@ -99,7 +102,7 @@ func Setup(ctx context.Context) {
debugServer.HideBanner = true
debugServer.HidePort = true
address := ":6060"
log.Info().Str("address", address).Msg("starting profiler")
slog.Info("starting profiler", slog.String("address", address))
debugServer.Logger.Fatal(debugServer.Start(address))
}()
}
Expand All @@ -120,8 +123,7 @@ func Setup(ctx context.Context) {

v1.Use(func(handlerFunc echo.HandlerFunc) echo.HandlerFunc {
return func(ctx echo.Context) error {
newLogger := log.Ctx(ctx.Request().Context()).With().Str("facade", "REST").Logger()
newCtx := newLogger.WithContext(context.Background())
newCtx := slox.With(ctx.Request().Context(), slog.String("facade", "REST"))
ctx.SetRequest(ctx.Request().WithContext(newCtx))
return handlerFunc(ctx)
}
Expand All @@ -139,8 +141,7 @@ func Setup(ctx context.Context) {

v2.Use(func(handlerFunc echo.HandlerFunc) echo.HandlerFunc {
return func(ctx echo.Context) error {
newLogger := log.Ctx(ctx.Request().Context()).With().Str("facade", "GQL").Logger()
newCtx := newLogger.WithContext(context.Background())
newCtx := slox.With(ctx.Request().Context(), slog.String("facade", "GQL"))
newCtx = context.WithValue(newCtx, util.ContextHeader{}, ctx.Request().Header)
newCtx = context.WithValue(newCtx, util.ContextRequest{}, ctx.Request())
newCtx = context.WithValue(newCtx, util.ContextResponse{}, ctx.Response().Writer)
Expand Down Expand Up @@ -250,22 +251,23 @@ func Setup(ctx context.Context) {
bytesIn = "0"
}

log.Info().
Str("time_rfc3339", time.Now().Format(time.RFC3339)).
Str("remote_ip", c.RealIP()).
Str("host", req.Host).
Str("uri", req.RequestURI).
Str("method", req.Method).
Str("path", p).
Str("referer", req.Referer()).
Str("user_agent", req.UserAgent()).
Int("status", res.Status).
Int64("latency", stop.Sub(start).Nanoseconds()/1000).
Str("latency_human", stop.Sub(start).String()).
Str("bytes_in", bytesIn).
Int64("bytes_out", res.Size).
Str("trace_id", spanContext.TraceID().String()).
Msg("Handled request")
slog.Info(
"Handled request",
slog.String("time_rfc3339", time.Now().Format(time.RFC3339)),
slog.String("remote_ip", c.RealIP()),
slog.String("host", req.Host),
slog.String("uri", req.RequestURI),
slog.String("method", req.Method),
slog.String("path", p),
slog.String("referer", req.Referer()),
slog.String("user_agent", req.UserAgent()),
slog.Int("status", res.Status),
slog.Int64("latency", stop.Sub(start).Nanoseconds()/1000),
slog.String("latency_human", stop.Sub(start).String()),
slog.String("bytes_in", bytesIn),
slog.Int64("bytes_out", res.Size),
slog.String("trace_id", spanContext.TraceID().String()),
)

return nil
}
Expand All @@ -282,7 +284,7 @@ func Setup(ctx context.Context) {

func Serve() {
address := fmt.Sprintf(":%d", viper.GetInt("port"))
log.Info().Str("address", address).Msg("starting server")
slog.Info("starting server", slog.String("address", address))

e.HidePort = true
e.Logger.Error(e.Start(address))
Expand All @@ -292,7 +294,8 @@ func installExportPipeline(ctx context.Context) func() {
client := otlptracehttp.NewClient()
exporter, err := otlptrace.New(ctx, client)
if err != nil {
log.Fatal().Err(err).Msg("creating OTLP trace exporter")
slog.Error("creating OTLP trace exporter", slog.Any("err", err))
os.Exit(1)
}

tracerProvider := sdktrace.NewTracerProvider(
Expand All @@ -304,7 +307,8 @@ func installExportPipeline(ctx context.Context) func() {

return func() {
if err := tracerProvider.Shutdown(ctx); err != nil {
log.Fatal().Err(err).Msg("stopping tracer provider")
slog.Error("stopping tracer provider", slog.Any("err", err))
os.Exit(1)
}
}
}
Expand All @@ -329,5 +333,8 @@ func Start() {
}

func Stop() error {
return errors.Wrap(e.Close(), "failed to stop http server")
if err := e.Close(); err != nil {
return fmt.Errorf("failed to stop http server: %w", err)
}
return nil
}
53 changes: 38 additions & 15 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@ package config

import (
"context"
"io"
"fmt"
"log/slog"
"os"
"time"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/Vilsol/slox"
"github.com/lmittmann/tint"
"github.com/spf13/viper"
)

var configDir = "."

const (
ansiReset = "\033[0m"
ansiBold = "\033[1m"
ansiWhite = "\033[38m"
ansiBrightMagenta = "\033[95m"
)

func SetConfigDir(newConfigDir string) {
configDir = newConfigDir
}
Expand All @@ -27,29 +35,44 @@ func InitializeConfig(baseCtx context.Context) context.Context {

err := viper.ReadInConfig() //nolint:ifshort

var out io.Writer = os.Stdout
if !viper.GetBool("production") {
out = zerolog.ConsoleWriter{
Out: os.Stdout,
TimeFormat: time.RFC3339,
}
slog.SetDefault(slog.New(
StackRewriter{
Upstream: tint.NewHandler(os.Stderr, &tint.Options{
Level: slog.LevelDebug,
AddSource: true,
TimeFormat: time.RFC3339Nano,
ReplaceAttr: func(groups []string, attr slog.Attr) slog.Attr {
if attr.Key == slog.LevelKey {
level := attr.Value.Any().(slog.Level)
if level == slog.LevelDebug {
attr.Value = slog.StringValue(ansiBrightMagenta + "DBG" + ansiReset)
}
} else if attr.Key == slog.MessageKey {
attr.Value = slog.StringValue(ansiBold + ansiWhite + fmt.Sprint(attr.Value.Any()) + ansiReset)
}
return attr
},
}).WithAttrs([]slog.Attr{slog.String("service", "api")}),
},
))
} else {
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
AddSource: true,
})))
}

log.Logger = zerolog.New(out).With().Str("service", "api").Timestamp().Logger()

if baseCtx == nil {
baseCtx = context.Background()
}

ctx := log.Logger.WithContext(baseCtx)

if err != nil {
log.Warn().Err(err).Msg("config initialized using defaults and environment only!")
slog.Warn("config initialized using defaults and environment only!", slog.Any("err", err))
}

log.Info().Msg("Config initialized")
slox.Info(baseCtx, "Config initialized")

return ctx
return baseCtx
}

func initializeDefaults() {
Expand Down
42 changes: 42 additions & 0 deletions config/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package config

import (
"context"
"log/slog"
"runtime"
"strings"
)

var _ slog.Handler = (*StackRewriter)(nil)

type StackRewriter struct {
Upstream slog.Handler
}

func (t StackRewriter) Enabled(ctx context.Context, level slog.Level) bool {
return t.Upstream.Enabled(ctx, level)
}

func (t StackRewriter) Handle(ctx context.Context, record slog.Record) error {
if record.PC != 0 {
fs := runtime.CallersFrames([]uintptr{record.PC})
f, _ := fs.Next()
if strings.HasPrefix(f.Function, "github.com/Vilsol/slox") {
// skip one more
var pcs [1]uintptr
runtime.Callers(5, pcs[:])
record.PC = pcs[0]
}
}

//nolint
return t.Upstream.Handle(ctx, record)
}

func (t StackRewriter) WithAttrs(attrs []slog.Attr) slog.Handler {
return t.Upstream.WithAttrs(attrs)
}

func (t StackRewriter) WithGroup(name string) slog.Handler {
return t.Upstream.WithGroup(name)
}
2 changes: 1 addition & 1 deletion db/postgres/otel/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func NewPlugin(opts ...Option) *Plugin {
cfg: cfg,
tracer: cfg.tracerProvider.Tracer(
defaultTracerName,
oteltrace.WithInstrumentationVersion(contrib.SemVersion()),
oteltrace.WithInstrumentationVersion(contrib.Version()),
),
}
}
Expand Down
Loading

0 comments on commit 66ac612

Please sign in to comment.