{"id":25760888,"url":"https://github.com/karim-w/stdlib","last_synced_at":"2025-02-26T18:28:07.078Z","repository":{"id":59044929,"uuid":"521974184","full_name":"Karim-W/stdlib","owner":"Karim-W","description":"a collection of standard library modules for go i use in my projects. The package offers a set of modules that are useful in most projects such as logging,tracing,pooling,caching,HTTP Request,etc.","archived":false,"fork":false,"pushed_at":"2024-08-29T04:19:30.000Z","size":321,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-08-29T05:34:39.026Z","etag":null,"topics":["cache","golang","http-client","pool","redis","sql"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Karim-W.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-08-06T14:50:54.000Z","updated_at":"2024-08-29T04:19:01.000Z","dependencies_parsed_at":"2023-10-12T13:20:59.572Z","dependency_job_id":"30bba55d-d7d1-49c2-9de4-1043c90a5bab","html_url":"https://github.com/Karim-W/stdlib","commit_stats":null,"previous_names":[],"tags_count":32,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Karim-W%2Fstdlib","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Karim-W%2Fstdlib/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Karim-W%2Fstdlib/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Karim-W%2Fstdlib/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Karim-W","download_url":"https://codeload.github.com/Karim-W/stdlib/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240910707,"owners_count":19877232,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cache","golang","http-client","pool","redis","sql"],"created_at":"2025-02-26T18:28:06.106Z","updated_at":"2025-02-26T18:28:07.070Z","avatar_url":"https://github.com/Karim-W.png","language":"Go","readme":"# Stdlib\n![stdlib](docs/stdlib.png)\u003cbr/\u003e\n[![codecov](https://codecov.io/gh/Karim-W/stdlib/branch/main/graph/badge.svg?token=RPBAD4H6Y9)](https://codecov.io/gh/Karim-W/stdlib)\n\nThis is a collection of standard library modules for go i use in my projects. The package offers a set of modules that are useful in most projects such as logging,tracing,pooling,caching,HTTP Request,etc.\n## Modules\n- [Cache](#cache)\n- [HTTP Client](#http-client)\n- [SQL](#sql)\n- [Pool](#pool)\n\n## Usage\n\n### Importing\nin code\n\n```go\nimport \"github.com/karim-w/stdlib\"\n```\nand\n```bash\ngo get github.com/karim-w/stdlib\n```\n\n### Cache\n\nCaching tools are provided to make it easier to use caching in your projects. The package provides two types of caching, in memory and redis. The package uses the [go-cache](\"github.com/patrickmn/go-cache\") package for in memory caching and the [go-redis](\"github.com/go-redis/redis\") package for redis caching.\n\n#### Redis Cache\nuses the [go-redis](\"github.com/go-redis/redis\") package\n\n```go\ncache, err := InitRedisCache(\"redis://:@localhost:6379/0\")\nif err != nil {\n\tt.Error(err)\n}\nerr = cache.SetCtx(context.TODO(),\"key\",map[string]interface{}{\n\t\"value\":8,\n})\n```\n#### In Memory Cache\nuses the [go-cache](\"github.com/patrickmn/go-cache\") package\n\n```go\ncache, err := InitInMemoryCache(\n\t\ttime.Minute,\n\t\ttime.Minute*2,\n\t)\nif err != nil {\n\tt.Error(err)\n}\nerr = cache.SetCtx(context.TODO(),\"key\",map[string]interface{}{\n\t\"value\":8,\n})\n```\n#### Extentedable via\n##### Logger\nuse the ZapLogger or any other logger that implements the Logger interface\n```go\nvar logger *zap.Logger\nnewCache := cache.WithLogger(logger)\n```\n##### Tracer\ncurrently only support the applicationinsights tracer loacted at [appinsights](\"github.com/BetaLixT/appInsightsTrace\")\n```go\nvar tracer *tracer.AppInsightsCore\nnewCache := cache.WithTracer(tracer)\n```\n\n### HTTP Client\nThe package provides a wrapper for the [http](\"net/http\") package to make it easier to use. The package provides a client that can be used to make HTTP requests. The package also provides a middleware that can be used to add tracing and logging to the requests.\n\n#### Initializing\n##### Plain client\n```go\nvar logger *zap.Logger\nclient := stdlib.ClientProvider()\n```\n\u003e Note: This function **WILL** panic if it fails to initialize a logger\n\n##### Traceable client\n```go\nvar logger *zap.Logger\nvar tracer *tracer.AppInsightsCore\nclient := stdlib.TracedClientProvider(\n\ttracer,\n\tlogger,\n)\n```\n##### Client Options\n```go\ntype ClientOptions struct {\n\tAuthorization string             //Authorization header\n\tContentType   string             //Content-Type header\n\tQuery         string             //Query string to append to the url\n\tHeaders       *map[string]string //Headers to add to the request\n\tTimeout       *time.Time         //Timeout for the request\n}\n```\n\n#### Usage\nThe client provides 5 Main Methods to make requests which are `GET` , `POST` , `PUT` , `DELETE` , `PATCH`. Each method takes a context, url, options, body and response. The response MUST be a pointer to the response type.\n`GET` And `Del` do not take a body in the request.\n##### GET\nMake a GET request\n```go\nResponse := interface{}\t\t//any type\ncode,err := client.Get(\n\tcontext.TODO(),\n\t\"http://localhost:8080\",\n\t\u0026stdlib.ClientOptions{},\n\t\u0026Response,\n)\n```\n\u003e Note: MUST pass a pointer to the response\n##### POST\nMake a POST request\n```go\nBody := interface{}\t\t\t//any type\nResponse := interface{}\t\t//any type\ncode,err := client.Post(\n\tcontext.TODO(),\n\t\"http://localhost:8080\",\n\t\u0026stdlib.ClientOptions{},\n\tBody,\n\t\u0026Response,\n)\n```\n\u003e Note: MUST pass a pointer to the response\n\n##### PUT\nMake a PUT request\n```go\nBody := interface{}\t\t\t//any type\nResponse := interface{}\t\t//any type\ncode,err := client.Put(\n\tcontext.TODO(),\n\t\"http://localhost:8080\",\n\t\u0026stdlib.ClientOptions{},\n\tBody,\n\t\u0026Response,\n)\n```\n\u003e Note: MUST pass a pointer to the response\n##### PATCH\nMake a PATCH request\n```go\nBody := interface{}\t\t\t//any type\nResponse := interface{}\t\t//any type\ncode,err := client.Patch(\n\tcontext.TODO(),\n\t\"http://localhost:8080\",\n\t\u0026stdlib.ClientOptions{},\n\tBody,\n\t\u0026Response,\n)\n```\n\u003e Note: MUST pass a pointer to the response\n\n##### DELETE\nMake a DELETE request\n```go\nResponse := interface{}\t\t//any type\ncode,err := client.Del(\n\tcontext.TODO(),\n\t\"http://localhost:8080\",\n\t\u0026stdlib.ClientOptions{},\n\t\u0026Response,\n)\n```\n\u003e Note: MUST pass a pointer to the response\n\n#### Extentedable via\n##### Authorization\n```go\nclient.SetAuthHandler(authProvider)\n```\nAuth provider must implement the `AuthProvider` interface\n```go\ntype AuthProvider interface {\n\tGetAuthHeader() string\n}\n```\n### SQL\nThe package provides a wrapper for the [sql](\"database/sql\") package to make it easier to use. The package provides a client that can be used to make SQL requests. The package also provides a middleware that can be used to add tracing and logging to the requests.\n\n#### Initializing\n\n\u003e Only supports Postgres, planning to add more\n\n\u003e Will not support MSSQL\n```go\ndb := stdlib.NativeDatabaseProvider(\n\tdatabaseDriver,\n\tdatabaseConnectionString,\n)\n```\nfor tracing and logging use the `TracedNativeDBWrapper` function\n```go\ndb := stdlib.TracedNativeDBWrapper(\n\tdatabaseDriver,\n\tdatabaseConnectionString,\n\ttracer,\n\tdbName,\n)\ndbwithlogger := db.WithLogger(logger)\n```\n\u003e WARNING: Both functions will panic if they fail to initialize a logger\n\n#### Usage\nThe client provides 3 Main Methods to make requests which are `Query` , `Exec` , `QueryRow`. Each with a variant for context they all take in query, args.\n\n##### QueryRow\nMake a Query request that returns a single row\n```go\nvar res int\nrow := db.Query(\n\t\"SELECT COUNT(*) FROM table\",\n\t[]interface{}{}...,\n)\nerr:=row.Scan(\u0026res)\n```\n##### QueryRowContext\nMake a Query request with context that returns a single row\n```go\nvar res int\nrow := db.QueryContext(\n\tcontext.TODO(),\n\t\"SELECT COUNT(*) FROM table\",\n\t[]interface{}{}...,\n)\nerr:=row.Scan(\u0026res)\n```\n\n##### Exec\nExecute a query that does not return a result rather the number of rows affected\n```go\nres,err := db.Exec(\n\t\"INSERT INTO table VALUES($1,$2)\",\n\t[]interface{}{}...,\n)\n```\n##### ExecContext\nExecute a query that does not return a result rather the number of rows affected with context\n```go\nres,err := db.ExecContext(\n\tcontext.TODO(),\n\t\"INSERT INTO table VALUES($1,$2)\",\n\t[]interface{}{}...,\n)\n```\n\n##### QueryRow\nMake a Query request that returns multiple rows\n```go\nres := []int{}\nrows,err := db.Query(\n\t\"SELECT COUNT(*) FROM table\",\n\t[]interface{}{}...,\n)\nfor rows.Next(){\n\tvar i int\n\terr:=rows.Scan(\u0026i)\n\tif err!=nil{\n\t\treturn err\n\t}\n\tres = append(res,i)\n}\n```\n##### QueryRowContext\nMake a Query request with context that returns multiple rows\n```go\nres := []int{}\nrows,err := db.QueryContext(\n\tcontext.TODO(),\n\t\"SELECT COUNT(*) FROM table\",\n\t[]interface{}{}...,\n)\nfor rows.Next(){\n\tvar i int\n\terr:=rows.Scan(\u0026i)\n\tif err!=nil{\n\t\treturn err\n\t}\n\tres = append(res,i)\n}\n```\n#### Begin\nBegin starts and returns a new transaction Object\n```go\ntx,err := db.Begin()\n```\n#### BeginTx\nBegin starts and returns a new transaction Object\n```go\ntx,err := db.BeginTx(context.TODO(),opts) //opts is a *sql.TxOptions\n```\n\n#### Extentedable via\n##### Logger\n```go\nnewDb := db.withLogger(logger) //*zap.Logger\n```\n\n### Pooler\nThe package provides Pooling utility for any type of object. The package rotates the objects in the pool and provides a way to get the object from the pool in a round robin thread safe manner.\n\n#### Initializing\n```go\ntype customType struct{\n\t//some fields\n}\n\npool,err := stdlib.NewPooler(\n\tfunc () *customType{\n\t\treturn \u0026customType{}\n\t},\n\t\u0026stdlib.PoolerOptions{\n\t\tPoolSize: 10,\n\t},\n)\n```\n\n#### Usage\n##### Get\nGet an object from the pool\n```go\nobj := pool.Get()\n```\n\n##### Size\nGet the size of the pool\n```go\nsize := pool.Size()\n```\n\n##### Clear\nClear the pool\n```go\npool.Clear()\n```\n\n### Tests\n#### Coverage\n#### Grid\n![img](https://codecov.io/gh/Karim-W/stdlib/branch/main/graphs/tree.svg?token=RPBAD4H6Y9)\n#### Sunburst\n![img](https://codecov.io/gh/Karim-W/stdlib/branch/main/graphs/sunburst.svg?token=RPBAD4H6Y9)\n#### Icicle\n![img](https://codecov.io/gh/Karim-W/stdlib/branch/main/graphs/icicle.svg?token=RPBAD4H6Y9)\n\n\n\n\n\n\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkarim-w%2Fstdlib","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkarim-w%2Fstdlib","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkarim-w%2Fstdlib/lists"}