Skip to content

Commit

Permalink
Add prometheus
Browse files Browse the repository at this point in the history
  • Loading branch information
uhthomas committed Feb 18, 2021
1 parent 2d42a89 commit 810f235
Show file tree
Hide file tree
Showing 10 changed files with 1,693 additions and 94 deletions.
5 changes: 5 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,19 @@ go_library(
name = "go_default_library",
srcs = [
"fs.go",
"option.go",
"server.go",
],
importpath = "github.com/uhthomas/kipp",
visibility = ["//visibility:public"],
deps = [
"//database:go_default_library",
"//filesystem:go_default_library",
"//internal/databaseutil:go_default_library",
"//internal/filesystemutil:go_default_library",
"@com_github_gabriel_vasile_mimetype//:go_default_library",
"@com_github_prometheus_client_golang//prometheus:go_default_library",
"@com_github_prometheus_client_golang//prometheus/promhttp:go_default_library",
"@com_github_zeebo_blake3//:go_default_library",
],
)
Expand Down
5 changes: 1 addition & 4 deletions cmd/kipp/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@ go_library(
visibility = ["//visibility:private"],
deps = [
"//:go_default_library",
"//internal/databaseutil:go_default_library",
"//internal/filesystemutil:go_default_library",
"//internal/x/context:go_default_library",
"//internal/httputil:go_default_library",
"@com_github_alecthomas_units//:go_default_library",
"@com_github_jackc_pgx_v4//stdlib:go_default_library",
"@org_golang_x_sync//errgroup:go_default_library",
],
)

Expand Down
68 changes: 12 additions & 56 deletions cmd/kipp/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,21 @@ package main

import (
"context"
"errors"
"flag"
"fmt"
"log"
"mime"
"net"
"net/http"
"time"

_ "github.com/jackc/pgx/v4/stdlib"
"github.com/uhthomas/kipp"
"github.com/uhthomas/kipp/internal/databaseutil"
"github.com/uhthomas/kipp/internal/filesystemutil"
xcontext "github.com/uhthomas/kipp/internal/x/context"
"golang.org/x/sync/errgroup"
"github.com/uhthomas/kipp/internal/httputil"
)

func serve(ctx context.Context) error {
addr := flag.String("addr", ":80", "listen addr")
dbf := flag.String("database", "badger", "database - see docs for more information")
fsf := flag.String("filesystem", "files", "filesystem - see docs for more information")
db := flag.String("database", "badger", "database - see docs for more information")
fs := flag.String("filesystem", "files", "filesystem - see docs for more information")
web := flag.String("web", "web", "web directory")
limit := flagBytesValue("limit", 150<<20, "upload limit")
lifetime := flag.Duration("lifetime", 24*time.Hour, "file lifetime")
Expand All @@ -39,55 +33,17 @@ func serve(ctx context.Context) error {
}
}

fs, err := filesystemutil.Parse(*fsf)
s, err := kipp.New(ctx,
kipp.ParseDB(*db),
kipp.ParseFS(*fs),
kipp.Lifetime(*lifetime),
kipp.Limit(int64(*limit)),
kipp.Data(*web),
)
if err != nil {
return fmt.Errorf("parse filesystem: %w", err)
return err
}

db, err := databaseutil.Parse(ctx, *dbf)
if err != nil {
return fmt.Errorf("parse database: %w", err)
}
defer db.Close(ctx)

log.Printf("listening on %s", *addr)

g, ctx := errgroup.WithContext(ctx)

s := &http.Server{
Addr: *addr,
Handler: &kipp.Server{
Database: db,
FileSystem: fs,
Limit: int64(*limit),
Lifetime: *lifetime,
PublicPath: *web,
},
// ReadTimeout: 5 * time.Second,
// WriteTimeout: 10 * time.Second,
BaseContext: func(net.Listener) context.Context { return xcontext.Detach(ctx) },
}

g.Go(func() error {
<-ctx.Done()
ctx := ctx
if *gracePeriod != 0 {
ctx = xcontext.Detach(ctx)
if *gracePeriod > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, *gracePeriod)
defer cancel()
}
}
return s.Shutdown(ctx)
})

g.Go(func() error {
if err := s.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
return fmt.Errorf("listen and serve: %w", err)
}
return nil
})

return g.Wait()
return httputil.ListenAndServe(ctx, *addr, s, *gracePeriod)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/dgraph-io/badger/v2 v2.2007.2
github.com/gabriel-vasile/mimetype v1.1.2
github.com/jackc/pgx/v4 v4.10.1
github.com/prometheus/client_golang v1.9.0
github.com/zeebo/blake3 v0.1.0
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a
)
507 changes: 497 additions & 10 deletions go.sum

Large diffs are not rendered by default.

Loading

0 comments on commit 810f235

Please sign in to comment.