Server and client stubs.

Implemented HTTP server with configuration getting stub.

Implemented CLI client with configuration getting stub.
This commit is contained in:
Stanislav Nikitin 2019-10-05 21:53:22 +05:00
parent 4d79c8da4b
commit 83a8694061
No known key found for this signature in database
GPG Key ID: 106900B32F8192EE
17 changed files with 549 additions and 1 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*DS_Store*

View File

@ -1,6 +1,6 @@
# Giredore
**Giredore** is an utility that does go-import redirects right.
**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

36
cmd/giredorectl/main.go Normal file
View File

@ -0,0 +1,36 @@
package main
import (
// stdlib
"os"
// local
"sources.dev.pztrn.name/pztrn/giredore/domains/client/v1"
"sources.dev.pztrn.name/pztrn/giredore/internal/logger"
// other
"github.com/teris-io/cli"
)
func main() {
config := cli.NewCommand("configuration", "work with giredore server configuration").
WithShortcut("conf").
WithCommand(
cli.NewCommand("get", "gets and prints out current giredore server configuration").
WithAction(func(args []string, options map[string]string) int {
logger.Initialize()
clientv1.Initialize()
clientv1.GetConfiguration(options)
return 0
}),
)
app := cli.New("giredore server controlling utility").
WithOption(
cli.NewOption("server", "giredore server address"),
).
WithCommand(config)
os.Exit(app.Run(os.Args, os.Stdout))
}

41
cmd/giredored/main.go Normal file
View File

@ -0,0 +1,41 @@
package main
import (
// stdlib
"os"
"os/signal"
"syscall"
// local
"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"
)
func main() {
logger.Initialize()
logger.Logger.Info().Msg("Starting giredore server...")
configuration.Initialize()
httpserver.Initialize()
serverv1.Initialize()
httpserver.Start()
logger.Logger.Info().Msg("giredore server is ready")
// 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()
shutdownDone <- true
}()
<-shutdownDone
os.Exit(0)
}

View File

@ -0,0 +1,18 @@
package clientv1
import (
// local
"sources.dev.pztrn.name/pztrn/giredore/internal/requester"
)
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)
if err != nil {
log.Fatal().Err(err).Msg("Failed to get configuration from giredore server!")
}
log.Debug().Msg("Got data: " + string(data))
}

View File

@ -0,0 +1,21 @@
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"
)
var (
log zerolog.Logger
)
func Initialize() {
log = logger.Logger.With().Str("type", "domain").Str("package", "client").Int("version", 1).Logger()
log.Info().Msg("Initializing...")
requester.Initialize()
}

View File

@ -0,0 +1,14 @@
package serverv1
import (
// stdlib
"net/http"
// other
"github.com/labstack/echo"
)
// This function responsible for getting runtime configuration.
func configurationGET(ec echo.Context) error {
return ec.JSON(http.StatusOK, map[string]string{"result": "success"})
}

View File

@ -0,0 +1,21 @@
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"
)
var (
log zerolog.Logger
)
func Initialize() {
log = logger.Logger.With().Str("type", "domain").Str("package", "server").Int("version", 1).Logger()
log.Info().Msg("Initializing...")
httpserver.Srv.GET("/_api/configuration", configurationGET)
}

12
go.mod Normal file
View File

@ -0,0 +1,12 @@
module sources.dev.pztrn.name/pztrn/giredore
go 1.13
require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/labstack/echo v3.3.10+incompatible
github.com/labstack/gommon v0.3.0 // indirect
github.com/rs/zerolog v1.15.0
github.com/teris-io/cli v1.0.1
github.com/vrischmann/envconfig v1.2.0
)

46
go.sum Normal file
View File

@ -0,0 +1,46 @@
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/labstack/echo v3.3.10+incompatible h1:pGRcYk231ExFAyoAjAfD85kQzRJCRI8bbnE7CX5OEgg=
github.com/labstack/echo v3.3.10+incompatible/go.mod h1:0INS7j/VjnFxD4E2wkz67b8cVwCLbBmJyDaka6Cmk1s=
github.com/labstack/gommon v0.3.0 h1:JEeO0bvc78PKdyHxloTKiF8BD5iGrH8T6MSeGvSgob0=
github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k=
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.9 h1:d5US/mDsogSGW37IV293h//ZFaeajb69h+EHFsv2xGg=
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/rs/zerolog v1.15.0 h1:uPRuwkWF4J6fGsJ2R0Gn2jB1EQiav9k3S6CSdygQJXY=
github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/teris-io/cli v1.0.1 h1:J6jnVHC552uqx7zT+Ux0++tIvLmJQULqxVhCid2u/Gk=
github.com/teris-io/cli v1.0.1/go.mod h1:V9nVD5aZ873RU/tQXLSXO8FieVPQhQvuNohsdsKXsGw=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.0.1 h1:tY9CJiPnMXf1ERmG2EyK7gNUd+c6RKGD0IfU8WdUSz8=
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
github.com/vrischmann/envconfig v1.2.0 h1:5/u4fI34/g3m0SdTQj/6f3r640jv9E5+yTXIZOWsxk0=
github.com/vrischmann/envconfig v1.2.0/go.mod h1:c5DuUlkzfsnspy1g7qiqryPCsW+NjsrLsYq4zhwsoHo=
github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a h1:aYOabOQFp6Vj6W1F80affTUvO9UxmJRx8K0gsfABByQ=
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190425163242-31fd60d6bfdc/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

View File

@ -0,0 +1,22 @@
package configuration
import (
// other
"github.com/vrischmann/envconfig"
)
type config struct {
HTTP struct {
Listen string `envconfig:"default=127.0.0.1:62222"`
WaitForSeconds int `envconfig:"default=10"`
}
}
// Initialize loads configuration into memory.
func (cf *config) Initialize() {
log.Info().Msg("Loading configuration...")
_ = envconfig.Init(cf)
log.Info().Msgf("Configuration parsed: %+v", cf)
}

