Skip to content

Commit f110099

Browse files
committed
tweak(cmd): only enable clickhouse with ENV
1 parent 042d321 commit f110099

File tree

5 files changed

+36
-16
lines changed

5 files changed

+36
-16
lines changed

cmd/lite/lite.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,12 @@ var rootCmd = &cobra.Command{
8080
Database: viper.GetString("clickhouse-database"),
8181
}
8282

83-
clickhouse.AutoMigrate(cmd.Context(), chConfig)
84-
clickhouseClient := clickhouse.NewClient(chConfig)
83+
clickhouseEnabled := shouldInitClickhouse(chConfig.Host)
84+
if clickhouseEnabled {
85+
clickhouse.AutoMigrate(cmd.Context(), chConfig)
86+
}
87+
88+
clickhouseClient := clickhouse.NewClient(chConfig, clickhouseEnabled)
8589

8690
s3Provider := viper.GetString("s3-provider")
8791
storageLayer := storage.New(s3Provider)
@@ -215,15 +219,19 @@ func init() {
215219
)
216220
}
217221

218-
func main() {
219-
if err := rootCmd.Execute(); err != nil {
220-
panic(err)
221-
}
222-
}
223-
224222
func bindError(err error) {
225223
if err != nil {
226224
slog.Error("failed to bind env", slog.Any("error", err))
227225
os.Exit(1)
228226
}
229227
}
228+
229+
func shouldInitClickhouse(clickhouseHost string) bool {
230+
return clickhouseHost != ""
231+
}
232+
233+
func main() {
234+
if err := rootCmd.Execute(); err != nil {
235+
panic(err)
236+
}
237+
}

internal/clickhouse/client.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"log/slog"
78
"time"
89

910
"github.com/ClickHouse/clickhouse-go/v2"
@@ -18,7 +19,8 @@ type Config struct {
1819
}
1920

2021
type Client struct {
21-
conn driver.Conn
22+
conn driver.Conn
23+
Enabled bool
2224
}
2325

2426
type Log struct {
@@ -36,8 +38,17 @@ type LogField struct {
3638
Type string
3739
}
3840

39-
func NewClient(config *Config) *Client {
40-
c := &Client{}
41+
func NewClient(config *Config, enabled bool) *Client {
42+
if !enabled {
43+
slog.Warn("clickhosue is disabled, logging is not available")
44+
return &Client{
45+
Enabled: false,
46+
}
47+
}
48+
49+
c := &Client{
50+
Enabled: true,
51+
}
4152

4253
options := getClickhouseOptions(config)
4354
conn, err := connect(options)

internal/http/httputil/mime.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ func GetMimeDetails(fileHeader *multipart.FileHeader, file multipart.File) (stri
1212
var err error
1313
var fileType string
1414

15-
// Get the file type from the header
16-
buf := make([]byte, fileHeader.Size)
15+
buf := make([]byte, 3072)
1716
_, err = file.Read(buf)
1817
if err != nil {
1918
return "", "", "", fmt.Errorf("error reading file: %w", err)

internal/http/middleware/mime.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ func ValidateMime(fileKey string, whitelistedTypes []string) echo.MiddlewareFunc
4040
return func(next echo.HandlerFunc) echo.HandlerFunc {
4141
return func(ctx echo.Context) error {
4242
var err error
43-
file, header, err := httputil.File(ctx.Request(), fileKey)
43+
file, _, err := httputil.File(ctx.Request(), fileKey)
4444
if err != nil {
45-
fmt.Printf("Failed to fine formdata file. Error: %v\n", err)
45+
fmt.Printf("Failed to find FormData file. Error: %v\n", err)
4646
return ctx.JSON(http.StatusBadRequest, echo.Map{
4747
"error": err.Error(),
4848
})
4949
}
5050

51-
buf := make([]byte, header.Size)
51+
buf := make([]byte, 3072)
5252
_, err = file.Read(buf)
5353
if err != nil {
5454
fmt.Printf("Failed to read buffer. Error: %v\n", err)

internal/service/file/file_service.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func NewService(db *bun.DB, storageLayer storage.StorageLayer) *Service {
2929
}
3030
}
3131

32+
// this is used in the public api only
3233
func (s *Service) CreateFile(
3334
ctx context.Context,
3435
organizationID string,
@@ -103,6 +104,7 @@ func (s *Service) CreateFile(
103104
return nil
104105
}
105106

107+
// uhhh, this is used in the dashboard, not the public api
106108
func (s *Service) CreateStorageFile(
107109
ctx context.Context,
108110
organizationID string,

0 commit comments

Comments
 (0)