@@ -1,31 +1,29 @@
|
||||
package config
|
||||
|
||||
|
||||
type Configurations struct {
|
||||
Env string
|
||||
Server ServerConfigurations
|
||||
Kafka KafkaConfigurations
|
||||
SchemaRegistry SchemaRegistryConfigurations
|
||||
Env string
|
||||
Server ServerConfigurations
|
||||
Kafka KafkaConfigurations
|
||||
SchemaRegistry SchemaRegistryConfigurations
|
||||
}
|
||||
|
||||
|
||||
type ServerConfigurations struct {
|
||||
Port string
|
||||
Goroutines int
|
||||
Port string
|
||||
Goroutines int
|
||||
Cors string
|
||||
}
|
||||
|
||||
|
||||
type KafkaConfigurations struct {
|
||||
Bootstrap_Servers string
|
||||
Request_Timeout_Ms int
|
||||
Retry_Backoff_MS int
|
||||
Sasl_User string
|
||||
Sasl_Password string
|
||||
Kafka_Topic_Json string
|
||||
Kafka_Topic_Protobuf string
|
||||
Bootstrap_Servers string
|
||||
Request_Timeout_Ms int
|
||||
Retry_Backoff_MS int
|
||||
Sasl_User string
|
||||
Sasl_Password string
|
||||
Kafka_Topic_Json string
|
||||
Kafka_Topic_Protobuf string
|
||||
}
|
||||
|
||||
type SchemaRegistryConfigurations struct {
|
||||
Endpoint string
|
||||
Topics string
|
||||
Endpoint string
|
||||
Topics string
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ env: ENVIRONMENT # read from environment variable
|
||||
server:
|
||||
port: 8000
|
||||
goroutines: 2000
|
||||
cors: CORS_LIST # read from environment variable
|
||||
|
||||
kafka:
|
||||
bootstrap_servers: BOOTSTRAP_SERVERS # read from environment variable
|
||||
|
||||
1
go.mod
1
go.mod
@@ -11,6 +11,7 @@ require (
|
||||
github.com/pkg/errors v0.9.1
|
||||
github.com/prometheus/client_golang v0.9.3
|
||||
github.com/riferrei/srclient v0.2.1
|
||||
github.com/rs/cors v1.8.2 // indirect
|
||||
github.com/spf13/viper v1.7.1
|
||||
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c
|
||||
google.golang.org/protobuf v1.25.0 // indirect
|
||||
|
||||
2
go.sum
2
go.sum
@@ -312,6 +312,8 @@ github.com/riferrei/srclient v0.2.1 h1:uIJhzPXW+suDsEDOZKf4oTZZXTyxtw98cFC70rFzv
|
||||
github.com/riferrei/srclient v0.2.1/go.mod h1:SmCz0lrYQ1pLqXlYq0yPnRccHLGh+llDA0i6hecPeW8=
|
||||
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
|
||||
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
|
||||
github.com/rs/cors v1.8.2 h1:KCooALfAYGs415Cwu5ABvv9n9509fSiG5SQJn/AQo4U=
|
||||
github.com/rs/cors v1.8.2/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
|
||||
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
|
||||
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
|
||||
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
|
||||
|
||||
26
main.go
26
main.go
@@ -1,22 +1,21 @@
|
||||
package main
|
||||
|
||||
|
||||
import (
|
||||
lib "com.navi.medici.janus/lib"
|
||||
config "com.navi.medici.janus/config"
|
||||
server "com.navi.medici.janus/server"
|
||||
lib "com.navi.medici.janus/lib"
|
||||
producer_module "com.navi.medici.janus/producer"
|
||||
server "com.navi.medici.janus/server"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"log"
|
||||
"strings"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var (
|
||||
port string
|
||||
port string
|
||||
cors string
|
||||
)
|
||||
|
||||
|
||||
func init() {
|
||||
|
||||
viper.SetConfigName("config")
|
||||
@@ -42,13 +41,14 @@ func init() {
|
||||
configuration.Kafka.Kafka_Topic_Json = viper.GetString(configuration.Kafka.Kafka_Topic_Json)
|
||||
configuration.Kafka.Kafka_Topic_Protobuf = viper.GetString(configuration.Kafka.Kafka_Topic_Protobuf)
|
||||
|
||||
//log.Printf("Env is: ", configuration.Env)
|
||||
//if configuration.Env == "PROD" {
|
||||
configuration.Kafka.Sasl_User = viper.GetString(configuration.Kafka.Sasl_User)
|
||||
configuration.Kafka.Sasl_Password = viper.GetString(configuration.Kafka.Sasl_Password)
|
||||
//}
|
||||
|
||||
//log.Printf("Env is: ", configuration.Env)
|
||||
//if configuration.Env == "PROD" {
|
||||
configuration.Kafka.Sasl_User = viper.GetString(configuration.Kafka.Sasl_User)
|
||||
configuration.Kafka.Sasl_Password = viper.GetString(configuration.Kafka.Sasl_Password)
|
||||
//}
|
||||
|
||||
port = configuration.Server.Port
|
||||
cors = viper.GetString(configuration.Server.Cors)
|
||||
log.Printf("PORT IS: %v", port)
|
||||
log.Printf(configuration.Kafka.Bootstrap_Servers)
|
||||
log.Printf(configuration.SchemaRegistry.Endpoint)
|
||||
@@ -75,7 +75,7 @@ func init() {
|
||||
func main() {
|
||||
|
||||
log.Printf("Serving on http://0.0.0.0:", port)
|
||||
httpServer, err1 := server.NewServer(port)
|
||||
httpServer, err1 := server.NewServer(port, cors)
|
||||
metricsServer, err2 := server.MetricServer("4000")
|
||||
if err1 != nil {
|
||||
log.Fatalln("Unable to start server, ", err1)
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"github.com/rs/cors"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
@@ -17,15 +20,14 @@ type Server struct {
|
||||
}
|
||||
|
||||
var (
|
||||
networkTCP = "tcp"
|
||||
networkTCP = "tcp"
|
||||
)
|
||||
|
||||
|
||||
func NewServer(port string) (*Server, error) {
|
||||
func NewServer(port string, corsList string) (*Server, error) {
|
||||
|
||||
network := networkTCP
|
||||
|
||||
listener, err := net.Listen(network, ":" + port)
|
||||
listener, err := net.Listen(network, ":"+port)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to create listener")
|
||||
}
|
||||
@@ -42,12 +44,23 @@ func NewServer(port string) (*Server, error) {
|
||||
// router.HandleFunc("/test", testHandler).Methods("GET")
|
||||
// router.HandleFunc("/stop", stopHandler).Methods("POST")
|
||||
|
||||
httpServer := &http.Server{Addr: ":" + port, Handler: router}
|
||||
httpServer := &http.Server{Addr: ":" + port, Handler: enableCors(router, corsList)}
|
||||
newServer := &Server{HttpServer: httpServer, Listener: listener}
|
||||
return newServer, nil
|
||||
|
||||
}
|
||||
|
||||
func enableCors(handler http.Handler, corsList string) http.Handler {
|
||||
corsArray := strings.Split(corsList, ",")
|
||||
fmt.Print(corsArray)
|
||||
c := cors.New(cors.Options{
|
||||
AllowedOrigins: corsArray,
|
||||
AllowCredentials: false,
|
||||
// Enable Debugging for testing, consider disabling in production
|
||||
Debug: false,
|
||||
})
|
||||
return c.Handler(handler)
|
||||
}
|
||||
func MetricServer(port string) (*Server, error) {
|
||||
network := networkTCP
|
||||
|
||||
|
||||
Reference in New Issue
Block a user