Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 081e1edd24 | |||
| c454307d56 | |||
| 359a89ba62 | |||
| 3b73091cfb | |||
| c8beab76f1 | |||
| 599eaa3712 | |||
| 1de38fdfdf | |||
| 4d1f3644b0 |
@@ -3,7 +3,7 @@ package api
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
def "git.pbiernat.dev/egommerce/api-entities/http"
|
||||
def "git.pbiernat.io/egommerce/api-entities/http"
|
||||
"github.com/go-redis/redis/v8"
|
||||
)
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ package api
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
def "git.pbiernat.dev/egommerce/api-entities/http"
|
||||
def "git.pbiernat.io/egommerce/api-entities/http"
|
||||
"github.com/go-redis/redis/v8"
|
||||
)
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
@@ -15,7 +16,7 @@ func init() {
|
||||
func GetEnv(name string, defVal string) string {
|
||||
env := os.Getenv(name)
|
||||
if env == "" {
|
||||
return defVal
|
||||
fmt.Panicln("Missing " + name + " env variable")
|
||||
}
|
||||
|
||||
return env
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"git.pbiernat.dev/egommerce/go-api-pkg/config"
|
||||
"git.pbiernat.io/egommerce/go-api-pkg/config"
|
||||
consul "github.com/hashicorp/consul/api"
|
||||
"github.com/hashicorp/consul/connect"
|
||||
)
|
||||
@@ -61,7 +61,12 @@ func (s *Service) GetID() 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 {
|
||||
@@ -72,12 +77,7 @@ func (s *Service) Register() error {
|
||||
Address: s.Address,
|
||||
Port: s.port,
|
||||
Tags: s.getTags(),
|
||||
Connect: &consul.AgentServiceConnect{
|
||||
Native: true,
|
||||
// SidecarService: &consul.AgentServiceRegistration{
|
||||
// Port: s.port,
|
||||
// },
|
||||
},
|
||||
// Connect: &consul.AgentServiceConnect{Native: true},
|
||||
// Proxy: &consul.AgentServiceConnectProxyConfig{
|
||||
// DestinationServiceName: s.Name,
|
||||
// },
|
||||
@@ -98,7 +98,7 @@ func (s *Service) Unregister() error {
|
||||
return s.agent.ServiceDeregister(s.GetID())
|
||||
}
|
||||
|
||||
func (s *Service) InitHealthChecks() {
|
||||
func (s *Service) RegisterHealthChecks() {
|
||||
go func() { // startup register
|
||||
ticker := time.NewTicker(time.Second * 1)
|
||||
for range ticker.C {
|
||||
@@ -109,7 +109,7 @@ func (s *Service) InitHealthChecks() {
|
||||
}()
|
||||
|
||||
go func() { // TTL
|
||||
interval := s.ttl - time.Second*2 // 2 seconds overhead
|
||||
interval := s.ttl - (time.Second * 2) // 2 seconds overhead
|
||||
ticker := time.NewTicker(interval)
|
||||
for range ticker.C {
|
||||
if _, err := s.healthCheck(); err != nil {
|
||||
@@ -120,12 +120,14 @@ func (s *Service) InitHealthChecks() {
|
||||
}
|
||||
|
||||
func (s *Service) Connect() (*connect.Service, error) {
|
||||
// srvName := s.Name
|
||||
srvName := s.Name
|
||||
svc, err := connect.NewService(srvName, s.client)
|
||||
// l := hclog.New(&hclog.LoggerOptions{
|
||||
// Name: "consul-registry",
|
||||
// Level: hclog.Trace,
|
||||
// })
|
||||
svc, err := connect.NewService(s.Name, s.client)
|
||||
s.connect = svc
|
||||
cnf := svc.ServerTLSConfig()
|
||||
fmt.Printf("CONNECT SERVER:: %s CONFIG:: %v\n", srvName, cnf)
|
||||
fmt.Printf("CONNECT SERVER:: %s CONFIG:: %v\n", s.Name, cnf)
|
||||
for k, c := range cnf.Certificates {
|
||||
fmt.Printf("CONNECT CERT %d: %v", k, c)
|
||||
}
|
||||
@@ -147,17 +149,14 @@ func (s *Service) healthCheck() (bool, error) {
|
||||
}
|
||||
req.Header.Set("User-Agent", "service/internal")
|
||||
|
||||
fmt.Printf("Sending HEALTH CHECK request to: %s\n", healthUrl)
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
fmt.Printf("Sending HEALTH CHECK request error: %v\n", err)
|
||||
return false
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
var body []byte
|
||||
resp.Body.Read(body)
|
||||
fmt.Printf("HEALTH CHECK response to: %v -- %v\n", resp, body)
|
||||
|
||||
return resp.StatusCode == http.StatusOK
|
||||
}()
|
||||
@@ -181,7 +180,7 @@ func (s *Service) getTags() []string {
|
||||
"traefik.enable=true",
|
||||
"traefik.http.routers." + s.Name + ".rule=PathPrefix(`" + s.pathPrefix + "`)",
|
||||
"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 + ".middlewares=auth_" + s.Name + ",requestid_" + s.Name + ",stripprefix_" + s.Name,
|
||||
"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
|
||||
|
||||
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/go-redis/redis/v8 v8.11.5
|
||||
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