Use logger middleware

This commit is contained in:
Lunny Xiao 2021-11-15 21:27:14 +08:00
parent ecca4db772
commit 97a0cf06ab
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
6 changed files with 28 additions and 558 deletions

View File

@ -12,30 +12,22 @@ GOFMT ?= gofmt -s
ifneq ($(DRONE_TAG),)
VERSION ?= $(subst v,,$(DRONE_TAG))
TEA_VERSION ?= $(VERSION)
DEPLOYER_VERSION ?= $(VERSION)
else
ifneq ($(DRONE_BRANCH),)
VERSION ?= $(subst release/v,,$(DRONE_BRANCH))
else
VERSION ?= master
endif
TEA_VERSION ?= $(shell git describe --tags --always | sed 's/-/+/' | sed 's/^v//')
DEPLOYER_VERSION ?= $(shell git describe --tags --always | sed 's/-/+/' | sed 's/^v//')
endif
VERSION_TAG ?= $(shell sed 's/+/_/' <<< $(TEA_VERSION))
VERSION_TAG ?= $(shell sed 's/+/_/' <<< $(DEPLOYER_VERSION))
TAGS ?=
LDFLAGS := -X "main.Version=$(TEA_VERSION)" -X "main.Tags=$(TAGS)" -s -w
ifeq ($(STATIC),true)
# NOTE: clean up this mess, when https://github.com/golang/go/issues/26492 is resolved
# static_build is a defacto standard tag used in go packages
TAGS := osusergo,netgo,static_build,$(TAGS)
LDFLAGS := $(LDFLAGS) -linkmode=external -extldflags "-static-pie" -X "main.Tags=$(TAGS)"
export CGO_ENABLED=1 # needed for linkmode=external
endif
LDFLAGS := -X "main.Version=$(DEPLOYER_VERSION)" -X "main.Tags=$(TAGS)" -s -w
# override to allow passing additional goflags via make CLI
override GOFLAGS := $(GOFLAGS) -mod=vendor -tags '$(TAGS)' -ldflags '$(LDFLAGS)'
override GOFLAGS := $(GOFLAGS) -tags '$(TAGS)' -ldflags '$(LDFLAGS)'
PACKAGES ?= $(shell $(GO) list ./... | grep -v /vendor/)
SOURCES ?= $(shell find . -name "*.go" -type f)
@ -51,7 +43,7 @@ all: build
.PHONY: clean
clean:
$(GO) clean -mod=vendor -i ./...
$(GO) clean -i ./...
rm -rf $(EXECUTABLE) $(DIST)
.PHONY: fmt
@ -134,7 +126,7 @@ endif
.PHONY: build-image
build-image:
docker build --build-arg VERSION=$(TEA_VERSION) -t gitea/pr-deployer:$(VERSION_TAG) .
docker build --build-arg VERSION=$(DEPLOYER_VERSION) -t gitea/pr-deployer:$(VERSION_TAG) .
.PHONY: release
release: release-dirs release-os release-compress release-check
@ -148,7 +140,14 @@ release-os:
@hash gox > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
cd /tmp && $(GO) get -u github.com/mitchellh/gox; \
fi
CGO_ENABLED=0 gox -verbose -tags="bindata" -cgo=false $(GOFLAGS) -osarch='!darwin/386 !darwin/arm' -os="windows linux darwin" -arch="amd64 arm arm64" -output="$(DIST)/release/pr-deployer-$(VERSION)-{{.OS}}-{{.Arch}}"
gox -verbose -tags="bindata" -cgo=false $(GOFLAGS) -osarch='!darwin/386 !darwin/arm' -os="windows linux darwin" -arch="amd64 arm arm64" -output="$(DIST)/release/pr-deployer-$(VERSION)-{{.OS}}-{{.Arch}}"
.PHONY: release-linux-amd64
release-linux-amd64:
@hash gox > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
cd /tmp && $(GO) get -u github.com/mitchellh/gox; \
fi
gox -verbose -tags="bindata" -cgo=false $(GOFLAGS) -os="linux" -arch="amd64" -output="$(DIST)/release/pr-deployer-$(VERSION)-{{.OS}}-{{.Arch}}"
.PHONY: release-compress
release-compress:

