From 93dfe2be64e8658839bcfe5356adf35f8cde7075 Mon Sep 17 00:00:00 2001 From: Nicolas James Date: Thu, 13 Feb 2025 18:04:18 +1100 Subject: initial commit --- src/server/main.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/server/main.go (limited to 'src/server/main.go') diff --git a/src/server/main.go b/src/server/main.go new file mode 100644 index 0000000..126edeb --- /dev/null +++ b/src/server/main.go @@ -0,0 +1,42 @@ +package main + +import ( + "log" + "net/http" + "time" + + "server/database" + "server/handlers" +) + +func main() { + const database_path string = "./react_go.db" + const address string = "0.0.0.0" + const port string = "80" + + database.Init(database_path) + defer database.Close() + log.Printf("database initialised at %s\n", database_path) + + mux := http.NewServeMux() + spa := spaHandler{staticPath: "./dist"} + mux.Handle("/", spa) + mux.HandleFunc("/api/login", handlers.Login) + mux.HandleFunc("/api/logout", handlers.Logout) + mux.HandleFunc("/api/signup", handlers.Signup) + mux.HandleFunc("/api/refresh", handlers.Refresh) + mux.HandleFunc("/api/posts", handlers.Posts) + mux.HandleFunc("/api/post", handlers.Post) + mux.HandleFunc("/image/", handlers.Image) + + server := &http.Server{ + Addr: address + ":" + port, + Handler: mux, + /* This is good practice: https://github.com/gorilla/mux */ + WriteTimeout: 15 * time.Second, + ReadTimeout: 15 * time.Second, + } + + log.Printf("server listening on %s:%s\n", address, port) + log.Fatal(server.ListenAndServe()) +} -- cgit v1.2.3