diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml new file mode 100644 index 00000000..ec0cbba5 --- /dev/null +++ b/.github/workflows/lint.yaml @@ -0,0 +1,100 @@ +name: lint + +on: + push: + branches: ["main", "develop"] + pull_request: + branches: ["main", "develop"] + +jobs: + lint: + name: Lint + runs-on: ubuntu-latest + timeout-minutes: 10 + steps: + - uses: actions/checkout@v3 + - name: Set up Go + uses: actions/setup-go@v4 + with: + 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: Gofumpt + run: gofumpt -l -w . && git diff --exit-code + + - name: Go Vet + run: go vet ./... + + - name: Go Tidy + run: go mod tidy && git diff --exit-code + + - name: Go Mod + run: go mod download + + - name: Go Mod Verify + run: go mod verify + + build: + name: Build + runs-on: ubuntu-latest + timeout-minutes: 10 + steps: + - uses: actions/checkout@v3 + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: "1.21" + check-latest: true + - name: Build + run: go build -v ./... + + static-checks: + name: Static Checks + runs-on: ubuntu-latest + timeout-minutes: 10 + steps: + - uses: actions/checkout@v3 + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: "1.21" + check-latest: true + + - name: Install staticcheck + run: go install honnef.co/go/tools/cmd/staticcheck@latest + + - name: Install nilaway + run: go install go.uber.org/nilaway/cmd/nilaway@latest + + - name: GolangCILint + uses: golangci/golangci-lint-action@v3.4.0 + with: + version: latest + args: --timeout 5m + + - name: Staticcheck + run: staticcheck ./... +# TODO: Ignore the issue in https://github.com/modelgateway/Glide/issues/32 +# - name: Nilaway +# run: nilaway ./... + + tests: + name: Tests + runs-on: ubuntu-latest + timeout-minutes: 10 + steps: + - uses: actions/checkout@v3 + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: "1.21" + check-latest: true + + - name: Test + run: go test -v -count=1 -race -shuffle=on -coverprofile=coverage.txt ./... diff --git a/.github/workflows/vuln.yaml b/.github/workflows/vuln.yaml new file mode 100644 index 00000000..bb3cf7f0 --- /dev/null +++ b/.github/workflows/vuln.yaml @@ -0,0 +1,40 @@ +name: security + +on: + push: + branches: [main, develop] + + pull_request: + branches: [main, develop] + + schedule: + - cron: '0 10 * * 1' # run "At 10:00 on Monday" + +jobs: + run: + name: Vulnerability Check + runs-on: ubuntu-latest + timeout-minutes: 5 + env: + GO111MODULE: on + steps: + - name: Install Go + uses: actions/setup-go@v4 + with: + go-version: '1.21.5' + check-latest: true + + - name: Checkout + uses: actions/checkout@v3 + + - name: Install govulncheck + run: go install golang.org/x/vuln/cmd/govulncheck@latest + + - name: Install gosec + run: go install github.com/securego/gosec/v2/cmd/gosec@latest + + - name: Govulncheck + run: govulncheck -test ./... + + - name: Govulncheck + run: gosec ./... diff --git a/.gitignore b/.gitignore index 951841c7..ccec7880 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ .idea dist .env +bin +glide +tmp +coverage.txt diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 00000000..3d40376e --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,19 @@ +output: + # Make output more digestible with quickfix in vim/emacs/etc. + sort-results: true + print-issued-lines: false + +linters: + enable: + - nolintlint + - revive + - staticcheck + +linters-settings: + govet: + # These govet checks are disabled by default, but they're useful. + enable: + - niliness + - reflectvaluecompare + - sortslice + - unusedwrite diff --git a/Makefile b/Makefile index 9d36809b..5db2a78a 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -10,18 +11,40 @@ 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}' -lint: ## Lint the source code + +install-checkers: ## Install static checkers + @echo "๐Ÿšš Downloading binaries.." + @GOBIN=$(CHECKER_BIN) go install mvdan.cc/gofumpt@latest + @GOBIN=$(CHECKER_BIN) go install go.uber.org/nilaway/cmd/nilaway@latest + @GOBIN=$(CHECKER_BIN) go install golang.org/x/vuln/cmd/govulncheck@latest + @GOBIN=$(CHECKER_BIN) go install github.com/securego/gosec/v2/cmd/gosec@latest + +lint: install-checkers ## Lint the source code + @echo "๐Ÿงน Formatting files.." + @go fmt ./... + @$(CHECKER_BIN)/gofumpt -l -w . @echo "๐Ÿงน Vetting go.mod.." @go vet ./... @echo "๐Ÿงน Cleaning go.mod.." @go mod tidy - @echo "๐Ÿงน Formatting files.." - @go fmt ./... + + +static-checks: install-checkers ## Static Analysis @echo "๐Ÿงน GoCI Lint.." @golangci-lint run ./... + @echo "๐Ÿงน Nilaway.." + @$(CHECKER_BIN)/nilaway ./... + +vuln: install-checkers ## Check for vulnerabilities + @echo "๐Ÿ” Checking for vulnerabilities" + @$(CHECKER_BIN)/govulncheck -test ./... + @$(CHECKER_BIN)/gosec -quiet -exclude=G104 ./... run: ## Run Glide @go run -ldflags $(LDFLAGS_COMMON) main.go build: ## Build Glide @go build -ldflags $(LDFLAGS_COMMON) -o ./dist/glide + +tests: ## Run tests + @go test -v -count=1 -race -shuffle=on -coverprofile=coverage.txt ./... diff --git a/glide b/glide deleted file mode 100755 index 39b0ef50..00000000 Binary files a/glide and /dev/null differ diff --git a/go.mod b/go.mod index 5b909391..1b1bf175 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/cloudwego/hertz v0.7.3 github.com/go-playground/validator/v10 v10.16.0 github.com/spf13/cobra v1.8.0 + go.uber.org/goleak v1.3.0 go.uber.org/multierr v1.11.0 ) @@ -16,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 @@ -32,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 ) diff --git a/go.sum b/go.sum index 84649808..0071384d 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= @@ -83,28 +84,29 @@ github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs= github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= golang.org/x/arch v0.0.0-20201008161808-52c3e6f60cff/go.mod h1:flIaEI6LNU6xOCD5PaJvn9wGP0agmIOqjrtsKGRguv4= 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= diff --git a/leak_test.go b/leak_test.go new file mode 100644 index 00000000..024a502a --- /dev/null +++ b/leak_test.go @@ -0,0 +1,11 @@ +package main + +import ( + _ "go.uber.org/goleak" +) + +// TODO: investigate why netpoll leaves pending goroutines +// https://github.com/modelgateway/Glide/issues/33 +//func TestMain(m *testing.M) { +// goleak.VerifyTestMain(m) +//} diff --git a/main.go b/main.go index 45dd7530..ea09a616 100644 --- a/main.go +++ b/main.go @@ -1,8 +1,9 @@ package main import ( - "glide/pkg/cmd" "log" + + "glide/pkg/cmd" ) func main() { diff --git a/pkg/api/http/config.go b/pkg/api/http/config.go index f9009fba..09f447d4 100644 --- a/pkg/api/http/config.go +++ b/pkg/api/http/config.go @@ -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), diff --git a/pkg/api/http/server.go b/pkg/api/http/server.go index 31774ec1..de4f0953 100644 --- a/pkg/api/http/server.go +++ b/pkg/api/http/server.go @@ -3,24 +3,25 @@ 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}) }) @@ -28,7 +29,7 @@ func (srv *HTTPServer) Run() error { 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)) diff --git a/pkg/api/servers.go b/pkg/api/servers.go index 44e8a676..bdc2152b 100644 --- a/pkg/api/servers.go +++ b/pkg/api/servers.go @@ -2,19 +2,19 @@ package api import ( "context" - "glide/pkg/api/http" "sync" + + "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 } diff --git a/pkg/buildAPIRequest.go b/pkg/buildAPIRequest.go index f28d22b1..0081074b 100644 --- a/pkg/buildAPIRequest.go +++ b/pkg/buildAPIRequest.go @@ -4,10 +4,12 @@ package pkg import ( "errors" - "github.com/go-playground/validator/v10" "fmt" + "glide/pkg/providers" "glide/pkg/providers/openai" + + "github.com/go-playground/validator/v10" ) type ProviderConfigs = pkg.ProviderConfigs @@ -15,30 +17,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 @@ -50,10 +49,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 } diff --git a/pkg/cmd/cli.go b/pkg/cmd/cli.go index 7a9619cd..7d011e36 100644 --- a/pkg/cmd/cli.go +++ b/pkg/cmd/cli.go @@ -1,8 +1,9 @@ package cmd import ( - "github.com/spf13/cobra" "glide/pkg" + + "github.com/spf13/cobra" ) // NewCLI Create a Glide CLI @@ -13,14 +14,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 diff --git a/pkg/gateway.go b/pkg/gateway.go index 417e8d9c..200c8cd1 100644 --- a/pkg/gateway.go +++ b/pkg/gateway.go @@ -3,12 +3,14 @@ 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. @@ -23,8 +25,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 } diff --git a/pkg/providers/openai/api.go b/pkg/providers/openai/api.go index 4a7a013b..26f9e6e9 100644 --- a/pkg/providers/openai/api.go +++ b/pkg/providers/openai/api.go @@ -5,7 +5,6 @@ import ( "net/http" ) - // provides the base URL and headers for the OpenAI API type ProviderAPIConfig struct { BaseURL string @@ -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 { diff --git a/pkg/providers/openai/chat.go b/pkg/providers/openai/chat.go index 064e7d46..61baf4ca 100644 --- a/pkg/providers/openai/chat.go +++ b/pkg/providers/openai/chat.go @@ -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"` @@ -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, diff --git a/pkg/providers/openai/index.go b/pkg/providers/openai/index.go index d7a7a60c..7dffb4e8 100644 --- a/pkg/providers/openai/index.go +++ b/pkg/providers/openai/index.go @@ -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, }