Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: move nested package to root #1

Merged
merged 4 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ jobs:
run: make test

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: coverage.out
flags: unittests

- name: Upload coverage
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v3
with:
name: coverage
path: coverage.out
Expand All @@ -74,7 +74,7 @@ jobs:
go: ["1.19"]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
with:
fetch-depth: 1
- uses: dominikh/[email protected]
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@

*.log
*.html
*.exe
*.exe

test_sample_data
26 changes: 18 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.0] - 2024
## [1.0]

### Added
### What's new

- Initial release
- Setup project structure
- Create utility functions
- Documentation
- Unit tests, benchmark tests, code coverage, static checks
- Plugin integration, CI, code quality checks
- Convert codebase to root level directly
- Improve metadata, description, README, and documentation
- Update functions, and dependencies

## [0.1.0 beta] - 20/11/2024

### What's new

- Init project structure, setup codebase, split layers
- Add metadata description
- Splitting packages, create functions
- Makeup unit tests, benchmark tests, code coverage, static checks
- Plugin integration(CI, code quality checks, support tools...)
- Generating documents, sample cases...

[**Full Changelog**](https://github.com/thuongtruong109/gouse/commits/v0.1.0)
42 changes: 18 additions & 24 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
Copyright (c) 2023 Tran Nguyen Thuong Truong. All rights reserved.
MIT License

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
Copyright (c) 2024 Tran Nguyen Thuong Truong

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
22 changes: 10 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,21 @@ install:
go install golang.org/x/perf/cmd/benchstat
go install honnef.co/go/tools/cmd/staticcheck@latest

start:
go run main.go

dev:
go run main.go -isDev=true
go run samples/main/index.go -isDev=true

build:
go build ./...

doc:
@echo "Generating docs..."
go run cmd/doc.go ./samples ./samples/api ./samples/array ./samples/cache ./samples/chart ./samples/config ./samples/crypto ./samples/date ./samples/function ./samples/helper ./samples/io ./samples/io/dir ./samples/io/file ./samples/io/path ./samples/math/check ./samples/math/operator ./samples/math/geometry ./samples/math/fomular ./samples/math/media ./samples/net ./samples/number ./samples/regex ./samples/strings ./samples/structs ./samples/types/cast ./samples/types/check
go run cmd/doc_gen.go ./samples
@echo "Done!"

test:
@echo "Running tests..."
go clean -testcache
go test -v -count=1 -cover -coverprofile=coverage.out ./array/... ./cache/... ./chart/... ./config/... ./cron/... ./date/... ./number/... ./regex/... ./strings/... ./structs/... ./types/...
go test -v -count=1 -cover -coverprofile=coverage.out ./*.go
go tool cover -func=coverage.out
@echo "Done!"

Expand All @@ -49,18 +46,19 @@ format:
gofmt -w -s . && goimports -w . && go fmt ./...
@echo "Done!"

lint:
@echo "Running lint..."
staticcheck ./...
@echo "Done!"
# lint:
# @echo "Running lint..."
# export PATH=$PATH:$(go env GOPATH)/bin
# staticcheck ./...
# @echo "Done!"

count:
@echo "Counting lines..."
bash count.sh public/count.svg array cache chart config connection cron crypto console date function helper io log math media net number os regex strings structs types
bash count.sh public/count.svg ./*.go
@echo "Done!"

pre:
make build && make test && make format && make lint && make doc
make build && make test && make format && make doc
git add .

clean:
Expand Down
41 changes: 28 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
<div align="center">
<img src="https://github.com/thuongtruong109/gouse/actions/workflows/ci.yml/badge.svg?branch=main" alt="ci_status">
<img src="https://github.com/thuongtruong109/gouse/actions/workflows/analysis.yml/badge.svg?branch=main" alt="codeql_status">
<a href="https://sonarcloud.io/summary/new_code?id=thuongtruong109_gouse"><img src="https://sonarcloud.io/api/project_badges/measure?project=thuongtruong109_gouse&metric=alert_status" alt="sonar"></a>
<a href="https://sonarcloud.io/summary/new_code?id=thuongtruong1009_gouse"><img src="https://sonarcloud.io/api/project_badges/measure?project=thuongtruong1009_gouse&metric=alert_status" alt="sonar"></a>
<a href="https://app.codacy.com/gh/thuongtruong109/gouse/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade"><img src="https://app.codacy.com/project/badge/Grade/21f940894abd4e0384ef8b84adc294da" alt="codacy"></a>
<a href="https://app.deepsource.com/gh/thuongtruong109/gouse/" target="_blank"><img alt="DeepSource" title="DeepSource" src="https://app.deepsource.com/gh/thuongtruong109/gouse.svg/?label=resolved+issues&show_trend=false&token=VqBk6AsowWePd3khy3AUkvXJ"/></a>
<a href="https://app.deepsource.com/gh/thuongtruong109/gouse/" target="_blank"><img alt="DeepSource" title="DeepSource" src="https://app.deepsource.com/gh/thuongtruong1009/gouse.svg/?label=resolved+issues&show_trend=false&token=VqBk6AsowWePd3khy3AUkvXJ"/></a>
<a href="https://goreportcard.com/report/thuongtruong109/gouse"><img src="https://goreportcard.com/badge/github.com/thuongtruong109/gouse" alt="go_report_card"></a>
<a href="https://codecov.io/gh/thuongtruong109/gouse"><img src="https://codecov.io/gh/thuongtruong109/gouse/branch/main/graph/badge.svg" alt="codecov"></a>
<a href="https://dl.circleci.com/status-badge/redirect/gh/thuongtruong109/gouse/tree/main"><img src="https://dl.circleci.com/status-badge/img/gh/thuongtruong109/gouse/tree/main.svg?style=svg" alt="circleci"></a>
<a href="https://codecov.io/gh/thuongtruong109/gouse"><img src="https://codecov.io/gh/thuongtruong1009/gouse/branch/main/graph/badge.svg" alt="codecov"></a>
<a href="https://dl.circleci.com/status-badge/redirect/gh/thuongtruong109/gouse/tree/main"><img src="https://dl.circleci.com/status-badge/img/gh/thuongtruong1009/gouse/tree/main.svg?style=svg" alt="circleci"></a>
<a href="https://pkg.go.dev/github.com/thuongtruong109/gouse"><img src="https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white&style=flat-square" alt="go.dev"></a>
<a href="https://godocs.io/github.com/thuongtruong109/gouse"><img alt="Godoc" src="https://godocs.io/github.com/thuongtruong109/gouse?status.svg"></a>
<!-- <a href="https://godocs.io/github.com/thuongtruong109/gouse"><img alt="Godoc" src="https://godocs.io/github.com/thuongtruong1009/gouse?status.svg"></a> -->
<img src="./public/count.svg" alt="gouse_functions_count">
<a href="LICENSE"><img src="https://img.shields.io/github/license/thuongtruong109/gouse" alt="license"></a>
<!-- <a href="LICENSE"><img src="https://img.shields.io/github/license/thuongtruong109/gouse" alt="license"></a> -->
<!-- <img alt="GitHub code size in bytes" src="https://img.shields.io/github/languages/code-size/thuongtruong109/gouse"> -->
<!-- <a href="https://app.codacy.com/gh/thuongtruong109/gouse/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_Coverage"><img src="https://app.codacy.com/project/badge/Coverage/21f940894abd4e0384ef8b84adc294da" alt="codacy"></a> -->
<!-- <a href="https://sourcegraph.com/github.com/thuongtruong109/gouse?badge"><img src="https://sourcegraph.com/github.com/thuongtruong109/gouse/-/badge.svg" alt="sourcegraph"></a> -->
</div>

# ![Banner](/public/banner.png)

<div align="center">
<table>
<tbody>
<td align="center">
<img width="2000" height="0"><br>
<sub>Made possible by my <a href="https://github.com/sponsors/thuongtruong109">Sponsor Program 💖</a></sub><br>
<img width="2000" height="0">
</td>
</tbody>
</table>
</div>

## 🧠 Why Gouse?

- Gouse is a modern essential [`Golang`](https://golang.org/) utility package delivering consistency, modularity, performance, & extras presets inspired by [`Lodash`](https://lodash.com/).
Expand Down Expand Up @@ -54,32 +66,35 @@
> Below is a list of modules that Gouse supports. This project is still in development stage, so not all features are available.

✅ Array <br/>
✅ Api <br/>
✅ Cache <br/>
✅ Chart <br/>
✅ Config <br/>
✅ Connection <br/>
✅ Console <br/>
✅ Cron <br/>
✅ Crypto <br/>
✅ Date <br/>
✅ Function <br/>
✅ Helper <br/>
✅ I/O <br/>
✅ Log <br/>
✅ Math <br/>
✅ Media <br/>
✅ Net <br/>
✅ Number <br/>
✅ OS <br/>
✅ Regex <br/>
✅ Struct <br/>
✅ String <br/>
✅ Tool <br/>
✅ Type <br/>

<!-- - [ ] API
- [ ] Cron
<!-- ✅ Connection <br/> -->
<!-- -
- [ ] Auth
- [ ] Error
- [ ] Json
- [ ] Lang
- [ ] Mail
- [ ] Media
- [ ] ORM
- [ ] Queue
- [ ] Socket
Expand Down Expand Up @@ -166,7 +181,7 @@ $ git clone https://github.com/thuongtruong109/gouse.git

## 📄 License

- Gouse is released under the [`MIT License`](LICENSE). See the LICENSE file for more information.
- Gouse is released under the [`MIT`](./LICENSE) License © 2024 [Tran Nguyen Thuong Truong](https://github.com/thuongtruong109)). See the for more information.

- For more information, see the [`Licensing FAQs`](https://opensource.org/faq#mit-vs-bsd).

Expand Down Expand Up @@ -226,4 +241,4 @@ $ git clone https://github.com/thuongtruong109/gouse.git
- [File handling in Golang](https://www.cloudhadoop.com/2018/11/learn-golang-tutorials-file-directory.html)
- [Golang x Github Actions](https://github.com/brpaz/github-actions-demo-go)
- [Dynamic HTML](https://css-tricks.com/dynamic-page-replacing-content)
- [Profiling Go Programs](https://blog.golang.org/pprof) - [Example](https://dev.to/immortalt/use-pprof-for-golang-program-memory-analysis-2cj6)
- [Profiling Go Programs](https://blog.golang.org/pprof) - [Pprof example](https://dev.to/immortalt/use-pprof-for-golang-program-memory-analysis-2cj6) - [Gws example](https://github.com/lxzan/gws)
65 changes: 54 additions & 11 deletions api/load_balancer.go → api.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
package api
package gouse

import (
"context"
"fmt"
"log"
"net"
"net/http"
"net/http/httputil"
"net/url"
"os"
"os/signal"
"sync"
"time"
)

type Backend struct {
type IBackend struct {
URL string `json:"url"`
IsDead bool
mutex sync.RWMutex
}

type Config struct {
ProxyPort string `json:"proxy"`
Backends []Backend `json:"backends"`
type ILbConfig struct {
ProxyPort string `json:"proxy"`
Backends []IBackend `json:"backends"`
}

func (backend *Backend) SetDead(b bool) {
func (backend *IBackend) SetDead(b bool) {
backend.mutex.Lock()
backend.IsDead = b
backend.mutex.Unlock()
}

func (backend *Backend) GetIsDead() bool {
func (backend *IBackend) GetIsDead() bool {
backend.mutex.RLock()
isAlive := backend.IsDead
backend.mutex.RUnlock()
Expand All @@ -36,7 +40,7 @@ func (backend *Backend) GetIsDead() bool {

var mutex sync.Mutex
var idx int = 0
var cfg Config
var cfg ILbConfig

func lbHandler(w http.ResponseWriter, r *http.Request) {
maxLen := len(cfg.Backends)
Expand Down Expand Up @@ -101,8 +105,8 @@ func HealthCheck() {
}
}

func LoadBalancer(proxyPort string, backends []Backend) {
cfg = Config{
func LoadBalancer(proxyPort string, backends []IBackend) {
cfg = ILbConfig{
ProxyPort: proxyPort,
Backends: backends,
}
Expand All @@ -112,10 +116,49 @@ func LoadBalancer(proxyPort string, backends []Backend) {
s := http.Server{
Addr: ":" + cfg.ProxyPort,
Handler: http.HandlerFunc(lbHandler),
ReadHeaderTimeout: 10 * time.Second, // Set an appropriate timeout value
ReadHeaderTimeout: 10 * time.Second,
}

if err := s.ListenAndServe(); err != nil {
log.Printf("Error starting server: %s", err.Error())
}
}

func GracefulShutdown() {
var wg sync.WaitGroup

wg.Add(1)

go func() {
defer wg.Done()

time.Sleep(5 * time.Second)
}()

c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)

fmt.Println("server started!")

srv := http.Server{
Addr: ":8080",
ReadHeaderTimeout: 5 * time.Second, // mitigate Slowloris attack by set timeout
}

go func() {
<-c
fmt.Println("server shutting down...")
go func() {
for {
fmt.Println("waiting for goroutines to finish...")
time.Sleep(1 * time.Second)
}
}()
wg.Wait()

if err := srv.Shutdown(context.Background()); err != nil {
fmt.Println("server shutdown error:", err)
}
}()
fmt.Println(srv.ListenAndServe())
}
13 changes: 0 additions & 13 deletions api/README.md

This file was deleted.

Loading
Loading