Skip to content

Commit

Permalink
Merge pull request #121 from nabbar/package_static
Browse files Browse the repository at this point in the history
# Package Static :
- refactor completly package
- use embed go func
- expand managment for more capabilities
- implement router status new model
- fix race detection

# Change
- bump dependancies
- remove useless dependancies
  • Loading branch information
nabbar authored Feb 10, 2022
2 parents 529cbcf + 35d33bd commit adc69db
Show file tree
Hide file tree
Showing 6 changed files with 921 additions and 254 deletions.
12 changes: 4 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ require (
github.com/gin-gonic/gin v1.7.7
github.com/go-ldap/ldap/v3 v3.4.1
github.com/go-playground/validator/v10 v10.10.0
github.com/gobuffalo/packr v1.30.1
github.com/google/go-github/v33 v33.0.0
github.com/hashicorp/go-hclog v1.1.0
github.com/hashicorp/go-retryablehttp v0.7.0
Expand All @@ -32,14 +31,14 @@ require (
github.com/sirupsen/logrus v1.8.1
github.com/spf13/jwalterweatherman v1.1.0
github.com/vbauerster/mpb/v5 v5.4.0
github.com/xanzy/go-gitlab v0.54.4
github.com/xanzy/go-gitlab v0.55.0
github.com/xhit/go-simple-mail v2.2.2+incompatible
github.com/xujiajun/nutsdb v0.6.0
github.com/xujiajun/utils v0.0.0-20190123093513-8bf096c4f53b
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/sys v0.0.0-20220207234003-57398862261d
golang.org/x/sys v0.0.0-20220209214540-3681064d5158
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211
)

Expand Down Expand Up @@ -77,8 +76,6 @@ require (
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/gobuffalo/envy v1.10.1 // indirect
github.com/gobuffalo/packd v1.0.1 // indirect
github.com/gogo/protobuf v1.3.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.3 // indirect
Expand All @@ -97,7 +94,6 @@ require (
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/jaytaylor/html2text v0.0.0-20211105163654-bc68cce691ba // indirect
github.com/joho/godotenv v1.4.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/juju/ratelimit v1.0.2-0.20191002062651-f60b32039441 // indirect
github.com/klauspost/compress v1.14.2 // indirect
Expand All @@ -120,7 +116,7 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/pkg/term v1.2.0-beta.2 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/rogpeppe/go-internal v1.8.1 // indirect
github.com/rogpeppe/go-internal v1.8.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
Expand All @@ -133,7 +129,7 @@ require (
github.com/x448/float16 v0.8.4 // indirect
github.com/xujiajun/mmap-go v1.0.1 // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect
golang.org/x/crypto v0.0.0-20220208233918-bba287dce954 // indirect
golang.org/x/crypto v0.0.0-20220209195652-db638375bc3a // indirect
golang.org/x/exp v0.0.0-20200513190911-00229845015e // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20211116232009-f0f3c7e86c11 // indirect
Expand Down
45 changes: 23 additions & 22 deletions logger/options.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
/***********************************************************************************************************************
/*
* MIT License
*
* MIT License
* Copyright (c) 2021 Nicolas JUHEL
*
* Copyright (c) 2021 Nicolas JUHEL
* 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:
*
* 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:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* 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.
*
* 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.
*
*
**********************************************************************************************************************/
*/

package logger

import (
"fmt"
valid "github.com/go-playground/validator/v10"
liberr "github.com/nabbar/golib/errors"
"os"
"strings"

valid "github.com/go-playground/validator/v10"
liberr "github.com/nabbar/golib/errors"
)

type FuncCustomConfig func(log Logger)
Expand Down Expand Up @@ -170,6 +170,7 @@ func (o Options) RegisterFuncUpdateLevel(fct FuncCustomConfig) {
o.change = fct
}

// Validate allow checking if the options' struct is valid with the awaiting model
func (o *Options) Validate() liberr.Error {
var e = ErrorValidatorError.Error(nil)

Expand Down
42 changes: 18 additions & 24 deletions static/error.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2020 Nicolas JUHEL
* Copyright (c) 2022 Nicolas JUHEL
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -29,11 +29,11 @@ package static
import "github.com/nabbar/golib/errors"

const (
EMPTY_PARAMS errors.CodeError = iota + errors.MinPkgStatic
EMPTY_PACKED
INDEX_NOT_FOUND
INDEX_REQUESTED_NOT_SET
FILE_NOT_FOUND
ErrorParamsEmpty errors.CodeError = iota + errors.MinPkgStatic
ErrorFileInfo
ErrorFileOpen
ErrorFiletemp
ErrorFileNotFound
)

var isCodeError = false
Expand All @@ -43,30 +43,24 @@ func IsCodeError() bool {
}

func init() {
isCodeError = errors.ExistInMapMessage(EMPTY_PARAMS)
errors.RegisterIdFctMessage(EMPTY_PARAMS, getMessage)
isCodeError = errors.ExistInMapMessage(ErrorParamsEmpty)
errors.RegisterIdFctMessage(ErrorParamsEmpty, getMessage)
}

func getMessage(code errors.CodeError) (message string) {
switch code {

case EMPTY_PARAMS:
return "given parameters is empty"

case EMPTY_PACKED:
return "packed file is empty"

case INDEX_NOT_FOUND:
return "mode index is defined but index.(html|htm) is not found"

case INDEX_REQUESTED_NOT_SET:
return "request call index but mode index is false"

case FILE_NOT_FOUND:
return "requested packed file is not found"

case errors.UNK_ERROR:
return ""
case ErrorParamsEmpty:
return "given parameters is empty"
case ErrorFileInfo:
return "cannot get file info"
case ErrorFileOpen:
return "cannot open file"
case ErrorFiletemp:
return "cannot create temporary file"
case ErrorFileNotFound:
return "file not found"
}

return ""
Expand Down
93 changes: 93 additions & 0 deletions static/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* MIT License
*
* Copyright (c) 2022 Nicolas JUHEL
*
* 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:
*
* 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.
*
*
*/

package static

import (
"embed"
"io"
"os"
"sync"
"time"

"github.com/gin-gonic/gin"

liberr "github.com/nabbar/golib/errors"
libiot "github.com/nabbar/golib/ioutils"
liblog "github.com/nabbar/golib/logger"
librtr "github.com/nabbar/golib/router"
libsts "github.com/nabbar/golib/status"
)

type Static interface {
RegisterRouter(route string, register librtr.RegisterRouter, router ...gin.HandlerFunc)
RegisterRouterInGroup(route, group string, register librtr.RegisterRouterInGroup, router ...gin.HandlerFunc)

RegisterLogger(log func() liblog.Logger)

SetDownload(pathFile string, flag bool)
SetIndex(group, route, pathFile string)
GetIndex(group, route string) string
SetRedirect(srcGroup, srcRoute, dstGroup, dstRoute string)
GetRedirect(srcGroup, srcRoute string) string
SetSpecific(group, route string, router gin.HandlerFunc)
GetSpecific(group, route string) gin.HandlerFunc

IsDownload(pathFile string) bool
IsIndex(pathFile string) bool
IsIndexForRoute(pathFile, group, route string) bool
IsRedirect(group, route string) bool

Has(pathFile string) bool
List(rootPath string) ([]string, liberr.Error)
Find(pathFile string) (io.ReadCloser, liberr.Error)
Info(pathFile string) (os.FileInfo, liberr.Error)
Temp(pathFile string) (libiot.FileProgress, liberr.Error)

Map(func(pathFile string, inf os.FileInfo) error) liberr.Error
UseTempForFileSize(size int64)

StatusInfo() (name string, release string, hash string)
StatusHealth() error
StatusComponent(mandatory bool, message libsts.FctMessage, infoCacheTimeout, healthCacheTimeout time.Duration, sts libsts.RouteStatus)

Get(c *gin.Context)
SendFile(c *gin.Context, filename string, size int64, isDownload bool, buf io.ReadCloser)
}

func New(content embed.FS, embedRootDir ...string) Static {
return &staticHandler{
m: sync.Mutex{},
l: nil,
c: content,
b: embedRootDir,
z: 0,
i: nil,
d: nil,
f: nil,
r: nil,
}
}
Loading

0 comments on commit adc69db

Please sign in to comment.