kleinTodo/main.go

59 lines
1.3 KiB
Go
Raw Normal View History

2025-07-26 23:31:00 +02:00
package main
import (
"fmt"
"gitea.kleinsense.nl/DariusKlein/kleinTodo/common"
"gitea.kleinsense.nl/DariusKlein/kleinTodo/handler"
"log"
"net/http"
"os"
"time"
)
func main() {
db, err := common.GetTodoDataStore()
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Create a new ServeMux to route requests.
mux := http.NewServeMux()
// Register handler for each endpoint.
mux.HandleFunc("POST /register", handler.RegisterHandler)
mux.HandleFunc("POST /login", handler.LoginHandler)
mux.HandleFunc("POST /store", handler.StoreHandler)
mux.HandleFunc("GET /sync", handler.SyncHandler)
// A simple root handler to confirm the server is running.
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
fmt.Fprintln(w, "API Server is running. Use the /register, /login, /store, or /sync endpoints.")
})
port := os.Getenv("SERVER_PORT")
if port == "" {
port = "8080"
}
// Configure the server.
server := &http.Server{
Addr: ":" + port,
Handler: mux,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
log.Printf("Server starting on port %s...", port)
// Start the server.
// log.Fatal will exit the application if the server fails to start.
if err := server.ListenAndServe(); err != nil {
log.Fatal(err)
}
}