28
go.mod
View File

@ -4,6 +4,7 @@ go 1.17
require (
gitea.com/go-chi/session v0.0.0-20211013065435-7d334f340c09
github.com/chi-middleware/logrus-logger v0.2.0
github.com/cloudflare/cloudflare-go v0.26.0
github.com/go-chi/chi/v5 v5.0.4
github.com/go-sql-driver/mysql v1.6.0
@ -19,42 +20,19 @@ require (
)
require (
github.com/Microsoft/go-winio v0.4.17 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 // indirect
github.com/acomagu/bufpipe v1.0.3 // indirect
github.com/containerd/containerd v1.5.7 // indirect
github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/docker/docker v20.10.10+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/emirpasic/gods v1.12.0 // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/go-git/gcfg v1.5.0 // indirect
github.com/go-git/go-billy/v5 v5.3.1 // indirect
github.com/go-git/go-git/v5 v5.4.2 // indirect
github.com/goccy/go-json v0.7.4 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/json-iterator/go v1.1.11 // indirect
github.com/kevinburke/ssh_config v0.0.0-20201106050909-4977a11b4351 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/pelletier/go-toml v1.9.3 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
@ -62,18 +40,14 @@ require (
github.com/subosito/gotenv v1.2.0 // indirect
github.com/syndtr/goleveldb v1.0.0 // indirect
github.com/unknwon/com v1.0.1 // indirect
github.com/xanzy/ssh-agent v0.3.0 // indirect
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
golang.org/x/net v0.0.0-20210510120150-4163338589ed // indirect
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22 // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect
google.golang.org/grpc v1.38.0 // indirect
google.golang.org/protobuf v1.26.0 // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
xorm.io/builder v0.3.9 // indirect
)

515
go.sum

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,8 @@
package models
import (
"fmt"
"xorm.io/xorm"
"gitea.com/gitea/pr-deployer/pkgs/settings"
@ -14,7 +16,7 @@ func Init() error {
var err error
engine, err = xorm.NewEngine(settings.DBType, settings.DBConnStr)
if err != nil {
return err
return fmt.Errorf("NewEngine: %v", err)
}
return engine.Sync(new(SystemConfig))

View File

@ -8,6 +8,7 @@ import (
"path/filepath"
"strings"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"golang.org/x/oauth2"
)
@ -143,13 +144,14 @@ func Init() error {
Proxy.ProxyHosts = viper.GetStringSlice("Proxy.ProxyHosts")
DBType = viper.GetString("DBType")
if DBType != "mysql" {
if DBType == "" {
DBType = "mysql"
}
DBConnStr = viper.GetString("DBConnStr")
if DBConnStr == "" {
return errors.New("DBConnStr is empty")
}
log.Tracef("DBType: %v DBConnStr: %v", DBType, DBConnStr)
DockerComposeTemplateFile = viper.GetString("DockerComposeTemplateFile")
if DockerComposeTemplateFile == "" {

View File

@ -14,8 +14,8 @@ import (
"golang.org/x/oauth2"
"gitea.com/go-chi/session"
logger "github.com/chi-middleware/logrus-logger"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/google/go-github/v39/github"
log "github.com/sirupsen/logrus"
"github.com/unrolled/render"
@ -59,7 +59,7 @@ func Web(publicFS, templateFS fs.FS) {
c := chi.NewRouter()
c.Use(session.Sessioner())
c.Use(middleware.Logger)
c.Use(logger.Logger("router", log.StandardLogger()))
c.Get("/", Home)
c.Get("/callback", OAuth2Callback)