Compare commits
36 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 081e1edd24 | |||
| c454307d56 | |||
| 359a89ba62 | |||
| 3b73091cfb | |||
| c8beab76f1 | |||
| 599eaa3712 | |||
| 1de38fdfdf | |||
| 4d1f3644b0 | |||
| 71b3f13284 | |||
| c64bc3fe6b | |||
| 016bddb5c4 | |||
| 98d85810f3 | |||
| 3fc1191f6e | |||
| 52793ac244 | |||
| f8a1ae841d | |||
| 9d424742d6 | |||
| a9b73a6536 | |||
| 95584eb407 | |||
| ce7e2bc75a | |||
| 4bedc5d926 | |||
| ea7f98f8bb | |||
| bbdce9b25a | |||
| 1d75153698 | |||
| c177133daf | |||
| dad915f2ec | |||
| 54e4006d0d | |||
| 9cb590bf7a | |||
| 70c41dab96 | |||
| 99ac0a83c6 | |||
| 30c45b4503 | |||
| 49f552bf68 | |||
| 6f875cbb94 | |||
| f9331deec1 | |||
| c29bb94c3d | |||
| 8d884eebda | |||
| 2f022919ff |
@@ -3,7 +3,7 @@ package api
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
def "git.pbiernat.dev/egommerce/api-entities/http"
|
def "git.pbiernat.io/egommerce/api-entities/http"
|
||||||
"github.com/go-redis/redis/v8"
|
"github.com/go-redis/redis/v8"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package api
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
def "git.pbiernat.dev/egommerce/api-entities/http"
|
def "git.pbiernat.io/egommerce/api-entities/http"
|
||||||
"github.com/go-redis/redis/v8"
|
"github.com/go-redis/redis/v8"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
@@ -15,7 +16,7 @@ func init() {
|
|||||||
func GetEnv(name string, defVal string) string {
|
func GetEnv(name string, defVal string) string {
|
||||||
env := os.Getenv(name)
|
env := os.Getenv(name)
|
||||||
if env == "" {
|
if env == "" {
|
||||||
return defVal
|
fmt.Panicln("Missing " + name + " env variable")
|
||||||
}
|
}
|
||||||
|
|
||||||
return env
|
return env
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.pbiernat.dev/egommerce/go-api-pkg/config"
|
"git.pbiernat.io/egommerce/go-api-pkg/config"
|
||||||
consul "github.com/hashicorp/consul/api"
|
consul "github.com/hashicorp/consul/api"
|
||||||
"github.com/hashicorp/consul/connect"
|
"github.com/hashicorp/consul/connect"
|
||||||
)
|
)
|
||||||
@@ -61,16 +61,26 @@ func (s *Service) GetID() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) GetFullAddr() string {
|
func (s *Service) GetFullAddr() string {
|
||||||
return fmt.Sprintf("https://%s:%d/", s.domain, s.port)
|
isTLS := s.port == 443
|
||||||
|
proto := "http"
|
||||||
|
if isTLS {
|
||||||
|
proto = "https"
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%s://%s:%d/", proto, s.domain, s.port)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) Register() error {
|
func (s *Service) Register() error {
|
||||||
def := &consul.AgentServiceRegistration{
|
def := &consul.AgentServiceRegistration{
|
||||||
ID: s.GetID(),
|
ID: s.GetID(),
|
||||||
|
// Kind: consul.ServiceKindConnectProxy,
|
||||||
Name: s.Name,
|
Name: s.Name,
|
||||||
Address: s.Address,
|
Address: s.Address,
|
||||||
Port: s.port,
|
Port: s.port,
|
||||||
Tags: s.getTags(),
|
Tags: s.getTags(),
|
||||||
|
// Connect: &consul.AgentServiceConnect{Native: true},
|
||||||
|
// Proxy: &consul.AgentServiceConnectProxyConfig{
|
||||||
|
// DestinationServiceName: s.Name,
|
||||||
|
// },
|
||||||
Check: &consul.AgentServiceCheck{
|
Check: &consul.AgentServiceCheck{
|
||||||
TTL: s.ttl.String(),
|
TTL: s.ttl.String(),
|
||||||
Status: "passing",
|
Status: "passing",
|
||||||
@@ -82,35 +92,42 @@ func (s *Service) Register() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
go func(s *Service) { // startup register
|
|
||||||
ticker := time.NewTicker(time.Second * 1)
|
|
||||||
for range ticker.C {
|
|
||||||
if ok, _ := s.healthCheck(); ok {
|
|
||||||
ticker.Stop()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}(s)
|
|
||||||
|
|
||||||
go func(s *Service) { // TTL
|
|
||||||
interval := s.ttl - time.Second*2 // 2 seconds overhead
|
|
||||||
ticker := time.NewTicker(interval)
|
|
||||||
for range ticker.C {
|
|
||||||
if _, err := s.healthCheck(); err != nil {
|
|
||||||
fmt.Printf("TTL Error #: %v\n", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}(s)
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func (s *Service) Unregister() error {
|
func (s *Service) Unregister() error {
|
||||||
return s.agent.ServiceDeregister(s.GetID())
|
return s.agent.ServiceDeregister(s.GetID())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Service) RegisterHealthChecks() {
|
||||||
|
go func() { // startup register
|
||||||
|
ticker := time.NewTicker(time.Second * 1)
|
||||||
|
for range ticker.C {
|
||||||
|
if ok, _ := s.healthCheck(); ok {
|
||||||
|
ticker.Stop()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
go func() { // TTL
|
||||||
|
interval := s.ttl - (time.Second * 2) // 2 seconds overhead
|
||||||
|
ticker := time.NewTicker(interval)
|
||||||
|
for range ticker.C {
|
||||||
|
if _, err := s.healthCheck(); err != nil {
|
||||||
|
fmt.Printf("TTL Error #: %v\n", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Service) Connect() (*connect.Service, error) {
|
func (s *Service) Connect() (*connect.Service, error) {
|
||||||
|
// l := hclog.New(&hclog.LoggerOptions{
|
||||||
|
// Name: "consul-registry",
|
||||||
|
// Level: hclog.Trace,
|
||||||
|
// })
|
||||||
svc, err := connect.NewService(s.Name, s.client)
|
svc, err := connect.NewService(s.Name, s.client)
|
||||||
s.connect = svc
|
s.connect = svc
|
||||||
cnf := svc.ServerTLSConfig()
|
cnf := svc.ServerTLSConfig()
|
||||||
|
fmt.Printf("CONNECT SERVER:: %s CONFIG:: %v\n", s.Name, cnf)
|
||||||
for k, c := range cnf.Certificates {
|
for k, c := range cnf.Certificates {
|
||||||
fmt.Printf("CONNECT CERT %d: %v", k, c)
|
fmt.Printf("CONNECT CERT %d: %v", k, c)
|
||||||
}
|
}
|
||||||
@@ -132,7 +149,6 @@ func (s *Service) healthCheck() (bool, error) {
|
|||||||
}
|
}
|
||||||
req.Header.Set("User-Agent", "service/internal")
|
req.Header.Set("User-Agent", "service/internal")
|
||||||
|
|
||||||
fmt.Printf("Sending HEALTH CHECK request to: %s\n", healthUrl)
|
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
@@ -141,7 +157,6 @@ func (s *Service) healthCheck() (bool, error) {
|
|||||||
|
|
||||||
var body []byte
|
var body []byte
|
||||||
resp.Body.Read(body)
|
resp.Body.Read(body)
|
||||||
fmt.Printf("HEALTH CHECK response to: %v -- %v\n", resp, body)
|
|
||||||
|
|
||||||
return resp.StatusCode == http.StatusOK
|
return resp.StatusCode == http.StatusOK
|
||||||
}()
|
}()
|
||||||
@@ -165,7 +180,7 @@ func (s *Service) getTags() []string {
|
|||||||
"traefik.enable=true",
|
"traefik.enable=true",
|
||||||
"traefik.http.routers." + s.Name + ".rule=PathPrefix(`" + s.pathPrefix + "`)",
|
"traefik.http.routers." + s.Name + ".rule=PathPrefix(`" + s.pathPrefix + "`)",
|
||||||
"traefik.http.routers." + s.Name + ".entryPoints=https",
|
"traefik.http.routers." + s.Name + ".entryPoints=https",
|
||||||
"traefik.http.routers." + s.Name + ".tls=true",
|
// "traefik.http.routers." + s.Name + ".tls=true",
|
||||||
"traefik.http.routers." + s.Name + ".service=" + s.Name,
|
"traefik.http.routers." + s.Name + ".service=" + s.Name,
|
||||||
"traefik.http.routers." + s.Name + ".middlewares=auth_" + s.Name + ",requestid_" + s.Name + ",stripprefix_" + s.Name,
|
"traefik.http.routers." + s.Name + ".middlewares=auth_" + s.Name + ",requestid_" + s.Name + ",stripprefix_" + s.Name,
|
||||||
"traefik.http.services." + s.Name + ".loadbalancer.server.scheme=http",
|
"traefik.http.services." + s.Name + ".loadbalancer.server.scheme=http",
|
||||||
|
|||||||
4
go.mod
4
go.mod
@@ -1,9 +1,9 @@
|
|||||||
module git.pbiernat.dev/egommerce/go-api-pkg
|
module git.pbiernat.io/egommerce/go-api-pkg
|
||||||
|
|
||||||
go 1.18
|
go 1.18
|
||||||
|
|
||||||
require (
|
require (
|
||||||
git.pbiernat.dev/egommerce/api-entities v0.0.26
|
git.pbiernat.io/egommerce/api-entities v0.0.26
|
||||||
github.com/fluent/fluent-logger-golang v1.9.0
|
github.com/fluent/fluent-logger-golang v1.9.0
|
||||||
github.com/go-redis/redis/v8 v8.11.5
|
github.com/go-redis/redis/v8 v8.11.5
|
||||||
github.com/hashicorp/consul v1.16.0
|
github.com/hashicorp/consul v1.16.0
|
||||||
|
|||||||
38
redis/cache.go
Normal file
38
redis/cache.go
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package redis
|
||||||
|
|
||||||
|
// import (
|
||||||
|
// "context"
|
||||||
|
// "strconv"
|
||||||
|
// "time"
|
||||||
|
|
||||||
|
// "github.com/go-redis/redis/v8"
|
||||||
|
// )
|
||||||
|
|
||||||
|
// func NewCache(host string, port int, password string) *redis.Client {
|
||||||
|
// redis := redis.NewClient(&redis.Options{
|
||||||
|
// Addr: host + ":" + strconv.Itoa(port),
|
||||||
|
// Password: password,
|
||||||
|
// DB: 0,
|
||||||
|
// })
|
||||||
|
// defer redis.Close()
|
||||||
|
|
||||||
|
// return redis
|
||||||
|
// }
|
||||||
|
|
||||||
|
// func Get(ctx context.Context, key string) (float64, error) {
|
||||||
|
// // ctx := context.Background() // FIXME
|
||||||
|
// price, err := s.cache.Get(ctx, key).Float64()
|
||||||
|
// if err != nil {
|
||||||
|
// s.log.Log("cache read error(key not exists): %#v", err)
|
||||||
|
// return 0, err
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return price, nil
|
||||||
|
// }
|
||||||
|
|
||||||
|
// func Set(ctx context.Context, key string, value any, exp time.Duration) error {
|
||||||
|
// // ctx := context.Background() // FIXME
|
||||||
|
// s.cache.Set(ctx, key, value, exp)
|
||||||
|
|
||||||
|
// return nil
|
||||||
|
// }
|
||||||
Reference in New Issue
Block a user