Compare commits
18 Commits
Author | SHA1 | Date | |
---|---|---|---|
52758c6e1f
|
|||
9e5f98a413
|
|||
899c406f09
|
|||
f0c5fbf68d
|
|||
d5fcc5cef9
|
|||
9c045e9fd3
|
|||
24a402ba7a | |||
412e404192 | |||
66ec7f86ab | |||
2e07cb9726 | |||
bbb68c48a1 | |||
85c60fd106 | |||
c910f12eae | |||
68ad99b767 | |||
5acc64de59 | |||
ef597063cf | |||
ce3c10c8a5 | |||
28adacf53e |
56
.drone.yml
56
.drone.yml
@@ -4,39 +4,31 @@ type: docker
|
||||
name: build
|
||||
|
||||
steps:
|
||||
- name: notify-start
|
||||
image: appleboy/drone-discord
|
||||
settings:
|
||||
webhook_id:
|
||||
from_secret: discord_webhook_id
|
||||
webhook_token:
|
||||
from_secret: discord_webhook_secret
|
||||
message: 'Starting building **{{repo.name}}#{{build.number}}@{{build.commit}}** @ {{datetime build.started "02-Jan-2006 15:04:05 MST" "Asia/Yekaterinburg"}} (See {{build.link}} for logs).'
|
||||
- name: lint
|
||||
image: code.pztrn.name/containers/mirror/golangci/golangci-lint:v1.46.2
|
||||
environment:
|
||||
CGO_ENABLED: 0
|
||||
commands:
|
||||
- golangci-lint run
|
||||
|
||||
- name: test
|
||||
image: code.pztrn.name/containers/mirror/golang:1.18.3-alpine
|
||||
environment:
|
||||
CGO_ENABLED: 0
|
||||
commands:
|
||||
- go test ./...
|
||||
|
||||
- name: docker
|
||||
image: plugins/docker
|
||||
settings:
|
||||
username:
|
||||
from_secret: dockerhub_user
|
||||
password:
|
||||
from_secret: dockerhub_password
|
||||
repo: pztrn/giredore
|
||||
auto_tag: true
|
||||
|
||||
- name: notify-end
|
||||
image: code.pztrn.name/containers/mirror/plugins/docker:20.13.0
|
||||
when:
|
||||
status:
|
||||
- success
|
||||
- failure
|
||||
image: appleboy/drone-discord
|
||||
branch: ["master"]
|
||||
settings:
|
||||
webhook_id:
|
||||
from_secret: discord_webhook_id
|
||||
webhook_token:
|
||||
from_secret: discord_webhook_secret
|
||||
message: "
|
||||
{{#success build.status}}
|
||||
**{{repo.name}}#{{build.number}}@{{build.commit}}** deployed.
|
||||
{{ else }}
|
||||
**{{repo.name}}#{{build.number}}@{{build.commit}}** failed. See {{build.link}}.
|
||||
{{/success}}"
|
||||
registry: code.pztrn.name
|
||||
username: drone
|
||||
password:
|
||||
from_secret: drone_secret
|
||||
repo: code.pztrn.name/apps/giredore
|
||||
auto_tag: true
|
||||
depends_on:
|
||||
- lint
|
||||
- test
|
||||
|
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
||||
*DS_Store*
|
||||
data/*
|
||||
.idea
|
||||
.vscode
|
||||
|
@@ -10,9 +10,19 @@ linters:
|
||||
- gocritic
|
||||
# Complains about main() lengths, which isn't an issue.
|
||||
- funlen
|
||||
# Magic numbers might be everywhere. Disabled for now.
|
||||
- gomnd
|
||||
# Deprecated.
|
||||
- exhaustivestruct
|
||||
linters-settings:
|
||||
lll:
|
||||
line-length: 420
|
||||
gocyclo:
|
||||
min-complexity: 40
|
||||
cyclop:
|
||||
max-complexity: 25
|
||||
|
||||
issues:
|
||||
exclude-rules:
|
||||
# There will be some ToDos.
|
||||
- linters:
|
||||
- godox
|
||||
text: "TODO"
|
||||
|
@@ -1,11 +1,12 @@
|
||||
FROM golang:1.13.1-alpine AS build
|
||||
FROM code.pztrn.name/containers/mirror/golang:1.18.3-alpine AS build
|
||||
|
||||
WORKDIR /go/src/sources.dev.pztrn.name/pztrn/giredore
|
||||
COPY . .
|
||||
|
||||
RUN cd /go/src/sources.dev.pztrn.name/pztrn/giredore/cmd/giredored && go build && cd ../giredorectl && go build
|
||||
ENV CGO_ENABLED=0
|
||||
RUN cd /go/src/sources.dev.pztrn.name/pztrn/giredore/cmd/giredored && go build -tags netgo -ldflags '-w -extldflags "-static"' && cd ../giredorectl && go build -tags netgo -ldflags '-w -extldflags "-static"'
|
||||
|
||||
FROM alpine:latest
|
||||
FROM code.pztrn.name/containers/mirror/alpine:3.16.0
|
||||
LABEL maintainer "Stanislav N. <pztrn@pztrn.name>"
|
||||
|
||||
COPY --from=build /go/src/sources.dev.pztrn.name/pztrn/giredore/cmd/giredored/giredored /usr/local/bin/giredored
|
||||
|
@@ -1,12 +1,10 @@
|
||||
# Giredore
|
||||
|
||||
[](https://github-ci.pztrn.name/pztrn/giredore) 
|
||||
|
||||
**Giredore** is an utility that does go-import redirects right. Main idea of this project is to provide easy-to-use yet powerful tool for Go developers to serve they packages right and help them with getting nice and very static URLs for their packages.
|
||||
|
||||
## What it able to do
|
||||
|
||||
* Be a HTTP server. *If you want HTTPS (and you normally should want it) - put giredore behind proxy or submit merge request that implements this feature)*
|
||||
* Provide simple CLI client for controlling things.
|
||||
|
||||
## Documentation
|
||||
|
||||
Documentation is available [here](https://docs.dev.pztrn.name/s/bmdesotu0rrje1og7s5g/giredore).
|
@@ -1,20 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"os"
|
||||
|
||||
// local
|
||||
clientv1 "sources.dev.pztrn.name/pztrn/giredore/domains/client/v1"
|
||||
"sources.dev.pztrn.name/pztrn/giredore/internal/logger"
|
||||
|
||||
// other
|
||||
"github.com/teris-io/cli"
|
||||
clientv1 "go.dev.pztrn.name/giredore/domains/client/v1"
|
||||
"go.dev.pztrn.name/giredore/internal/logger"
|
||||
)
|
||||
|
||||
func main() {
|
||||
config := cli.NewCommand("configuration", "work with giredore server configuration").
|
||||
WithShortcut("conf").
|
||||
config := cli.NewCommand("serverconfig", "work with giredore server configuration").
|
||||
WithShortcut("serverconf").
|
||||
WithCommand(
|
||||
cli.NewCommand("get", "gets and prints out current giredore server configuration").
|
||||
WithAction(func(args []string, options map[string]string) int {
|
||||
@@ -34,6 +30,7 @@ func main() {
|
||||
logger.Initialize()
|
||||
clientv1.Initialize()
|
||||
clientv1.SetAllowedIPs(args, options)
|
||||
|
||||
return 0
|
||||
}),
|
||||
),
|
||||
|
@@ -1,16 +1,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
// local
|
||||
serverv1 "sources.dev.pztrn.name/pztrn/giredore/domains/server/v1"
|
||||
"sources.dev.pztrn.name/pztrn/giredore/internal/configuration"
|
||||
"sources.dev.pztrn.name/pztrn/giredore/internal/httpserver"
|
||||
"sources.dev.pztrn.name/pztrn/giredore/internal/logger"
|
||||
serverv1 "go.dev.pztrn.name/giredore/domains/server/v1"
|
||||
"go.dev.pztrn.name/giredore/internal/configuration"
|
||||
"go.dev.pztrn.name/giredore/internal/httpserver"
|
||||
"go.dev.pztrn.name/giredore/internal/logger"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -29,7 +27,9 @@ func main() {
|
||||
// CTRL+C handler.
|
||||
signalHandler := make(chan os.Signal, 1)
|
||||
shutdownDone := make(chan bool, 1)
|
||||
|
||||
signal.Notify(signalHandler, os.Interrupt, syscall.SIGTERM)
|
||||
|
||||
go func() {
|
||||
<-signalHandler
|
||||
httpserver.Shutdown()
|
||||
|
@@ -1,16 +1,15 @@
|
||||
package clientv1
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"strings"
|
||||
|
||||
// local
|
||||
"sources.dev.pztrn.name/pztrn/giredore/internal/requester"
|
||||
"sources.dev.pztrn.name/pztrn/giredore/internal/structs"
|
||||
"go.dev.pztrn.name/giredore/internal/requester"
|
||||
"go.dev.pztrn.name/giredore/internal/structs"
|
||||
)
|
||||
|
||||
func GetConfiguration(options map[string]string) {
|
||||
url := "http://" + options["server"] + "/_api/configuration"
|
||||
|
||||
log.Info().Msg("Getting configuration from giredore server...")
|
||||
|
||||
data, err := requester.Get(url)
|
||||
@@ -23,6 +22,7 @@ func GetConfiguration(options map[string]string) {
|
||||
|
||||
func SetAllowedIPs(args []string, options map[string]string) {
|
||||
url := "http://" + options["server"] + "/_api/configuration/allowedips"
|
||||
|
||||
log.Info().Str("allowed IPs", args[0]).Msg("Setting allowed IPs for API interaction...")
|
||||
|
||||
req := &structs.AllowedIPsSetRequest{
|
||||
|
@@ -1,17 +1,12 @@
|
||||
package clientv1
|
||||
|
||||
import (
|
||||
// local
|
||||
"sources.dev.pztrn.name/pztrn/giredore/internal/logger"
|
||||
"sources.dev.pztrn.name/pztrn/giredore/internal/requester"
|
||||
|
||||
// other
|
||||
"github.com/rs/zerolog"
|
||||
"go.dev.pztrn.name/giredore/internal/logger"
|
||||
"go.dev.pztrn.name/giredore/internal/requester"
|
||||
)
|
||||
|
||||
var (
|
||||
log zerolog.Logger
|
||||
)
|
||||
var log zerolog.Logger
|
||||
|
||||
func Initialize() {
|
||||
log = logger.Logger.With().Str("type", "domain").Str("package", "client").Int("version", 1).Logger()
|
||||
|
@@ -1,12 +1,10 @@
|
||||
package clientv1
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"strings"
|
||||
|
||||
// local
|
||||
"sources.dev.pztrn.name/pztrn/giredore/internal/requester"
|
||||
"sources.dev.pztrn.name/pztrn/giredore/internal/structs"
|
||||
"go.dev.pztrn.name/giredore/internal/requester"
|
||||
"go.dev.pztrn.name/giredore/internal/structs"
|
||||
)
|
||||
|
||||
func DeletePackage(args []string, options map[string]string) {
|
||||
@@ -17,6 +15,7 @@ func DeletePackage(args []string, options map[string]string) {
|
||||
log.Info().Str("original path", req.OriginalPath).Msg("Sending package deletion request to giredored...")
|
||||
|
||||
url := "http://" + options["server"] + "/_api/packages"
|
||||
|
||||
data, err := requester.Delete(url, req)
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Failed to send package deletion request to giredored")
|
||||
@@ -28,6 +27,7 @@ func DeletePackage(args []string, options map[string]string) {
|
||||
func GetPackages(args []string, options map[string]string) {
|
||||
pkgs := strings.Split(args[0], ",")
|
||||
|
||||
// nolint:exhaustruct
|
||||
req := &structs.PackageGetRequest{}
|
||||
if pkgs[0] == "all" {
|
||||
req.All = true
|
||||
@@ -36,6 +36,7 @@ func GetPackages(args []string, options map[string]string) {
|
||||
}
|
||||
|
||||
url := "http://" + options["server"] + "/_api/packages"
|
||||
|
||||
log.Info().Msg("Getting packages data from giredore server...")
|
||||
|
||||
data, err := requester.Post(url, req)
|
||||
@@ -54,9 +55,16 @@ func SetPackage(args []string, options map[string]string) {
|
||||
VCS: args[3],
|
||||
}
|
||||
|
||||
// Execute some necessary checks.
|
||||
// If package's original path isn't starting with "/" - add it.
|
||||
if !strings.HasPrefix(pkg.OriginalPath, "/") {
|
||||
pkg.OriginalPath = "/" + pkg.OriginalPath
|
||||
}
|
||||
|
||||
log.Info().Str("description", pkg.Description).Str("original path", pkg.OriginalPath).Str("real path", pkg.RealPath).Str("VCS", pkg.VCS).Msg("Sending set/update request to giredored...")
|
||||
|
||||
url := "http://" + options["server"] + "/_api/packages"
|
||||
|
||||
data, err := requester.Put(url, pkg)
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Failed to send package update/set request to giredored")
|
||||
|
@@ -1,32 +1,32 @@
|
||||
package serverv1
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"net/http"
|
||||
|
||||
// local
|
||||
"sources.dev.pztrn.name/pztrn/giredore/internal/configuration"
|
||||
"sources.dev.pztrn.name/pztrn/giredore/internal/structs"
|
||||
|
||||
// other
|
||||
"github.com/labstack/echo"
|
||||
"go.dev.pztrn.name/giredore/internal/configuration"
|
||||
"go.dev.pztrn.name/giredore/internal/structs"
|
||||
)
|
||||
|
||||
// This function responsible for getting runtime configuration.
|
||||
func configurationGET(ec echo.Context) error {
|
||||
return ec.JSON(http.StatusOK, map[string]string{"result": "success"})
|
||||
// nolint:wrapcheck
|
||||
return ec.JSON(http.StatusOK, configuration.Cfg)
|
||||
}
|
||||
|
||||
func configurationAllowedIPsSET(ec echo.Context) error {
|
||||
func configurationAllowedIPsSET(ectx echo.Context) error {
|
||||
// nolint:exhaustruct
|
||||
req := &structs.AllowedIPsSetRequest{}
|
||||
if err := ec.Bind(req); err != nil {
|
||||
if err := ectx.Bind(req); err != nil {
|
||||
log.Error().Err(err).Msg("Failed to parse allowed IPs set request")
|
||||
return ec.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrParsingAllowedIPsSetRequest}})
|
||||
// nolint:exhaustruct,wrapcheck
|
||||
return ectx.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrParsingAllowedIPsSetRequest}})
|
||||
}
|
||||
|
||||
log.Debug().Msgf("Got set allowed IPs request: %+v", req)
|
||||
|
||||
configuration.Cfg.SetAllowedIPs(req.AllowedIPs)
|
||||
|
||||
return ec.JSON(http.StatusOK, &structs.Reply{Status: structs.StatusSuccess})
|
||||
// nolint:exhaustruct,wrapcheck
|
||||
return ectx.JSON(http.StatusOK, &structs.Reply{Status: structs.StatusSuccess})
|
||||
}
|
||||
|
@@ -1,17 +1,12 @@
|
||||
package serverv1
|
||||
|
||||
import (
|
||||
// local
|
||||
"sources.dev.pztrn.name/pztrn/giredore/internal/httpserver"
|
||||
"sources.dev.pztrn.name/pztrn/giredore/internal/logger"
|
||||
|
||||
// other
|
||||
"github.com/rs/zerolog"
|
||||
"go.dev.pztrn.name/giredore/internal/httpserver"
|
||||
"go.dev.pztrn.name/giredore/internal/logger"
|
||||
)
|
||||
|
||||
var (
|
||||
log zerolog.Logger
|
||||
)
|
||||
var log zerolog.Logger
|
||||
|
||||
func Initialize() {
|
||||
log = logger.Logger.With().Str("type", "domain").Str("package", "server").Int("version", 1).Logger()
|
||||
|
@@ -1,41 +1,43 @@
|
||||
package serverv1
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
// local
|
||||
"sources.dev.pztrn.name/pztrn/giredore/internal/configuration"
|
||||
"sources.dev.pztrn.name/pztrn/giredore/internal/structs"
|
||||
|
||||
// other
|
||||
"github.com/labstack/echo"
|
||||
"go.dev.pztrn.name/giredore/internal/configuration"
|
||||
"go.dev.pztrn.name/giredore/internal/structs"
|
||||
)
|
||||
|
||||
func throwGoImports(ec echo.Context) error {
|
||||
func throwGoImports(ectx echo.Context) error {
|
||||
// Getting real path. This might be the package itself, or namespace
|
||||
// to list available packages.
|
||||
// For now only package itself is supported, all other features in ToDo.
|
||||
packageNameRaw := ec.Request().URL.Path
|
||||
packageNameRaw := ectx.Request().URL.Path
|
||||
|
||||
pkgs, errs := configuration.Cfg.GetPackagesInfo([]string{packageNameRaw})
|
||||
|
||||
if errs != nil {
|
||||
log.Error().Str("package", packageNameRaw).Msgf("Failed to get package information: %+v", errs)
|
||||
return ec.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: errs})
|
||||
|
||||
// nolint:exhaustruct,wrapcheck
|
||||
return ectx.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: errs})
|
||||
}
|
||||
|
||||
if len(pkgs) == 0 {
|
||||
return ec.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrNoPackagesFound}})
|
||||
// nolint:exhaustruct,wrapcheck
|
||||
return ectx.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrNoPackagesFound}})
|
||||
}
|
||||
|
||||
pkg, found := pkgs[packageNameRaw]
|
||||
if !found {
|
||||
return ec.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrNoPackagesFound}})
|
||||
// nolint:exhaustruct,wrapcheck
|
||||
return ectx.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrNoPackagesFound}})
|
||||
}
|
||||
|
||||
// We should compose package name using our domain under which giredore
|
||||
// is working.
|
||||
domain := ec.Request().Host
|
||||
domain := ectx.Request().Host
|
||||
packageName := domain + packageNameRaw
|
||||
|
||||
tmpl := singlePackageTemplate
|
||||
@@ -43,5 +45,6 @@ func throwGoImports(ec echo.Context) error {
|
||||
tmpl = strings.Replace(tmpl, "{VCS}", pkg.VCS, 1)
|
||||
tmpl = strings.Replace(tmpl, "{REPOPATH}", pkg.RealPath, 1)
|
||||
|
||||
return ec.HTML(http.StatusOK, tmpl)
|
||||
// nolint:wrapcheck
|
||||
return ectx.HTML(http.StatusOK, tmpl)
|
||||
}
|
||||
|
@@ -1,30 +1,31 @@
|
||||
package serverv1
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
// local
|
||||
"sources.dev.pztrn.name/pztrn/giredore/internal/configuration"
|
||||
"sources.dev.pztrn.name/pztrn/giredore/internal/structs"
|
||||
|
||||
// other
|
||||
"github.com/labstack/echo"
|
||||
"go.dev.pztrn.name/giredore/internal/configuration"
|
||||
"go.dev.pztrn.name/giredore/internal/structs"
|
||||
)
|
||||
|
||||
// This function responsible for getting packages configuration.
|
||||
func packagesGET(ec echo.Context) error {
|
||||
func packagesGET(ectx echo.Context) error {
|
||||
// nolint:exhaustruct
|
||||
req := &structs.PackageGetRequest{}
|
||||
if err := ec.Bind(req); err != nil {
|
||||
if err := ectx.Bind(req); err != nil {
|
||||
log.Error().Err(err).Msg("Failed to parse package get request")
|
||||
return ec.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrParsingPackagesGetRequest}})
|
||||
|
||||
// nolint:exhaustruct,wrapcheck
|
||||
return ectx.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrParsingPackagesGetRequest}})
|
||||
}
|
||||
|
||||
log.Info().Msgf("Received package(s) info get request: %+v", req)
|
||||
|
||||
var pkgs map[string]*structs.Package
|
||||
|
||||
var errors []structs.Error
|
||||
|
||||
if req.All {
|
||||
pkgs = configuration.Cfg.GetAllPackagesInfo()
|
||||
} else {
|
||||
@@ -32,18 +33,23 @@ func packagesGET(ec echo.Context) error {
|
||||
}
|
||||
|
||||
if len(errors) > 0 {
|
||||
return ec.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: errors, Data: pkgs})
|
||||
// nolint:wrapcheck
|
||||
return ectx.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: errors, Data: pkgs})
|
||||
}
|
||||
|
||||
return ec.JSON(http.StatusOK, &structs.Reply{Status: structs.StatusSuccess, Data: pkgs})
|
||||
// nolint:exhaustruct,wrapcheck
|
||||
return ectx.JSON(http.StatusOK, &structs.Reply{Status: structs.StatusSuccess, Data: pkgs})
|
||||
}
|
||||
|
||||
// This function responsible for deleting package.
|
||||
func packagesDELETE(ec echo.Context) error {
|
||||
func packagesDELETE(ectx echo.Context) error {
|
||||
// nolint:exhaustruct
|
||||
req := &structs.PackageDeleteRequest{}
|
||||
if err := ec.Bind(req); err != nil {
|
||||
if err := ectx.Bind(req); err != nil {
|
||||
log.Error().Err(err).Msg("Failed to parse package delete request")
|
||||
return ec.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrParsingDeleteRequest}})
|
||||
|
||||
// nolint:exhaustruct,wrapcheck
|
||||
return ectx.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrParsingDeleteRequest}})
|
||||
}
|
||||
|
||||
log.Info().Msgf("Received package delete request: %+v", req)
|
||||
@@ -51,28 +57,35 @@ func packagesDELETE(ec echo.Context) error {
|
||||
errs := configuration.Cfg.DeletePackage(req)
|
||||
|
||||
if len(errs) > 0 {
|
||||
return ec.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: errs})
|
||||
// nolint:exhaustruct,wrapcheck
|
||||
return ectx.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: errs})
|
||||
}
|
||||
|
||||
return ec.JSON(http.StatusOK, &structs.Reply{Status: structs.StatusSuccess})
|
||||
// nolint:exhaustruct,wrapcheck
|
||||
return ectx.JSON(http.StatusOK, &structs.Reply{Status: structs.StatusSuccess})
|
||||
}
|
||||
|
||||
// This function responsible for setting or updating packages.
|
||||
func packagesSET(ec echo.Context) error {
|
||||
func packagesSET(ectx echo.Context) error {
|
||||
// nolint:exhaustruct
|
||||
req := &structs.Package{}
|
||||
if err := ec.Bind(req); err != nil {
|
||||
if err := ectx.Bind(req); err != nil {
|
||||
log.Error().Err(err).Msg("Failed to parse package data")
|
||||
return ec.JSON(http.StatusBadRequest, nil)
|
||||
|
||||
// nolint:wrapcheck
|
||||
return ectx.JSON(http.StatusBadRequest, nil)
|
||||
}
|
||||
|
||||
log.Info().Msgf("Received package set/update request: %+v", req)
|
||||
|
||||
// Validate passed package data.
|
||||
if !strings.HasPrefix(req.OriginalPath, "/") {
|
||||
return ec.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrPackageOrigPathShouldStartWithSlash}})
|
||||
// nolint:exhaustruct,wrapcheck
|
||||
return ectx.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrPackageOrigPathShouldStartWithSlash}})
|
||||
}
|
||||
|
||||
configuration.Cfg.AddOrUpdatePackage(req)
|
||||
|
||||
return ec.JSON(http.StatusOK, &structs.Reply{Status: structs.StatusSuccess})
|
||||
// nolint:exhaustruct,wrapcheck
|
||||
return ectx.JSON(http.StatusOK, &structs.Reply{Status: structs.StatusSuccess})
|
||||
}
|
||||
|
2
go.mod
2
go.mod
@@ -1,4 +1,4 @@
|
||||
module sources.dev.pztrn.name/pztrn/giredore
|
||||
module go.dev.pztrn.name/giredore
|
||||
|
||||
go 1.13
|
||||
|
||||
|
@@ -1,7 +1,6 @@
|
||||
package configuration
|
||||
|
||||
import (
|
||||
// other
|
||||
"github.com/vrischmann/envconfig"
|
||||
)
|
||||
|
||||
|
@@ -1,11 +1,8 @@
|
||||
package configuration
|
||||
|
||||
import (
|
||||
// local
|
||||
"sources.dev.pztrn.name/pztrn/giredore/internal/logger"
|
||||
|
||||
// other
|
||||
"github.com/rs/zerolog"
|
||||
"go.dev.pztrn.name/giredore/internal/logger"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -19,9 +16,11 @@ func Initialize() {
|
||||
log = logger.Logger.With().Str("type", "internal").Str("package", "configuration").Logger()
|
||||
log.Info().Msg("Initializing...")
|
||||
|
||||
// nolint:exhaustruct
|
||||
envCfg = &envConfig{}
|
||||
envCfg.Initialize()
|
||||
|
||||
// nolint:exhaustruct
|
||||
Cfg = &fileConfig{}
|
||||
Cfg.Initialize()
|
||||
|
||||
|
@@ -1,7 +1,6 @@
|
||||
package configuration
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
@@ -9,8 +8,7 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
// local
|
||||
"sources.dev.pztrn.name/pztrn/giredore/internal/structs"
|
||||
"go.dev.pztrn.name/giredore/internal/structs"
|
||||
)
|
||||
|
||||
// This structure represents configuration that will be parsed via file.
|
||||
@@ -19,22 +17,22 @@ import (
|
||||
// may be accesses concurrently. In other words DO NOT USE EXPORTED FIELDS
|
||||
// DIRECTLY!
|
||||
type fileConfig struct {
|
||||
packagesMutex sync.RWMutex
|
||||
// Packages describes packages mapping.
|
||||
Packages map[string]*structs.Package
|
||||
// HTTP describes HTTP server configuration.
|
||||
HTTP struct {
|
||||
// AllowedIPs is a list of IPs that allowed to access API.
|
||||
// There might be other authentication implemented in future.
|
||||
AllowedIPs []string
|
||||
allowedipsmutex sync.RWMutex
|
||||
// Listen is an address on which HTTP server will listen.
|
||||
Listen string
|
||||
// AllowedIPs is a list of IPs that allowed to access API.
|
||||
// There might be other authentication implemented in future.
|
||||
AllowedIPs []string
|
||||
// WaitForSeconds is a timeout during which we will wait for
|
||||
// HTTP server be up. If timeout will pass and HTTP server won't
|
||||
// start processing requests - giredore will exit.
|
||||
WaitForSeconds int
|
||||
}
|
||||
// Packages describes packages mapping.
|
||||
Packages map[string]*structs.Package
|
||||
packagesMutex sync.RWMutex
|
||||
}
|
||||
|
||||
func (fc *fileConfig) AddOrUpdatePackage(pkg *structs.Package) {
|
||||
@@ -52,6 +50,7 @@ func (fc *fileConfig) DeletePackage(req *structs.PackageDeleteRequest) []structs
|
||||
|
||||
if !found {
|
||||
errors = append(errors, structs.ErrPackageWasntDefined)
|
||||
|
||||
return errors
|
||||
}
|
||||
|
||||
@@ -62,6 +61,7 @@ func (fc *fileConfig) DeletePackage(req *structs.PackageDeleteRequest) []structs
|
||||
|
||||
func (fc *fileConfig) GetAllowedIPs() []string {
|
||||
var allowedIPs []string
|
||||
|
||||
fc.HTTP.allowedipsmutex.RLock()
|
||||
allowedIPs = append(allowedIPs, fc.HTTP.AllowedIPs...)
|
||||
fc.HTTP.allowedipsmutex.RUnlock()
|
||||
@@ -83,6 +83,7 @@ func (fc *fileConfig) GetAllPackagesInfo() map[string]*structs.Package {
|
||||
|
||||
func (fc *fileConfig) GetPackagesInfo(packages []string) (map[string]*structs.Package, []structs.Error) {
|
||||
pkgs := make(map[string]*structs.Package)
|
||||
|
||||
var errors []structs.Error
|
||||
|
||||
fc.packagesMutex.Lock()
|
||||
@@ -114,6 +115,7 @@ func (fc *fileConfig) Initialize() {
|
||||
// exists.
|
||||
if _, err2 := os.Stat(configPath); os.IsNotExist(err2) {
|
||||
cfgLoadLog.Error().Msg("Unable to load configuration from filesystem.")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -135,15 +137,18 @@ func (fc *fileConfig) Initialize() {
|
||||
|
||||
// Ensure that localhost (127.0.0.1) are defined in AllowedIPs.
|
||||
var localhostIsAllowed bool
|
||||
|
||||
for _, ip := range fc.HTTP.AllowedIPs {
|
||||
if strings.Contains(ip, "127.0.0.1") {
|
||||
localhostIsAllowed = true
|
||||
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !localhostIsAllowed {
|
||||
cfgLoadLog.Warn().Msg("Localhost (127.0.0.1) wasn't allowed to access configuration API, adding it to list of allowed IP addresses")
|
||||
|
||||
fc.HTTP.AllowedIPs = append(fc.HTTP.AllowedIPs, "127.0.0.1")
|
||||
} else {
|
||||
cfgLoadLog.Debug().Msg("Localhost (127.0.0.1) is allowed to access configuration API")
|
||||
@@ -159,13 +164,16 @@ func (fc *fileConfig) normalizePath(configPath string) (string, error) {
|
||||
if strings.Contains(configPath, "~") {
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
// nolint:wrapcheck
|
||||
return "", err
|
||||
}
|
||||
|
||||
configPath = strings.Replace(configPath, "~", homeDir, 1)
|
||||
}
|
||||
|
||||
absPath, err1 := filepath.Abs(configPath)
|
||||
if err1 != nil {
|
||||
// nolint:wrapcheck
|
||||
return "", err1
|
||||
}
|
||||
|
||||
@@ -181,6 +189,7 @@ func (fc *fileConfig) Save() {
|
||||
data, err := json.Marshal(fc)
|
||||
if err != nil {
|
||||
cfgSaveLog.Fatal().Err(err).Msg("Failed to encode data into JSON. Configuration file won't be saved!")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
@@ -1,33 +1,30 @@
|
||||
package httpserver
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
// local
|
||||
"sources.dev.pztrn.name/pztrn/giredore/internal/configuration"
|
||||
"sources.dev.pztrn.name/pztrn/giredore/internal/structs"
|
||||
|
||||
// other
|
||||
"github.com/labstack/echo"
|
||||
"go.dev.pztrn.name/giredore/internal/configuration"
|
||||
"go.dev.pztrn.name/giredore/internal/structs"
|
||||
)
|
||||
|
||||
func checkAllowedIPs() echo.MiddlewareFunc {
|
||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(ec echo.Context) error {
|
||||
return func(ectx echo.Context) error {
|
||||
// Do nothing if request came not in "/_api" namespace.
|
||||
if !strings.HasPrefix(ec.Request().RequestURI, "/_api") {
|
||||
_ = next(ec)
|
||||
return nil
|
||||
if !strings.HasPrefix(ectx.Request().RequestURI, "/_api") {
|
||||
return next(ectx)
|
||||
}
|
||||
|
||||
// Get IPs and subnets from configuration and parse them
|
||||
// into comparable things.
|
||||
// If IP address was specified without network mask - assume /32.
|
||||
var subnets []*net.IPNet
|
||||
|
||||
allowedIPs := configuration.Cfg.GetAllowedIPs()
|
||||
|
||||
for _, ip := range allowedIPs {
|
||||
ipToParse := ip
|
||||
if !strings.Contains(ip, "/") {
|
||||
@@ -37,7 +34,9 @@ func checkAllowedIPs() echo.MiddlewareFunc {
|
||||
_, net, err := net.ParseCIDR(ipToParse)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("subnet", ipToParse).Msg("Failed to parse CIDR. /_api/ endpoint won't be accessible, this should be fixed manually in configuration file!")
|
||||
return ec.JSON(http.StatusInternalServerError, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrInvalidAllowedIPDefined}})
|
||||
|
||||
// nolint:exhaustruct,wrapcheck
|
||||
return ectx.JSON(http.StatusInternalServerError, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrInvalidAllowedIPDefined}})
|
||||
}
|
||||
|
||||
subnets = append(subnets, net)
|
||||
@@ -45,21 +44,24 @@ func checkAllowedIPs() echo.MiddlewareFunc {
|
||||
|
||||
// Check if requester's IP address are within allowed IP
|
||||
// subnets.
|
||||
ipToCheck := net.ParseIP(ec.RealIP())
|
||||
ipToCheck := net.ParseIP(ectx.RealIP())
|
||||
|
||||
var allowed bool
|
||||
|
||||
for _, subnet := range subnets {
|
||||
if subnet.Contains(ipToCheck) {
|
||||
allowed = true
|
||||
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if allowed {
|
||||
_ = next(ec)
|
||||
return nil
|
||||
return next(ectx)
|
||||
}
|
||||
|
||||
return ec.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrIPAddressNotAllowed}})
|
||||
// nolint:exhaustruct,wrapcheck
|
||||
return ectx.JSON(http.StatusBadRequest, &structs.Reply{Status: structs.StatusFailure, Errors: []structs.Error{structs.ErrIPAddressNotAllowed}})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,21 +1,17 @@
|
||||
package httpserver
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"context"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
// local
|
||||
"sources.dev.pztrn.name/pztrn/giredore/internal/configuration"
|
||||
"sources.dev.pztrn.name/pztrn/giredore/internal/logger"
|
||||
|
||||
// other
|
||||
"github.com/labstack/echo"
|
||||
"github.com/labstack/echo/middleware"
|
||||
"github.com/rs/zerolog"
|
||||
"go.dev.pztrn.name/giredore/internal/configuration"
|
||||
"go.dev.pztrn.name/giredore/internal/logger"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -44,10 +40,12 @@ func Initialize() {
|
||||
// Shutdown stops HTTP server. Returns true on success and false on failure.
|
||||
func Shutdown() {
|
||||
log.Info().Msg("Shutting down HTTP server...")
|
||||
|
||||
err := Srv.Shutdown(context.Background())
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("Failed to stop HTTP server")
|
||||
}
|
||||
|
||||
log.Info().Msg("HTTP server shutted down")
|
||||
}
|
||||
|
||||
@@ -59,50 +57,75 @@ func Start() {
|
||||
go func() {
|
||||
err := Srv.Start(configuration.Cfg.HTTP.Listen)
|
||||
if !strings.Contains(err.Error(), "Server closed") {
|
||||
log.Fatal().Err(err).Msg("HTTP server critial error occurred")
|
||||
log.Fatal().Err(err).Msg("HTTP server critical error occurred")
|
||||
}
|
||||
}()
|
||||
|
||||
// Check that HTTP server was started.
|
||||
// nolint:exhaustruct
|
||||
httpc := &http.Client{Timeout: time.Second * 1}
|
||||
checks := 0
|
||||
|
||||
for {
|
||||
checks++
|
||||
|
||||
if checks >= configuration.Cfg.HTTP.WaitForSeconds {
|
||||
log.Fatal().Int("seconds passed", checks).Msg("HTTP server isn't up")
|
||||
}
|
||||
|
||||
time.Sleep(time.Second * 1)
|
||||
resp, err := httpc.Get("http://" + configuration.Cfg.HTTP.Listen + "/_internal/waitForOnline")
|
||||
|
||||
localCtx, cancelFunc := context.WithTimeout(context.Background(), time.Second*1)
|
||||
|
||||
req, err := http.NewRequestWithContext(localCtx, "GET", "http://"+configuration.Cfg.HTTP.Listen+"/_internal/waitForOnline", nil)
|
||||
if err != nil {
|
||||
log.Panic().Err(err).Msg("Failed to create HTTP request!")
|
||||
}
|
||||
|
||||
resp, err := httpc.Do(req)
|
||||
if err != nil {
|
||||
log.Debug().Err(err).Msg("HTTP error occurred, HTTP server isn't ready, waiting...")
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
response, err := ioutil.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
|
||||
if err != nil {
|
||||
log.Debug().Err(err).Msg("Failed to read response body, HTTP server isn't ready, waiting...")
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
log.Debug().Str("status", resp.Status).Int("body length", len(response)).Msg("HTTP response received")
|
||||
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
if len(response) == 0 {
|
||||
log.Debug().Msg("Response is empty, HTTP server isn't ready, waiting...")
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
log.Debug().Int("status code", resp.StatusCode).Msgf("Response: %+v", string(response))
|
||||
|
||||
if len(response) == 17 {
|
||||
// This is useless context cancel function call. Thanks to lostcancel linter.
|
||||
cancelFunc()
|
||||
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log.Info().Msg("HTTP server is ready to process requests")
|
||||
}
|
||||
|
||||
func waitForHTTPServerToBeUpHandler(ec echo.Context) error {
|
||||
func waitForHTTPServerToBeUpHandler(ectx echo.Context) error {
|
||||
response := map[string]string{
|
||||
"error": "None",
|
||||
}
|
||||
return ec.JSON(200, response)
|
||||
|
||||
// nolint:wrapcheck
|
||||
return ectx.JSON(200, response)
|
||||
}
|
||||
|
@@ -1,24 +1,29 @@
|
||||
package httpserver
|
||||
|
||||
import (
|
||||
// other
|
||||
"time"
|
||||
|
||||
"github.com/labstack/echo"
|
||||
)
|
||||
|
||||
func requestLogger() echo.MiddlewareFunc {
|
||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(ec echo.Context) error {
|
||||
return func(ectx echo.Context) error {
|
||||
startTime := time.Now()
|
||||
|
||||
err := next(ectx)
|
||||
|
||||
log.Info().
|
||||
Str("From", ec.RealIP()).
|
||||
Str("To", ec.Request().Host).
|
||||
Str("Method", ec.Request().Method).
|
||||
Str("Path", ec.Request().URL.Path).
|
||||
Int64("Length", ec.Request().ContentLength).
|
||||
Str("UA", ec.Request().UserAgent()).
|
||||
Str("From", ectx.RealIP()).
|
||||
Str("To", ectx.Request().Host).
|
||||
Str("Method", ectx.Request().Method).
|
||||
Str("Path", ectx.Request().URL.Path).
|
||||
Int64("Length", ectx.Request().ContentLength).
|
||||
Str("UA", ectx.Request().UserAgent()).
|
||||
TimeDiff("TimeMS", time.Now(), startTime).
|
||||
Msg("HTTP request")
|
||||
|
||||
_ = next(ec)
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,12 +1,10 @@
|
||||
package httpserver
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
// other
|
||||
"github.com/labstack/echo"
|
||||
)
|
||||
|
||||
@@ -15,7 +13,7 @@ import (
|
||||
type StrictJSONBinder struct{}
|
||||
|
||||
// Bind parses JSON input.
|
||||
func (sjb *StrictJSONBinder) Bind(i interface{}, c echo.Context) error {
|
||||
func (sjb *StrictJSONBinder) Bind(data interface{}, c echo.Context) error {
|
||||
req := c.Request()
|
||||
if req.ContentLength == 0 {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, "Request body can't be empty")
|
||||
@@ -24,7 +22,10 @@ func (sjb *StrictJSONBinder) Bind(i interface{}, c echo.Context) error {
|
||||
// Decode it.
|
||||
decoder := json.NewDecoder(req.Body)
|
||||
decoder.DisallowUnknownFields()
|
||||
if err := decoder.Decode(i); err != nil {
|
||||
|
||||
// ToDo: rework this code.
|
||||
// nolint:errorlint
|
||||
if err := decoder.Decode(data); err != nil {
|
||||
if ute, ok := err.(*json.UnmarshalTypeError); ok {
|
||||
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Unmarshal type error: expected=%v, got=%v, field=%v, offset=%v", ute.Type, ute.Value, ute.Field, ute.Offset))
|
||||
} else if se, ok := err.(*json.SyntaxError); ok {
|
||||
|
@@ -1,13 +1,11 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
// other
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
@@ -17,12 +15,14 @@ var (
|
||||
)
|
||||
|
||||
// Initialize initializes zerolog with proper formatting and log level.
|
||||
// nolint:forbidigo
|
||||
func Initialize() {
|
||||
// Check environment for logger level.
|
||||
// Defaulting to INFO.
|
||||
loggerLevel, loggerLevelFound := os.LookupEnv("LOGGER_LEVEL")
|
||||
if loggerLevelFound {
|
||||
fmt.Println("Setting logger level to:", loggerLevel)
|
||||
|
||||
switch strings.ToUpper(loggerLevel) {
|
||||
case "DEBUG":
|
||||
zerolog.SetGlobalLevel(zerolog.DebugLevel)
|
||||
@@ -36,7 +36,7 @@ func Initialize() {
|
||||
zerolog.SetGlobalLevel(zerolog.FatalLevel)
|
||||
default:
|
||||
fmt.Println("Invalid logger level passed:", loggerLevel)
|
||||
fmt.Println("Fofcing INFO")
|
||||
fmt.Println("Forcing INFO")
|
||||
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
||||
}
|
||||
} else {
|
||||
@@ -44,29 +44,32 @@ func Initialize() {
|
||||
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
||||
}
|
||||
|
||||
// nolint:exhaustruct
|
||||
output := zerolog.ConsoleWriter{Out: os.Stdout, NoColor: false, TimeFormat: time.RFC3339}
|
||||
output.FormatLevel = func(i interface{}) string {
|
||||
var v string
|
||||
if ii, ok := i.(string); ok {
|
||||
ii = strings.ToUpper(ii)
|
||||
switch ii {
|
||||
output.FormatLevel = func(lvlRaw interface{}) string {
|
||||
var formattedLvl string
|
||||
|
||||
if lvl, ok := lvlRaw.(string); ok {
|
||||
lvl = strings.ToUpper(lvl)
|
||||
switch lvl {
|
||||
case "DEBUG":
|
||||
v = fmt.Sprintf("\x1b[30m%-5s\x1b[0m", ii)
|
||||
formattedLvl = fmt.Sprintf("\x1b[30m%-5s\x1b[0m", lvl)
|
||||
case "ERROR":
|
||||
v = fmt.Sprintf("\x1b[31m%-5s\x1b[0m", ii)
|
||||
formattedLvl = fmt.Sprintf("\x1b[31m%-5s\x1b[0m", lvl)
|
||||
case "FATAL":
|
||||
v = fmt.Sprintf("\x1b[35m%-5s\x1b[0m", ii)
|
||||
formattedLvl = fmt.Sprintf("\x1b[35m%-5s\x1b[0m", lvl)
|
||||
case "INFO":
|
||||
v = fmt.Sprintf("\x1b[32m%-5s\x1b[0m", ii)
|
||||
formattedLvl = fmt.Sprintf("\x1b[32m%-5s\x1b[0m", lvl)
|
||||
case "PANIC":
|
||||
v = fmt.Sprintf("\x1b[36m%-5s\x1b[0m", ii)
|
||||
formattedLvl = fmt.Sprintf("\x1b[36m%-5s\x1b[0m", lvl)
|
||||
case "WARN":
|
||||
v = fmt.Sprintf("\x1b[33m%-5s\x1b[0m", ii)
|
||||
formattedLvl = fmt.Sprintf("\x1b[33m%-5s\x1b[0m", lvl)
|
||||
default:
|
||||
v = ii
|
||||
formattedLvl = lvl
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf("| %s |", v)
|
||||
|
||||
return fmt.Sprintf("| %s |", formattedLvl)
|
||||
}
|
||||
|
||||
Logger = zerolog.New(output).With().Timestamp().Logger()
|
||||
|
@@ -1,22 +1,17 @@
|
||||
package requester
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
// local
|
||||
"sources.dev.pztrn.name/pztrn/giredore/internal/logger"
|
||||
|
||||
// other
|
||||
"github.com/rs/zerolog"
|
||||
"go.dev.pztrn.name/giredore/internal/logger"
|
||||
)
|
||||
|
||||
var (
|
||||
log zerolog.Logger
|
||||
)
|
||||
var log zerolog.Logger
|
||||
|
||||
func Initialize() {
|
||||
log = logger.Logger.With().Str("type", "internal").Str("package", "requester").Logger()
|
||||
@@ -27,6 +22,7 @@ func Delete(url string, data interface{}) ([]byte, error) {
|
||||
return execRequest("DELETE", url, data)
|
||||
}
|
||||
|
||||
// nolint:wrapcheck
|
||||
func execRequest(method string, url string, data interface{}) ([]byte, error) {
|
||||
log.Debug().Str("method", method).Str("URL", url).Msg("Trying to execute HTTP request...")
|
||||
|
||||
@@ -34,11 +30,12 @@ func execRequest(method string, url string, data interface{}) ([]byte, error) {
|
||||
|
||||
var dataToSend []byte
|
||||
if data != nil {
|
||||
// nolint:errchkjson
|
||||
dataToSend, _ = json.Marshal(data)
|
||||
}
|
||||
|
||||
// Compose HTTP request.
|
||||
httpReq, err := http.NewRequest(method, url, bytes.NewReader(dataToSend))
|
||||
httpReq, err := http.NewRequestWithContext(context.Background(), method, url, bytes.NewReader(dataToSend))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -52,6 +49,7 @@ func execRequest(method string, url string, data interface{}) ([]byte, error) {
|
||||
if err2 != nil {
|
||||
return nil, err2
|
||||
}
|
||||
|
||||
response.Body.Close()
|
||||
|
||||
log.Debug().Int("response body length (bytes)", len(bodyBytes)).Msg("Got response")
|
||||
|
@@ -1,14 +1,15 @@
|
||||
package requester
|
||||
|
||||
import (
|
||||
// stdlib
|
||||
"net"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// nolint:exhaustivestruct
|
||||
func getHTTPClient() *http.Client {
|
||||
c := &http.Client{
|
||||
// nolint:exhaustruct
|
||||
client := &http.Client{
|
||||
Transport: &http.Transport{
|
||||
ExpectContinueTimeout: time.Second * 5,
|
||||
DialContext: (&net.Dialer{
|
||||
@@ -19,5 +20,5 @@ func getHTTPClient() *http.Client {
|
||||
},
|
||||
}
|
||||
|
||||
return c
|
||||
return client
|
||||
}
|
||||
|
Reference in New Issue
Block a user