diff --git a/Dockerfile b/Dockerfile index 8b28c44..2f55657 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,7 +5,7 @@ COPY go.mod go.sum ./ RUN go mod download COPY . . COPY cmd/main.go ./cmd -RUN CGO_ENABLED=0 GOOS=linux go build -ldflags "-X main.gitCommit=$(git rev-list -1 HEAD)" -o /go/bin/main ./cmd/main.go +RUN CGO_ENABLED=0 GOOS=linux go build -ldflags "-X main.gitCommit=$(git rev-list -1 HEAD)" -o /go/bin/torch ./cmd/main.go FROM alpine:latest WORKDIR / diff --git a/Makefile b/Makefile index a66aeef..d595b74 100644 --- a/Makefile +++ b/Makefile @@ -30,7 +30,7 @@ docker_build: docker build -f Dockerfile -t ${PROJECT_NAME} -t ${PROJECT_NAME}:latest . docker_build_local_push: - GOOS=linux go build -o ./torch ./cmd/main.go + #GOOS=linux go build -o ./torch ./cmd/main.go docker build -f Dockerfile -t ${PROJECT_NAME} . docker tag ${PROJECT_NAME}:latest localhost:5000/${REPOSITORY_NAME}:latest docker push localhost:5000/${REPOSITORY_NAME}:latest @@ -39,7 +39,7 @@ docker_run: docker run -p 8080:8080 ${PROJECT_NAME}:latest kubectl_apply: - kubectl delete -f ./deployment.yaml ;\ - kubectl apply -f ./deployment.yaml + kubectl delete -f ./deployment/deployment.yaml ;\ + kubectl apply -f ./deployment/deployment.yaml -kubectl_deploy: docker_build kubectl_apply +kubectl_deploy: docker_build_local_push kubectl_apply diff --git a/cmd/main.go b/cmd/main.go index 6493b05..9330924 100755 --- a/cmd/main.go +++ b/cmd/main.go @@ -3,12 +3,10 @@ package main import ( "flag" "io/ioutil" - "net/http" "github.com/jrmanes/torch/config" handlers "github.com/jrmanes/torch/pkg/http" - "github.com/gorilla/mux" "github.com/jrmanes/torch/pkg/k8s" log "github.com/sirupsen/logrus" "gopkg.in/yaml.v2" @@ -49,32 +47,6 @@ func main() { // Parse the command-line flags and read the configuration file log.Info("Running on namespace: ", k8s.GetCurrentNamespace()) cfg = ParseFlags() - log.Info("Config File:\n", cfg) - httpPort := "8080" - - // Set up the HTTP server - r := mux.NewRouter() - r.Use(handlers.LogRequest) - r.HandleFunc("/config", func(w http.ResponseWriter, r *http.Request) { - handlers.GetConfig(w, r, cfg) - }).Methods("GET") - r.HandleFunc("/list", func(w http.ResponseWriter, r *http.Request) { - handlers.List(w, r, cfg) - }).Methods("GET") - r.HandleFunc("/gen", func(w http.ResponseWriter, r *http.Request) { - handlers.Gen(w, r, cfg) - }).Methods("POST") - r.HandleFunc("/genAll", func(w http.ResponseWriter, r *http.Request) { - handlers.GenAll(w, r, cfg) - }).Methods("POST") - - server := &http.Server{ - Addr: ":" + httpPort, - Handler: r, - } - - // Start the HTTP server - log.Info("Server started on port ", httpPort) - log.Fatal(server.ListenAndServe()) + handlers.Run(cfg) } diff --git a/pkg/http/router.go b/pkg/http/router.go new file mode 100644 index 0000000..99f3bca --- /dev/null +++ b/pkg/http/router.go @@ -0,0 +1,26 @@ +package handlers + +import ( + "net/http" + + "github.com/gorilla/mux" + "github.com/jrmanes/torch/config" +) + +func Router(r *mux.Router, cfg config.MutualPeersConfig) *mux.Router { + r.Use(LogRequest) + r.HandleFunc("/config", func(w http.ResponseWriter, r *http.Request) { + GetConfig(w, r, cfg) + }).Methods("GET") + r.HandleFunc("/list", func(w http.ResponseWriter, r *http.Request) { + List(w, r, cfg) + }).Methods("GET") + r.HandleFunc("/gen", func(w http.ResponseWriter, r *http.Request) { + Gen(w, r, cfg) + }).Methods("POST") + r.HandleFunc("/genAll", func(w http.ResponseWriter, r *http.Request) { + GenAll(w, r, cfg) + }).Methods("POST") + + return r +} diff --git a/pkg/http/server.go b/pkg/http/server.go new file mode 100644 index 0000000..62bf440 --- /dev/null +++ b/pkg/http/server.go @@ -0,0 +1,56 @@ +package handlers + +import ( + "context" + "net/http" + "os" + "os/signal" + "syscall" + "time" + + "github.com/jrmanes/torch/config" + + "github.com/gorilla/mux" + log "github.com/sirupsen/logrus" +) + +func Run(cfg config.MutualPeersConfig) { + httpPort := "8080" + + // Set up the HTTP server + r := mux.NewRouter() + // Get the routers + r = Router(r, cfg) + // Use the middleware + r.Use(LogRequest) + + // Create the server + server := &http.Server{ + Addr: ":" + httpPort, + Handler: r, + } + + done := make(chan os.Signal, 1) + signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) + + go func() { + if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { + log.Error("Listening on: ", err) + } + }() + log.Info("Server Started...") + log.Info("Listening on port: " + httpPort) + + <-done + log.Info("Server Stopped") + + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer func() { + cancel() + }() + + if err := server.Shutdown(ctx); err != nil { + log.Error("Server Shutdown Failed: ", err) + } + log.Info("Server Exited Properly") +}