Skip to content

Commit

Permalink
feat: fix dockerfile - split in server and router - clenaup main
Browse files Browse the repository at this point in the history
Signed-off-by: Jose Ramon Mañes <jose@celestia.org>
  • Loading branch information
tty47 committed Jun 22, 2023
1 parent 3d12c18 commit 8ca7289
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 /
Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
30 changes: 1 addition & 29 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
26 changes: 26 additions & 0 deletions pkg/http/router.go
Original file line number Diff line number Diff line change
@@ -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
}
56 changes: 56 additions & 0 deletions pkg/http/server.go
Original file line number Diff line number Diff line change
@@ -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")
}

0 comments on commit 8ca7289

Please sign in to comment.