From 16a60795670e48b06e36f73a5a890edbb4491d19 Mon Sep 17 00:00:00 2001 From: Kumar Damani Date: Wed, 10 Jun 2020 06:53:46 -0400 Subject: initial commit --- src/app.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/app.go (limited to 'src/app.go') diff --git a/src/app.go b/src/app.go new file mode 100644 index 0000000..0581bfb --- /dev/null +++ b/src/app.go @@ -0,0 +1,50 @@ +package main + +import ( + "app/common" + "app/jobCheckin" + "log" + "net/http" + "os" +) + +func pingHandler(w http.ResponseWriter, r *http.Request) { + common.JsonRespond(&w, http.StatusOK, "pong") + return +} + +func main() { + // define urls being served + pingEndpoint := "ping" + checkinEndpoint := "check-in" + + // create mux + mux := http.NewServeMux() + + // define handlers for endpoints here + mux.HandleFunc("/"+pingEndpoint, pingHandler) + mux.HandleFunc("/"+pingEndpoint+"/", pingHandler) + + // check-in handled by jobCheckin package + jobCheckinHandler := jobCheckin.MakeHTTPHandler(checkinEndpoint) + mux.Handle("/"+checkinEndpoint, jobCheckinHandler) + mux.Handle("/"+checkinEndpoint+"/", jobCheckinHandler) + + // add more routes... eg. /foo handled by foo package + // ... + + log.Println("Starting Restful services...") + log.Println("Using port:", os.Getenv("HTTP_LISTEN_PORT")) + + // start listening + err := http.ListenAndServe(":"+os.Getenv("HTTP_LISTEN_PORT"), mux) + log.Print(err) + errorHandler(err) +} + +func errorHandler(err error) { + if err != nil { + log.Println(err) + os.Exit(1) + } +} -- cgit v1.2.3