From 0669c6ec0ae0eb3fcfc7174b15a17b8974f5a2e6 Mon Sep 17 00:00:00 2001 From: Andrey Savchuk Date: Wed, 25 Apr 2018 18:19:42 +0300 Subject: [PATCH] init --- .gitignore | 75 +++++++++++ Dockerfile | 18 +++ Gopkg.lock | 302 ++++++++++++++++++++++++++++++++++++++++++ Gopkg.toml | 30 +++++ build.sh | 4 + conf/config.yml | 5 + conf/configuration.go | 16 +++ conf/remoteConfig.go | 10 ++ helper/handler.go | 11 ++ main.go | 101 ++++++++++++++ 10 files changed, 572 insertions(+) create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 Gopkg.lock create mode 100644 Gopkg.toml create mode 100755 build.sh create mode 100644 conf/config.yml create mode 100644 conf/configuration.go create mode 100644 conf/remoteConfig.go create mode 100644 helper/handler.go create mode 100644 main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5386965 --- /dev/null +++ b/.gitignore @@ -0,0 +1,75 @@ +# Created by .ignore support plugin (hsz.mobi) +### JetBrains template +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +/.idea/ +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/dictionaries +*.iml +convert_service +convert-service + +# Sensitive or high-churn files: +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml + +# Gradle: +.idea/**/gradle.xml +.idea/**/libraries + +# CMake +cmake-build-debug/ +cmake-build-release/ + +# Mongo Explorer plugin: +.idea/**/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties +### Go template +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, build with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +/node_modules/ +/vendor/ +google/ +package.json +structure/ +yarn.lock diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..6aae299 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ +# Version: 0.0.1 +FROM alpine:latest + +#RUN CGO_ENABLED=0 GOOS=linux go build -installsuffix cgo -v + +RUN mkdir app + +RUN apk add --no-cache tzdata +RUN cp /usr/share/zoneinfo/UTC /etc/localtime +RUN echo "UTC" > /etc/timezone + +ADD admin-service /app/admin-service +ADD conf/config.yml /app/config.yml +ADD static /app/static + +ENTRYPOINT ["/app/admin-service"] + +EXPOSE 5000 diff --git a/Gopkg.lock b/Gopkg.lock new file mode 100644 index 0000000..c9ef38d --- /dev/null +++ b/Gopkg.lock @@ -0,0 +1,302 @@ +# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. + + +[[projects]] + name = "github.com/asaskevich/govalidator" + packages = ["."] + revision = "ccb8e960c48f04d6935e72476ae4a51028f9e22f" + version = "v9" + +[[projects]] + branch = "master" + name = "github.com/facebookgo/stack" + packages = ["."] + revision = "751773369052141c013c6e827a71e8f35c07879c" + +[[projects]] + name = "github.com/fsnotify/fsnotify" + packages = ["."] + revision = "c2828203cd70a50dcccfb2761f8b1f8ceef9a8e9" + version = "v1.4.7" + +[[projects]] + name = "github.com/go-pg/pg" + packages = [ + ".", + "internal", + "internal/parser", + "internal/pool", + "orm", + "types" + ] + revision = "5b73ce88484575f3480edf393237f6bf79d5f166" + version = "v6.11.2" + +[[projects]] + name = "github.com/golang/protobuf" + packages = [ + "proto", + "protoc-gen-go/descriptor", + "ptypes", + "ptypes/any", + "ptypes/duration", + "ptypes/struct", + "ptypes/timestamp" + ] + revision = "925541529c1fa6821df4e44ce2723319eb2be768" + version = "v1.0.0" + +[[projects]] + branch = "master" + name = "github.com/googollee/go-engine.io" + packages = [ + ".", + "message", + "parser", + "polling", + "transport", + "websocket" + ] + revision = "80ae0e43aca17b4c5a6834999d0f2eaa16b9afda" + +[[projects]] + branch = "master" + name = "github.com/googollee/go-socket.io" + packages = ["."] + revision = "970a1286d096d33129776082dfa89b35970e00c2" + +[[projects]] + name = "github.com/gorilla/websocket" + packages = ["."] + revision = "ea4d1f681babbce9545c9c5f3d5194a789c89f5b" + version = "v1.2.0" + +[[projects]] + branch = "master" + name = "github.com/graarh/golang-socketio" + packages = [ + ".", + "protocol", + "transport" + ] + revision = "2c44953b9b5fcd4da83ec8816f002ce68a12ef2d" + +[[projects]] + branch = "master" + name = "github.com/hashicorp/hcl" + packages = [ + ".", + "hcl/ast", + "hcl/parser", + "hcl/printer", + "hcl/scanner", + "hcl/strconv", + "hcl/token", + "json/parser", + "json/scanner", + "json/token" + ] + revision = "ef8a98b0bbce4a65b5aa4c368430a80ddc533168" + +[[projects]] + branch = "master" + name = "github.com/jinzhu/inflection" + packages = ["."] + revision = "04140366298a54a039076d798123ffa108fff46c" + +[[projects]] + branch = "master" + name = "github.com/lib/pq" + packages = [ + ".", + "oid" + ] + revision = "d34b9ff171c21ad295489235aec8b6626023cd04" + +[[projects]] + name = "github.com/magiconair/properties" + packages = ["."] + revision = "c3beff4c2358b44d0493c7dda585e7db7ff28ae6" + version = "v1.7.6" + +[[projects]] + branch = "master" + name = "github.com/mitchellh/mapstructure" + packages = ["."] + revision = "00c29f56e2386353d58c599509e8dc3801b0d716" + +[[projects]] + name = "github.com/pelletier/go-toml" + packages = ["."] + revision = "acdc4509485b587f5e675510c4f2c63e90ff68a8" + version = "v1.1.0" + +[[projects]] + name = "github.com/pressly/goose" + packages = ["."] + revision = "056a4d47dcc4d67fa3947a4f13945a5c690e568b" + version = "v2.1.0" + +[[projects]] + name = "github.com/processout/grpc-go-pool" + packages = ["."] + revision = "65ee74e8c5006a01f3cf6f3eb3b90379e45a24ad" + version = "v1.0" + +[[projects]] + name = "github.com/sirupsen/logrus" + packages = ["."] + revision = "c155da19408a8799da419ed3eeb0cb5db0ad5dbc" + version = "v1.0.5" + +[[projects]] + name = "github.com/spf13/afero" + packages = [ + ".", + "mem" + ] + revision = "63644898a8da0bc22138abf860edaf5277b6102e" + version = "v1.1.0" + +[[projects]] + name = "github.com/spf13/cast" + packages = ["."] + revision = "8965335b8c7107321228e3e3702cab9832751bac" + version = "v1.2.0" + +[[projects]] + branch = "master" + name = "github.com/spf13/jwalterweatherman" + packages = ["."] + revision = "7c0cea34c8ece3fbeb2b27ab9b59511d360fb394" + +[[projects]] + name = "github.com/spf13/pflag" + packages = ["."] + revision = "583c0c0531f06d5278b7d917446061adc344b5cd" + version = "v1.0.1" + +[[projects]] + name = "github.com/spf13/viper" + packages = ["."] + revision = "b5e8006cbee93ec955a89ab31e0e3ce3204f3736" + version = "v1.0.2" + +[[projects]] + branch = "master" + name = "gitlab8.alx/msp2.0/msp-lib" + packages = [ + "backend", + "config", + "database", + "logger", + "proto/stubs", + "socket", + "structure", + "utils" + ] + revision = "292a73558ab9ae03f1d5287dc722ca8a1172c412" + +[[projects]] + branch = "master" + name = "golang.org/x/crypto" + packages = ["ssh/terminal"] + revision = "b0697eccbea9adec5b7ba8008f4c33d98d733388" + +[[projects]] + branch = "master" + name = "golang.org/x/net" + packages = [ + "context", + "http/httpguts", + "http2", + "http2/hpack", + "idna", + "internal/timeseries", + "lex/httplex", + "trace" + ] + revision = "5f9ae10d9af5b1c89ae6904293b14b064d4ada23" + +[[projects]] + branch = "master" + name = "golang.org/x/sys" + packages = [ + "unix", + "windows" + ] + revision = "79b0c6888797020a994db17c8510466c72fe75d9" + +[[projects]] + name = "golang.org/x/text" + packages = [ + "collate", + "collate/build", + "internal/colltab", + "internal/gen", + "internal/tag", + "internal/triegen", + "internal/ucd", + "language", + "secure/bidirule", + "transform", + "unicode/bidi", + "unicode/cldr", + "unicode/norm", + "unicode/rangetable" + ] + revision = "f21a4dfb5e38f5895301dc265a8def02365cc3d0" + version = "v0.3.0" + +[[projects]] + branch = "master" + name = "google.golang.org/genproto" + packages = [ + "googleapis/api/annotations", + "googleapis/rpc/errdetails", + "googleapis/rpc/status" + ] + revision = "7fd901a49ba6a7f87732eb344f6e3c5b19d1b200" + +[[projects]] + name = "google.golang.org/grpc" + packages = [ + ".", + "balancer", + "balancer/base", + "balancer/roundrobin", + "codes", + "connectivity", + "credentials", + "encoding", + "encoding/proto", + "grpclb/grpc_lb_v1/messages", + "grpclog", + "internal", + "keepalive", + "metadata", + "naming", + "peer", + "resolver", + "resolver/dns", + "resolver/passthrough", + "stats", + "status", + "tap", + "transport" + ] + revision = "d11072e7ca9811b1100b80ca0269ac831f06d024" + version = "v1.11.3" + +[[projects]] + name = "gopkg.in/yaml.v2" + packages = ["."] + revision = "5420a8b6744d3b0345ab293f6fcba19c978f1183" + version = "v2.2.1" + +[solve-meta] + analyzer-name = "dep" + analyzer-version = 1 + inputs-digest = "c6f11f2f89bc92ca9c30a2800a34ad973dda58ff5a45c6fbcb0c52e6cee6c43e" + solver-name = "gps-cdcl" + solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml new file mode 100644 index 0000000..24f0f17 --- /dev/null +++ b/Gopkg.toml @@ -0,0 +1,30 @@ +# Gopkg.toml example +# +# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md +# for detailed Gopkg.toml documentation. +# +# required = ["github.com/user/thing/cmd/thing"] +# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] +# +# [[constraint]] +# name = "github.com/user/project" +# version = "1.0.0" +# +# [[constraint]] +# name = "github.com/user/project2" +# branch = "dev" +# source = "github.com/myfork/project2" +# +# [[override]] +# name = "github.com/x/y" +# version = "2.4.0" +# +# [prune] +# non-go = false +# go-tests = true +# unused-packages = true + + +[prune] + go-tests = true + unused-packages = true diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..0369386 --- /dev/null +++ b/build.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +export GOPATH="$PWD/../../" +CGO_ENABLED=0 GOOS=linux go build -installsuffix cgo -v \ No newline at end of file diff --git a/conf/config.yml b/conf/config.yml new file mode 100644 index 0000000..877e6a5 --- /dev/null +++ b/conf/config.yml @@ -0,0 +1,5 @@ +configServiceAddress: + ip: "10.250.9.114" + port: "5000" +moduleName: "converter" +instanceUuid: "bf482806-0c3d-4e0d-b9d4-12c037b12d70" \ No newline at end of file diff --git a/conf/configuration.go b/conf/configuration.go new file mode 100644 index 0000000..cc49302 --- /dev/null +++ b/conf/configuration.go @@ -0,0 +1,16 @@ +package conf + +type Configuration struct { + ConfigServiceAddress AddressConfiguration + ModuleName string + InstanceUuid string +} + +type AddressConfiguration struct { + Port string `valid:"required~Required"` + IP string `valid:"required~Required"` +} + +func (addressConfiguration *AddressConfiguration) GetAddress() string { + return addressConfiguration.IP + ":" + addressConfiguration.Port +} diff --git a/conf/remoteConfig.go b/conf/remoteConfig.go new file mode 100644 index 0000000..efaf4ac --- /dev/null +++ b/conf/remoteConfig.go @@ -0,0 +1,10 @@ +package conf + +import "gitlab8.alx/msp2.0/msp-lib/database" + +type RemoteConfig struct { + GrpcInnerIp string `valid:"required~Required"` + GrpcPrefix string + GrpcAddress AddressConfiguration `valid:"required~Required"` + Database database.DBConfiguration `valid:"required~Required"` +} diff --git a/helper/handler.go b/helper/handler.go new file mode 100644 index 0000000..9461185 --- /dev/null +++ b/helper/handler.go @@ -0,0 +1,11 @@ +package helper + +type Handlers struct { + // ===== INSTANCE ===== +} + +func GetHandlers() *Handlers { + return &Handlers{ + // ===== INSTANCE ===== + } +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..868087a --- /dev/null +++ b/main.go @@ -0,0 +1,101 @@ +package main + +import ( + rn "runtime" + "gitlab8.alx/msp2.0/msp-lib/backend" + "gitlab8.alx/msp2.0/msp-lib/config" + "gitlab8.alx/msp2.0/msp-lib/socket" + "gitlab8.alx/msp2.0/msp-lib/logger" + "gitlab8.alx/msp2.0/msp-lib/structure" + "gitlab8.alx/msp2.0/msp-lib/utils" + "admin-service/conf" + "os" + "path" + "admin-service/helper" + "time" + "github.com/asaskevich/govalidator" + "github.com/graarh/golang-socketio" + "encoding/json" + "gitlab8.alx/msp2.0/msp-lib/database" +) + +var ( + configData *conf.Configuration + executableFileDir string +) + +func init() { + appConfig := config.Get().(*conf.Configuration) + socket.InitClient( + socket.SocketConfiguration{ + Host: appConfig.ConfigServiceAddress.IP, + Port: appConfig.ConfigServiceAddress.Port, + Secure: false, + UrlParams: map[string]string{"module_name": appConfig.ModuleName, "instance_uuid": appConfig.InstanceUuid}, + }, + func(client *gosocketio.Client) { + subscribeSocket(client, utils.SendConfigWhenConnected) + subscribeSocket(client, utils.SendConfigChanged) + subscribeSocket(client, utils.SendConfigOnRequest) + }, + ) + time.Sleep(time.Second * 3) + for config.GetRemote() == nil { + logger.Warn("Remote config isn't received") + time.Sleep(time.Second * 5) + } + remoteConfig := config.GetRemote().(*conf.RemoteConfig) + validRemoteConfig(remoteConfig) + config.InitConfig(&conf.Configuration{}) + database.InitDb(remoteConfig.Database) +} + +func main() { + ex, err := os.Executable() + executableFileDir = path.Dir(ex) + if err != nil { + logger.Fatal(err) + panic(err) + } + if utils.DEV { + _, filename, _, _ := rn.Caller(0) + executableFileDir = path.Dir(filename) + } + createGrpcServer() +} + +// Start a GRPC server. +func createGrpcServer() { + // Run our server in a goroutine so that it doesn't block. + handlers := helper.GetHandlers() + remoteConfig := config.GetRemote().(*conf.RemoteConfig) + addr := structure.AddressConfiguration{IP: remoteConfig.GrpcAddress.IP, Port: remoteConfig.GrpcAddress.Port} + addrInner := structure.AddressConfiguration{IP: remoteConfig.GrpcInnerIp, Port: remoteConfig.GrpcAddress.Port} + backend.StartBackendGrpcServer(addr, backend.GetDefaultService(remoteConfig.GrpcPrefix+"/", handlers)) + methods := backend.GetBackendConfig(addrInner, remoteConfig.GrpcPrefix+"/", handlers) + bytes, err := json.Marshal(methods) + if err != nil { + logger.Warn("Error when serializing Backend Routes", err) + } + socket.GetClient().Emit(utils.SendRoutesWhenConnected, string(bytes)) + logger.Infof("EXPORTED MODULE METHODS: %s", methods) +} + +func validRemoteConfig(remoteConfig *conf.RemoteConfig) { + _, err := govalidator.ValidateStruct(remoteConfig) + if err != nil { + validationErrors := govalidator.ErrorsByField(err) + logger.Fatal("Remote config int't valid", validationErrors) + panic(err) + } +} + +func subscribeSocket(client *gosocketio.Client, eventName string) { + client.On(eventName, func(h *gosocketio.Channel, args string) error { + logger.Infof("--- Got event: %s message: %s", eventName, args) + remoteConfig := &conf.RemoteConfig{} + config.InitRemoteConfig(remoteConfig, args) + validRemoteConfig(remoteConfig) + return nil + }) +} \ No newline at end of file