Skip to content

Commit

Permalink
Deployment (#5)
Browse files Browse the repository at this point in the history
* minor fixes

* remove bin and vendor from git

* minor fixes
  • Loading branch information
AbolfazlSoltaani authored Feb 11, 2024
1 parent 461ff81 commit e8498f8
Show file tree
Hide file tree
Showing 1,646 changed files with 506 additions and 700,253 deletions.
190 changes: 188 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,188 @@
/.idea
data/
# Created by https://www.toptal.com/developers/gitignore/api/goland,go,database,macos
# Edit at https://www.toptal.com/developers/gitignore?templates=goland,go,database,macos

### Database ###
*.accdb
*.db
*.dbf
*.mdb
*.pdb
*.sqlite3
*.db-shm
*.db-wal

### Go ###
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
bin/

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
vendor/

# Go workspace file
go.work

### GoLand ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

# AWS User-specific
.idea/**/aws.xml

# Generated files
.idea/**/contentModel.xml

# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr

# CMake
cmake-build-*/

# Mongo Explorer plugin
.idea/**/mongoSettings.xml

# File-based project format
*.iws

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# SonarLint plugin
.idea/sonarlint/

.idea/
data/

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# Editor-based Rest Client
.idea/httpRequests

# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser

### GoLand Patch ###
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721

# *.iml
# modules.xml
# .idea/misc.xml
# *.ipr

# Sonarlint plugin
# https://plugins.jetbrains.com/plugin/7973-sonarlint
.idea/**/sonarlint/

# SonarQube Plugin
# https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin
.idea/**/sonarIssues.xml

# Markdown Navigator plugin
# https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced
.idea/**/markdown-navigator.xml
.idea/**/markdown-navigator-enh.xml
.idea/**/markdown-navigator/

# Cache file creation bug
# See https://youtrack.jetbrains.com/issue/JBR-2257
.idea/$CACHE_FILE$

# CodeStream plugin
# https://plugins.jetbrains.com/plugin/12206-codestream
.idea/codestream.xml

# Azure Toolkit for IntelliJ plugin
# https://plugins.jetbrains.com/plugin/8053-azure-toolkit-for-intellij
.idea/**/azureSettings.xml

### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

### macOS Patch ###
# iCloud generated files
*.icloud
data/

# End of https://www.toptal.com/developers/gitignore/api/goland,go,database,macos
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ RUN apt-get update && \

WORKDIR /app
COPY ./bin/zookeeper ./
COPY ./config/config.yml ./config/config.yml
COPY config/sample-config.yml ./config/config.yml
EXPOSE 8000

ENTRYPOINT ["/app/zookeeper"]
2 changes: 2 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"Zookeeper/internal/zookeeper"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)

Expand All @@ -16,6 +17,7 @@ func init() {
}

func main() {
log.SetLevel(log.DebugLevel)
z := zookeeper.NewZookeeper()
z.Run()
}
5 changes: 2 additions & 3 deletions config/config.yml → config/sample-config.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
health_check_path: "/healthz"
port: 8000
replica: 1
brokers:
- name: "node1"
host: "http://broker1:8080"
- name: "node2"
host: "http://broker2:8080"
host: "http://broker:8080"
postgres:
host: "postgres-zookeeper"
port: 5432
Expand Down
29 changes: 0 additions & 29 deletions docker-compose.yaml

This file was deleted.

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21
require (
github.com/gin-gonic/gin v1.9.1
github.com/lib/pq v1.10.9
github.com/sirupsen/logrus v1.9.3
github.com/spf13/viper v1.18.2
)

Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6ke
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
Expand Down Expand Up @@ -111,6 +113,7 @@ golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqR
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
Expand Down
5 changes: 3 additions & 2 deletions init.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
CREATE TABLE queues (
queue VARCHAR(255) NOT NULL,
is_master BOOLEAN DEFAULT false,
broker VARCHAR(255) NOT NULL
);
broker VARCHAR(255) NOT NULL,
PRIMARY KEY (queue, broker)
);
34 changes: 17 additions & 17 deletions internal/broker/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"Zookeeper/internal/types"
"bytes"
"encoding/json"
log "github.com/sirupsen/logrus"
"io"
"log"
"net/http"
"strings"
)
Expand All @@ -32,7 +32,8 @@ func (b *Client) NewRequest(method string, url string, route string, body io.Rea

// HealthCheck checks the health of the broker
func (b *Client) HealthCheck() error {
return nil
err := b.Do(http.MethodGet, "/healthz", nil, nil)
return err
}

func processRequest(req *http.Request) ([]byte, error) {
Expand All @@ -53,6 +54,7 @@ func (b *Client) Do(method, path string, req interface{}, resp interface{}) erro
var body io.Reader
if req != nil {
data, err := json.Marshal(req)
log.Info(string(data))
if err != nil {
return err
}
Expand All @@ -65,7 +67,6 @@ func (b *Client) Do(method, path string, req interface{}, resp interface{}) erro
}

data, err := processRequest(httpRequest)
log.Printf("Response from broker %s: %s", b.Name, string(data))
if err == nil {
if resp != nil {
return json.Unmarshal(data, resp)
Expand All @@ -75,11 +76,10 @@ func (b *Client) Do(method, path string, req interface{}, resp interface{}) erro
return err
}

// Push pushes a message to the queueName
// Push pushes a message to the key
func (b *Client) Push(req *types.Element) error {
log.Printf("Pushing message to queue %s in broker %s", req.QueueName, b.Name)
replaceDict := map[string]string{
"{queue_name}": req.QueueName,
"{key}": req.Key,
}
apiURL := substringReplace(routes.RoutePush, replaceDict)
request := map[string][]byte{
Expand All @@ -92,7 +92,7 @@ func (b *Client) Push(req *types.Element) error {
return nil
}

// Front Get front value of any queue that is a master and not empty
// Front Get front value of any key that is a master and not empty
func (b *Client) Front() (*types.Element, error) {
res := &types.Element{}
err := b.Do(http.MethodGet, routes.RouteFront, nil, res)
Expand All @@ -102,26 +102,26 @@ func (b *Client) Front() (*types.Element, error) {
return res, nil
}

// AddQueue adds a queue to the broker
func (b *Client) AddQueue(queueName string, isMaster bool) error {
log.Printf("Adding queue %s to broker %s", queueName, b.Name)
req := &types.AddQueueRequest{
QueueName: queueName,
IsMaster: isMaster,
// AddKey adds a queue to the broker
func (b *Client) AddKey(key string, isMaster bool) error {
req := &types.AddKeyRequest{
Key: key,
IsMaster: isMaster,
}
err := b.Do(http.MethodPost, routes.RouteAddQueue, req, nil)
err := b.Do(http.MethodPost, routes.RouteKey, req, nil)
return err
}

// Remove pops a message from queue \"queueName\"
func (b *Client) Remove(queueName string) error {
func (b *Client) Remove(key string) error {
replaceDict := map[string]string{
"{queue_name}": queueName,
"{key}": key,
}
apiURL := substringReplace(routes.RoutePop, replaceDict)
req := map[string]string{
"queue_name": queueName,
"key": key,
}

err := b.Do(http.MethodPost, apiURL, req, nil)
return err
}
Expand Down
Loading

0 comments on commit e8498f8

Please sign in to comment.