Skip to content

Commit

Permalink
#8 Fixing lint errors in the existing codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
roma-glushko committed Dec 19, 2023
1 parent 38c56a2 commit 35b37d7
Show file tree
Hide file tree
Showing 15 changed files with 81 additions and 68 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ jobs:
go-version: "1.21"
check-latest: true

- name: Install
run: go install mvdan.cc/gofumpt@latest

- name: Go Format
run: gofmt -s -w . && git diff --exit-code

- name: Gofmtf
run: gofumpt -l -w . && git diff --exit-code

- name: Go Vet
run: go vet ./...

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ dist
.env
bin
glide
tmp
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
CHECKER_BIN=$(PWD)/tmp/bin
VERSION_PACKAGE := glide/pkg
COMMIT ?= $(shell git describe --dirty --long --always --abbrev=15)
VERSION ?= "latest" # TODO: pull/pass a real version
Expand All @@ -10,9 +11,15 @@ help:
@echo "🛠️ Glide Dev Commands:\n"
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'


install: ## Install static checkers
@echo "🚚 Downloading binaries.."
@GOBIN=$(CHECKER_BIN) go install mvdan.cc/gofumpt@latest

lint: ## Lint the source code
@echo "🧹 Formatting files.."
@go fmt ./...
@$(CHECKER_BIN)/gofumpt -l -w .
@echo "🧹 Vetting go.mod.."
@go vet ./...
@echo "🧹 Cleaning go.mod.."
Expand Down
11 changes: 6 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ require (
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/cloudwego/netpoll v0.5.0 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/golang/protobuf v1.5.0 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/henrylee2cn/ameda v1.4.10 // indirect
github.com/henrylee2cn/goutil v0.0.0-20210127050712-89660552f6f8 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
Expand All @@ -33,9 +34,9 @@ require (
github.com/tidwall/pretty v1.2.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect
golang.org/x/crypto v0.7.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
)
24 changes: 12 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.5.4 h1:jRbGcIw6P2Meqdwuo0H1p6JVLbL5DHKAKlYndzMwVZI=
github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU=
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
Expand All @@ -33,8 +33,9 @@ github.com/go-playground/validator/v10 v10.16.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QX
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.5.0 h1:LUVKkCeviFUMKqHa4tXIIij/lbhnMbP7Fn5wKdKkRh4=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/henrylee2cn/ameda v1.4.8/go.mod h1:liZulR8DgHxdK+MEwvZIylGnmcjzQ6N6f2PlWe7nEO4=
Expand Down Expand Up @@ -91,22 +92,21 @@ golang.org/x/arch v0.0.0-20201008161808-52c3e6f60cff/go.mod h1:flIaEI6LNU6xOCD5P
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 h1:18EFjUmQOcUvxNYSkA6jO9VAiXCnxFY6NyDX0bHDmkU=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY=
golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20220110181412-a018aaa089fe/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package main

import (
"glide/pkg/cmd"
"log"

"glide/pkg/cmd"
)

func main() {
Expand Down
7 changes: 4 additions & 3 deletions pkg/api/http/config.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package http

import (
"time"

"github.com/cloudwego/hertz/pkg/app/server"
"github.com/cloudwego/hertz/pkg/network/netpoll"
"time"
)

type HTTPServerConfig struct {
type ServerConfig struct {
// TODO: add fields
}

func (cfg *HTTPServerConfig) ToServer() *server.Hertz {
func (cfg *ServerConfig) ToServer() *server.Hertz {
// TODO: do real server build based on provided config
return server.Default(
server.WithIdleTimeout(1*time.Second),
Expand Down
13 changes: 7 additions & 6 deletions pkg/api/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,33 @@ package http
import (
"context"
"fmt"
"time"

"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/server"
"github.com/cloudwego/hertz/pkg/common/utils"
"github.com/cloudwego/hertz/pkg/protocol/consts"
"time"
)

type HTTPServer struct {
type Server struct {
server *server.Hertz
}

func NewHttpServer(config *HTTPServerConfig) (*HTTPServer, error) {
return &HTTPServer{
func NewServer(config *ServerConfig) (*Server, error) {
return &Server{
server: config.ToServer(),
}, nil
}

func (srv *HTTPServer) Run() error {
func (srv *Server) Run() error {
srv.server.GET("/health", func(ctx context.Context, c *app.RequestContext) {
c.JSON(consts.StatusOK, utils.H{"healthy": true})
})

return srv.server.Run()
}

func (srv *HTTPServer) Shutdown(_ context.Context) error {
func (srv *Server) Shutdown(_ context.Context) error {
exitWaitTime := srv.server.GetOptions().ExitWaitTimeout

println(fmt.Sprintf("Begin graceful shutdown, wait at most %d seconds...", exitWaitTime/time.Second))
Expand Down
10 changes: 5 additions & 5 deletions pkg/api/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ package api

import (
"context"

Check failure on line 4 in pkg/api/servers.go

View workflow job for this annotation

GitHub Actions / Static Checks

File is not `gofumpt`-ed (gofumpt)
"glide/pkg/api/http"
"sync"

Check failure on line 6 in pkg/api/servers.go

View workflow job for this annotation

GitHub Actions / Static Checks

File is not `gofumpt`-ed (gofumpt)
"glide/pkg/api/http"
)

type ServerManager struct {
httpServer *http.HTTPServer
httpServer *http.Server
shutdownWG *sync.WaitGroup
}

func NewServerManager(httpConfig *http.HTTPServerConfig) (*ServerManager, error) {
httpServer, err := http.NewHttpServer(httpConfig)
func NewServerManager(httpConfig *http.ServerConfig) (*ServerManager, error) {
httpServer, err := http.NewServer(httpConfig)
// TODO: init other servers like gRPC in future

if err != nil {
return nil, err
}
Expand Down
38 changes: 18 additions & 20 deletions pkg/buildAPIRequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ package pkg

import (
"errors"
"github.com/go-playground/validator/v10"
"fmt"

Check failure on line 7 in pkg/buildAPIRequest.go

View workflow job for this annotation

GitHub Actions / Static Checks

File is not `gofumpt`-ed (gofumpt)

"github.com/go-playground/validator/v10"
"glide/pkg/providers"
"glide/pkg/providers/openai"
)
Expand All @@ -15,30 +16,27 @@ type ProviderConfigs = pkg.ProviderConfigs
// Initialize configList

var configList = map[string]interface{}{
"openai": openai.OpenAIConfig,
"openai": openai.OpenAIConfig,
}

// Create a new validator instance
var validate *validator.Validate = validator.New()


func BuildAPIRequest(provider string, params map[string]string, mode string) (interface{}, error) {
// provider is the name of the provider, e.g. "openai", params is the map of parameters from the client,
// mode is the mode of the provider, e.g. "chat", configList is the list of provider configurations

// provider is the name of the provider, e.g. "openai", params is the map of parameters from the client,
// mode is the mode of the provider, e.g. "chat", configList is the list of provider configurations

var providerConfig map[string]interface{}
if config, ok := configList[provider].(ProviderConfigs); ok {
if modeConfig, ok := config[mode].(map[string]interface{}); ok {
providerConfig = modeConfig
}
}
if modeConfig, ok := config[mode].(map[string]interface{}); ok {
providerConfig = modeConfig
}
}

// If the provider is not supported, return an error
if providerConfig == nil {
return nil, errors.New("unsupported provider")
}

// If the provider is not supported, return an error
if providerConfig == nil {
return nil, errors.New("unsupported provider")
}

// Build the providerConfig map by iterating over the keys in the providerConfig map and checking if the key exists in the params map

Expand All @@ -50,10 +48,10 @@ func BuildAPIRequest(provider string, params map[string]string, mode string) (in

// Validate the providerConfig map using the validator package
err := validate.Struct(providerConfig)
if err != nil {
// Handle validation error
return nil, fmt.Errorf("validation error: %v", err)
}
if err != nil {
// Handle validation error
return nil, fmt.Errorf("validation error: %v", err)
}
// If everything is fine, return the providerConfig and nil error
return providerConfig, nil
return providerConfig, nil
}
3 changes: 1 addition & 2 deletions pkg/cmd/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ func NewCLI() *cobra.Command {
Version: pkg.GetVersion(),
RunE: func(cmd *cobra.Command, args []string) error {
gateway, err := pkg.NewGateway()

if err != nil {
return err
}

return gateway.Run(cmd.Context())
},
//SilenceUsage: true,
// SilenceUsage: true,
}

return cli
Expand Down
10 changes: 5 additions & 5 deletions pkg/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package pkg
import (
"context"
"fmt"
"glide/pkg/api"
"glide/pkg/api/http"
"go.uber.org/multierr"
"os"
"os/signal"
"syscall"

"glide/pkg/api"
"glide/pkg/api/http"
"go.uber.org/multierr"
)

// Gateway represents an instance of running Glide gateway.
Expand All @@ -23,8 +24,7 @@ type Gateway struct {
}

func NewGateway() (*Gateway, error) {
serverManager, err := api.NewServerManager(&http.HTTPServerConfig{})

serverManager, err := api.NewServerManager(&http.ServerConfig{})
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/providers/openai/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net/http"
)


// provides the base URL and headers for the OpenAI API
type ProviderAPIConfig struct {
BaseURL string
Expand All @@ -15,7 +14,7 @@ type ProviderAPIConfig struct {
Embed string
}

func OpenAIAPIConfig(APIKey string) *ProviderAPIConfig {
func APIConfig(_ string) *ProviderAPIConfig {
return &ProviderAPIConfig{
BaseURL: "https://api.openai.com/v1",
Headers: func(APIKey string) http.Header {
Expand Down
6 changes: 3 additions & 3 deletions pkg/providers/openai/chat.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package openai

type OpenAiProviderConfig struct {
type ProviderConfig struct {
Model string `json:"model" validate:"required,lowercase"`
Messages string `json:"messages" validate:"required"` // does this need to be updated to []string?
MaxTokens int `json:"max_tokens" validate:"omitempty,gte=0"`
Expand Down Expand Up @@ -31,8 +31,8 @@ var defaultMessage = `[
]`

// Provide the request body for OpenAI's ChatCompletion API
func OpenAiChatDefaultConfig() OpenAiProviderConfig {
return OpenAiProviderConfig{
func ChatDefaultConfig() ProviderConfig {
return ProviderConfig{
Model: "gpt-3.5-turbo",
Messages: defaultMessage,
MaxTokens: 100,
Expand Down
7 changes: 3 additions & 4 deletions pkg/providers/openai/index.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package openai

import (
"glide/pkg/providers"
"glide/pkg/providers"
)


// TODO: this needs to be imported into buildAPIRequest.go
var OpenAIConfig = pkg.ProviderConfigs{
"api": OpenAIAPIConfig,
"chat": OpenAiChatDefaultConfig,
"api": APIConfig,
"chat": ChatDefaultConfig,
}

0 comments on commit 35b37d7

Please sign in to comment.