summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2023-12-27 12:59:52 +0100
committerxengineering <me@xengineering.eu>2023-12-27 13:05:05 +0100
commit508aa4b7e4afa4da593021568afad1d565e8b968 (patch)
treed5d0cccc046a0c32b342b94c49bf242a86f06d52 /main.go
parent4721d69eb139e7da26f5454b80c39a4f1c8f1e23 (diff)
downloadceres-508aa4b7e4afa4da593021568afad1d565e8b968.tar
ceres-508aa4b7e4afa4da593021568afad1d565e8b968.tar.zst
ceres-508aa4b7e4afa4da593021568afad1d565e8b968.zip
Add HTTP server without handlers
This adds the infrastructure to add HTTP handlers. It already sets up the HTTP server and makes sure it is started and stopped.
Diffstat (limited to 'main.go')
-rw-r--r--main.go32
1 files changed, 32 insertions, 0 deletions
diff --git a/main.go b/main.go
index 5665d86..6b3a0a9 100644
--- a/main.go
+++ b/main.go
@@ -1,12 +1,16 @@
package main
import (
+ "context"
"log"
+ "net/http"
"os"
"os/signal"
"syscall"
"xengineering.eu/ceres/model"
+
+ "github.com/gorilla/mux"
)
func main() {
@@ -16,9 +20,37 @@ func main() {
model.InitDatabase()
defer model.CloseDatabase()
+ var srv *http.Server = startServer("127.0.0.1:8080")
+ go srv.ListenAndServe()
+ defer stopServer(srv)
+
listener := make(chan os.Signal)
signal.Notify(listener, syscall.SIGTERM)
signal.Notify(listener, syscall.SIGINT)
sig := <-listener
log.Printf("Cleaning up due to OS signal '%v'\n", sig)
}
+
+func startServer(addr string) *http.Server {
+ var r *mux.Router = mux.NewRouter()
+
+ muxer := http.NewServeMux()
+ muxer.Handle("/", r)
+
+ var srv http.Server
+ srv.Addr = addr
+ srv.Handler = muxer
+
+ log.Printf("Configured server to listen on http://%s\n", srv.Addr)
+
+ return &srv
+}
+
+func stopServer(srv *http.Server) {
+ var err error = srv.Shutdown(context.Background())
+ if err != nil {
+ log.Printf("Failed to shutdown HTTP server: %v\n", err)
+ } else {
+ log.Println("Stopped HTTP server")
+ }
+}