Todo/main.go

88 lines
2.9 KiB
Go
Raw Normal View History

2024-08-18 04:29:25 +00:00
package main
import (
"errors"
"flag"
"fmt"
"net/http"
"path"
2024-08-18 04:29:25 +00:00
"github.com/Cameron-Reed1/todo-web/api"
"github.com/Cameron-Reed1/todo-web/db"
"github.com/Cameron-Reed1/todo-web/pages"
)
func main() {
db_path := flag.String("db", "./main.db", "Path to the main sqlite3 database")
user_db_dir := flag.String("user-dbs", "./user_dbs", "Path to the directory containing per-user sqlite3 databases")
2024-08-18 04:29:25 +00:00
bind_port := flag.Int("p", 8080, "Port to bind to")
bind_addr := flag.String("a", "0.0.0.0", "Address to bind to")
static_dir := flag.String("static", "./static", "Path to static files")
2024-08-18 04:29:25 +00:00
noFront := flag.Bool("no-frontend", false, "Disable the frontend endpoints")
// a := false; noBack := &a // flag.Bool("no-backend", false, "Disable the backend endpoints") // This didn't really make sense
2024-08-18 04:29:25 +00:00
flag.Parse()
mux := http.NewServeMux()
if !*noFront {
addFrontendEndpoints(mux, *static_dir)
2024-08-18 04:29:25 +00:00
}
addBackendEndpoints(mux)
2024-08-18 04:29:25 +00:00
db.SetUserDBDir(*user_db_dir)
db.OpenMainDB(*db_path)
defer db.CloseMainDB()
2024-08-18 04:29:25 +00:00
addr := fmt.Sprintf("%s:%d", *bind_addr, *bind_port)
server := http.Server{ Addr: addr, Handler: mux }
fmt.Printf("Starting server on %s...\n", addr)
err := server.ListenAndServe()
if errors.Is(err, http.ErrServerClosed) {
fmt.Printf("Server closed\n")
} else if err != nil {
fmt.Printf("Error starting server: %s\n", err)
}
}
func addFrontendEndpoints(mux *http.ServeMux, static_path string) {
2024-08-18 04:29:25 +00:00
fmt.Println("Frontend enabled")
mux.HandleFunc("/", Error404)
mux.HandleFunc("/{$}", pages.RootPage)
mux.HandleFunc("/overdue", pages.OverdueFragment)
mux.HandleFunc("/today", pages.TodayFragment)
mux.HandleFunc("/upcoming", pages.UpcomingFragment)
mux.HandleFunc("/login", pages.Login)
mux.HandleFunc("/create-account", pages.CreateAccount)
// mux.HandleFunc("/account", pages.AccountPage)
mux.HandleFunc("POST /logout", pages.Logout)
2024-08-18 04:29:25 +00:00
mux.HandleFunc("DELETE /delete/{id}", pages.DeleteItem)
mux.HandleFunc("PATCH /set/{id}", pages.SetItemCompleted)
2024-08-20 05:26:47 +00:00
mux.HandleFunc("PUT /update", pages.UpdateItem)
2024-08-18 04:29:25 +00:00
mux.HandleFunc("POST /new", pages.CreateItem)
fileServer := http.FileServer(http.Dir(static_path))
2024-08-18 04:29:25 +00:00
mux.Handle("/css/", fileServer)
mux.Handle("/js/", fileServer)
mux.Handle("/img/", fileServer)
mux.HandleFunc("/img/login.jpg", func(w http.ResponseWriter, r *http.Request) { pages.RandomImage(w, r, path.Join(static_path, "img/login/")) })
2024-10-01 19:01:53 +00:00
mux.HandleFunc("/img/login/", Error404)
2024-08-18 04:29:25 +00:00
}
func addBackendEndpoints(mux *http.ServeMux) {
fmt.Println("Backend enabled")
mux.HandleFunc("/api/", api.InvalidEndpoint)
mux.HandleFunc("GET /api/get", api.GetAll)
mux.HandleFunc("GET /api/get/{id}", api.GetTodo)
mux.HandleFunc("POST /api/new", api.AddTodo)
}
func Error404(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Error 404: Page not found\n"))
}