an updated and hopefully faster version of the ST Toolbox
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

92 lines
3.9 KiB

package main
import (
"log"
"net/http"
"os"
"time"
root "marmic/servicetrade-toolbox"
"marmic/servicetrade-toolbox/internal/handlers/web"
"marmic/servicetrade-toolbox/internal/middleware"
"github.com/gorilla/mux"
)
func main() {
err := root.InitializeWebTemplates() // Note the change here
if err != nil {
log.Fatalf("Failed to initialize web templates: %v", err)
}
r := mux.NewRouter()
// Serve embedded static files
staticFS, err := root.GetStaticFS()
if err != nil {
log.Fatalf("Failed to get static filesystem: %v", err)
}
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.FS(staticFS))))
// Serve static files
// r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
// Auth routes
r.HandleFunc("/login", web.LoginHandler).Methods("GET", "POST")
r.HandleFunc("/logout", web.LogoutHandler).Methods("GET", "POST")
// Protected routes
protected := r.PathPrefix("/").Subrouter()
protected.Use(middleware.AuthMiddleware)
protected.HandleFunc("/", web.DashboardHandler).Methods("GET")
protected.HandleFunc("/jobs", web.JobsHandler).Methods("GET", "POST")
protected.HandleFunc("/invoices", web.InvoicesHandler).Methods("GET", "POST")
protected.HandleFunc("/ok-invoice/{id}", web.UpdateInvoiceStatusHandler).Methods("PUT")
protected.HandleFunc("/draft-invoice/{id}", web.UpdateInvoiceStatusHandler).Methods("PUT")
protected.HandleFunc("/failed-invoice/{id}", web.UpdateInvoiceStatusHandler).Methods("PUT")
protected.HandleFunc("/pending_accounting-invoice/{id}", web.UpdateInvoiceStatusHandler).Methods("PUT")
protected.HandleFunc("/processed-invoice/{id}", web.UpdateInvoiceStatusHandler).Methods("PUT")
protected.HandleFunc("/void-invoice/{id}", web.UpdateInvoiceStatusHandler).Methods("PUT")
protected.HandleFunc("/admin", web.AdminHandler).Methods("GET")
protected.HandleFunc("/assets", web.AssetsHandler).Methods("GET")
protected.HandleFunc("/companies", web.CompaniesHandler).Methods("GET")
protected.HandleFunc("/contacts", web.ContactsHandler).Methods("GET")
protected.HandleFunc("/contracts", web.ContractsHandler).Methods("GET")
protected.HandleFunc("/generic", web.GenericHandler).Methods("GET")
protected.HandleFunc("/locations", web.LocationsHandler).Methods("GET")
protected.HandleFunc("/notifications", web.NotificationsHandler).Methods("GET")
protected.HandleFunc("/quotes", web.QuotesHandler).Methods("GET")
protected.HandleFunc("/services", web.ServicesHandler).Methods("GET")
protected.HandleFunc("/tags", web.TagsHandler).Methods("GET")
protected.HandleFunc("/users", web.UsersHandler).Methods("GET")
// Document upload routes
protected.HandleFunc("/documents", web.DocumentsHandler).Methods("GET")
protected.HandleFunc("/process-csv", web.ProcessCSVHandler).Methods("POST")
protected.HandleFunc("/upload-documents", web.UploadDocumentsHandler).Methods("POST")
// Document removal routes
protected.HandleFunc("/documents/remove", web.DocumentRemoveHandler).Methods("GET")
protected.HandleFunc("/documents/remove/process-csv", web.ProcessRemoveCSVHandler).Methods("POST")
protected.HandleFunc("/documents/remove/job-selection", web.JobSelectionHandler).Methods("POST")
protected.HandleFunc("/documents/remove/job/{jobID}", web.GetJobAttachmentsHandler).Methods("GET")
protected.HandleFunc("/documents/remove/attachments/{jobID}", web.RemoveJobAttachmentsHandler).Methods("POST")
protected.HandleFunc("/documents/remove/bulk", web.BulkRemoveDocumentsHandler).Methods("POST")
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Println("Server starting on :" + port)
// Create a custom server with appropriate timeouts
server := &http.Server{
Addr: ":" + port,
Handler: r,
ReadTimeout: 30 * time.Minute, // Large timeout for big file uploads
WriteTimeout: 30 * time.Minute, // Large timeout for big file responses
IdleTimeout: 120 * time.Second, // How long to wait for the next request
}
log.Fatal(server.ListenAndServe())
}