diff --git a/config.yml b/config.yml new file mode 100644 index 0000000..91db3ed --- /dev/null +++ b/config.yml @@ -0,0 +1,4 @@ +--- +listen: + address: 0.0.0.0 + port: 80 diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b0d7706 --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module git.bjphoster.com/source/yaskm + +go 1.22.2 + +require ( + github.com/gorilla/mux v1.8.1 + gopkg.in/yaml.v2 v2.4.0 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..215de0d --- /dev/null +++ b/go.sum @@ -0,0 +1,6 @@ +github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= +github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= diff --git a/main.go b/main.go new file mode 100644 index 0000000..8e3c0e4 --- /dev/null +++ b/main.go @@ -0,0 +1,43 @@ +package main + +import ( + "context" + "fmt" + "os" + "os/signal" + "syscall" + "time" +) + +var APP_VERSION string = "latest" +var COMMIT_ID string = "undefined" +var ws *WebServer + +func main() { + // Create a channel to receive the OS signals + sc := make(chan os.Signal, 1) + signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM) + + // Initialize the WebService structure + ws = new(WebServer) + ws.Initialize() + + // Start the WebService + go ws.Start() + + // Wait for a signal + <-sc + fmt.Println("Shutting down...") + + // Create a context with a timeout for graceful shutdown + shCtx, shCancel := context.WithTimeout(context.Background(), 5*time.Second) + defer shCancel() + + // Shutdown the HTTP server + err := ws.HTTPServer.Shutdown(shCtx) + if err != nil { + fmt.Printf("Server shutdown error: %s", err) + os.Exit(1) + } + +} diff --git a/routes.go b/routes.go new file mode 100644 index 0000000..eedf5b6 --- /dev/null +++ b/routes.go @@ -0,0 +1,7 @@ +package main + +import "github.com/gorilla/mux" + +func (s *WebServer) Routes(r *mux.Router) { + r.HandleFunc("/version", handleVersion).Methods("GET") +} diff --git a/templates/html/version.html b/templates/html/version.html new file mode 100644 index 0000000..e4f30ee --- /dev/null +++ b/templates/html/version.html @@ -0,0 +1,12 @@ + + + + {{.Name}} + + +

+ Version: {{.Version}}
+ Commit ID: {{.CommitId}} +

+ + diff --git a/type_webserver.go b/type_webserver.go new file mode 100644 index 0000000..51bc643 --- /dev/null +++ b/type_webserver.go @@ -0,0 +1,67 @@ +package main + +import ( + "fmt" + "net/http" + "os" + + "github.com/gorilla/mux" + "gopkg.in/yaml.v2" +) + +type WebServer struct { + HTTPServer *http.Server + Listen WSListen `yaml:"listen"` +} + +type WSListen struct { + Address string `yaml:"address"` + Port string `yaml:"port"` +} + +func (s *WebServer) Initialize() { + // Initialize default values + s.Listen = WSListen{ + Address: "0.0.0.0", + Port: "80", + } + + // Attempt to read the config file + configFile, err := os.ReadFile("config.yml") + if err != nil { + if os.IsNotExist(err) { + // File does not exist, log and use default config + fmt.Println("Config file not found, using default settings.") + } else { + // Some other error occurred when trying to read the file, exit + fmt.Println("Error reading config file:", err) + os.Exit(1) + } + } else { + // If the file exists, unmarshal it into the ServiceSettings struct + err = yaml.Unmarshal(configFile, &s) + if err != nil { + fmt.Println("Error parsing config file:", err) + os.Exit(1) + } + } +} + +func (s *WebServer) Start() error { + // Create a new MUX router and an HTTP server + r := mux.NewRouter() + s.HTTPServer = &http.Server{ + Addr: s.Listen.Address + ":" + s.Listen.Port, + Handler: r, + } + + // Associate the various handlers (routes) + s.Routes(r) + + // Start the server + fmt.Println("Listening on", s.Listen.Address+":"+s.Listen.Port) + err := s.HTTPServer.ListenAndServe() + + // Return error, or nil + return err +} diff --git a/version.go b/version.go new file mode 100644 index 0000000..7383cf4 --- /dev/null +++ b/version.go @@ -0,0 +1,26 @@ +package main + +import ( + _ "embed" + "html/template" + "net/http" +) + +//go:embed templates/html/version.html +var versionTemplate string + +func handleVersion(w http.ResponseWriter, r *http.Request) { + type SiteInfo struct { + CommitId string + Name string + Version string + } + + tmpl, _ := template.New("version.html").Parse(versionTemplate) + // Return (write) the version to the response body + tmpl.Execute(w, SiteInfo{ + CommitId: COMMIT_ID, + Name: "YASKM", + Version: APP_VERSION, + }) +}