View File

@ -0,0 +1,25 @@
package configuration
import (
// local
"sources.dev.pztrn.name/pztrn/giredore/internal/logger"
// other
"github.com/rs/zerolog"
)
var (
log zerolog.Logger
loggerInitialized bool
Cfg *config
)
func Initialize() {
log = logger.Logger.With().Str("type", "internal").Str("package", "configuration").Logger()
loggerInitialized = true
log.Info().Msg("Initializing...")
Cfg = &config{}
Cfg.Initialize()
}

View File

@ -0,0 +1,107 @@
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"
)
var (
log zerolog.Logger
Srv *echo.Echo
)
func Initialize() {
log = logger.Logger.With().Str("type", "internal").Str("package", "httpserver").Logger()
log.Info().Msg("Initializing...")
Srv = echo.New()
Srv.Use(middleware.Recover())
Srv.Use(requestLogger())
Srv.DisableHTTP2 = true
Srv.HideBanner = true
Srv.HidePort = true
Srv.GET("/_internal/waitForOnline", waitForHTTPServerToBeUpHandler)
}
// 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")
}
// Start starts HTTP server and checks that server is ready to process
// requests. Returns true on success and false on failure.
func Start() {
log.Info().Str("address", configuration.Cfg.HTTP.Listen).Msg("Starting HTTP server...")
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 occured")
}
}()
// Check that HTTP server was started.
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")
if err != nil {
log.Debug().Err(err).Msg("HTTP error occured, 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 {
break
}
}
}
log.Info().Msg("HTTP server is ready to process requests")
}
func waitForHTTPServerToBeUpHandler(ec echo.Context) error {
response := map[string]string{
"error": "None",
}
return ec.JSON(200, response)
}

View File

@ -0,0 +1,24 @@
package httpserver
import (
// other
"github.com/labstack/echo"
)
func requestLogger() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(ec echo.Context) error {
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()).
Msg("HTTP request")
_ = next(ec)
return nil
}
}
}

View File

@ -0,0 +1,76 @@
package logger
import (
// stdlib
"fmt"
"os"
"strings"
"time"
// other
"github.com/rs/zerolog"
)
var (
Logger zerolog.Logger
SuperVerbosive bool
loggerInitialized bool
)
// Initialize initializes zerolog with proper formatting and log level.
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)
case "INFO":
zerolog.SetGlobalLevel(zerolog.InfoLevel)
case "WARN":
zerolog.SetGlobalLevel(zerolog.WarnLevel)
case "ERROR":
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
case "FATAL":
zerolog.SetGlobalLevel(zerolog.FatalLevel)
default:
fmt.Println("Invalid logger level passed:", loggerLevel)
fmt.Println("Fofcing INFO")
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}
} else {
fmt.Println("Setting logger level to: info")
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}
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 {
case "DEBUG":
v = fmt.Sprintf("\x1b[30m%-5s\x1b[0m", ii)
case "ERROR":
v = fmt.Sprintf("\x1b[31m%-5s\x1b[0m", ii)
case "FATAL":
v = fmt.Sprintf("\x1b[35m%-5s\x1b[0m", ii)
case "INFO":
v = fmt.Sprintf("\x1b[32m%-5s\x1b[0m", ii)
case "PANIC":
v = fmt.Sprintf("\x1b[36m%-5s\x1b[0m", ii)
case "WARN":
v = fmt.Sprintf("\x1b[33m%-5s\x1b[0m", ii)
default:
v = ii
}
}
return fmt.Sprintf("| %s |", v)
}
Logger = zerolog.New(output).With().Timestamp().Logger()
loggerInitialized = true
}

View File

@ -0,0 +1,54 @@
package requester
import (
// stdlib
"io/ioutil"
"net/http"
// local
"sources.dev.pztrn.name/pztrn/giredore/internal/logger"
// other
"github.com/rs/zerolog"
)
var (
log zerolog.Logger
)
func Initialize() {
log = logger.Logger.With().Str("type", "internal").Str("package", "requester").Logger()
log.Info().Msg("Initializing...")
}
func execRequest(method string, url string, data map[string]string) ([]byte, error) {
log.Debug().Str("method", method).Str("URL", url).Msg("Trying to execute HTTP request...")
httpClient := getHTTPClient()
// Compose HTTP request.
// ToDo: POST/PUT/other methods that require body.
httpReq, err := http.NewRequest(method, url, nil)
if err != nil {
return nil, err
}
response, err1 := httpClient.Do(httpReq)
if err1 != nil {
return nil, err1
}
bodyBytes, err2 := ioutil.ReadAll(response.Body)
if err2 != nil {
return nil, err2
}
response.Body.Close()
log.Debug().Int("response body length (bytes)", len(bodyBytes)).Msg("Got response")
return bodyBytes, nil
}
func Get(url string) ([]byte, error) {
return execRequest("GET", url, nil)
}

View File

@ -0,0 +1,30 @@
package requester
import (
// stdlib
"net"
"net/http"
"time"
)
func getHTTPClient() *http.Client {
c := &http.Client{
Transport: &http.Transport{
// ToDo: configurable.
ExpectContinueTimeout: time.Second * 5,
DialContext: (&net.Dialer{
// ToDo: configurable.
Timeout: time.Second * 5,
}).DialContext,
// ToDo: configurable.
ResponseHeaderTimeout: time.Second * 5,
// ToDo: configurable.
TLSHandshakeTimeout: time.Second * 10,
},
}
// ToDo: skip verifying insecure certificates if option was
// specified.
return c
}