-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fix dockerfile - split in server and router - clenaup main
Signed-off-by: Jose Ramon Mañes <jose@celestia.org>
- Loading branch information
Showing
5 changed files
with
88 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |