package main import ( "database/sql" "encoding/json" "fmt" "html/template" "log" "net/http" "os" "path/filepath" "threadr/handlers" "github.com/gorilla/sessions" _ "github.com/go-sql-driver/mysql" ) func loadConfig(filename string) (*handlers.Config, error) { file, err := os.Open(filename) if err != nil { return nil, err } defer file.Close() var config handlers.Config err = json.NewDecoder(file).Decode(&config) return &config, err } func main() { config, err := loadConfig("config/config.json") if err != nil { log.Fatal("Error loading config:", err) } dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s", config.DBUsername, config.DBPassword, config.DBServerHost, config.DBDatabase) db, err := sql.Open("mysql", dsn) if err != nil { log.Fatal("Error connecting to database:", err) } defer db.Close() dir, err := os.Getwd() if err != nil { log.Fatal("Error getting working directory:", err) } // Parse partial templates tmpl := template.Must(template.ParseFiles( filepath.Join(dir, "templates/partials/navbar.html"), filepath.Join(dir, "templates/partials/cookie_banner.html"), )) // Parse page-specific templates with unique names tmpl, err = tmpl.ParseFiles( filepath.Join(dir, "templates/pages/about.html"), filepath.Join(dir, "templates/pages/board.html"), filepath.Join(dir, "templates/pages/boards.html"), filepath.Join(dir, "templates/pages/home.html"), filepath.Join(dir, "templates/pages/login.html"), filepath.Join(dir, "templates/pages/news.html"), filepath.Join(dir, "templates/pages/profile.html"), filepath.Join(dir, "templates/pages/profile_edit.html"), filepath.Join(dir, "templates/pages/signup.html"), filepath.Join(dir, "templates/pages/thread.html"), filepath.Join(dir, "templates/pages/userhome.html"), ) if err != nil { log.Fatal("Error parsing page templates:", err) } store := sessions.NewCookieStore([]byte("secret-key")) // Replace with secure key in production app := &handlers.App{ DB: db, Store: store, Config: config, Tmpl: tmpl, } fs := http.FileServer(http.Dir("static")) http.Handle(config.ThreadrDir+"/static/", http.StripPrefix(config.ThreadrDir+"/static/", fs)) http.HandleFunc(config.ThreadrDir+"/", app.SessionMW(handlers.HomeHandler(app))) http.HandleFunc(config.ThreadrDir+"/login/", app.SessionMW(handlers.LoginHandler(app))) http.HandleFunc(config.ThreadrDir+"/logout/", app.SessionMW(handlers.LogoutHandler(app))) http.HandleFunc(config.ThreadrDir+"/userhome/", app.SessionMW(app.RequireLoginMW(handlers.UserHomeHandler(app)))) http.HandleFunc(config.ThreadrDir+"/boards/", app.SessionMW(handlers.BoardsHandler(app))) http.HandleFunc(config.ThreadrDir+"/board/", app.SessionMW(handlers.BoardHandler(app))) http.HandleFunc(config.ThreadrDir+"/thread/", app.SessionMW(handlers.ThreadHandler(app))) http.HandleFunc(config.ThreadrDir+"/about/", app.SessionMW(handlers.AboutHandler(app))) http.HandleFunc(config.ThreadrDir+"/profile/", app.SessionMW(app.RequireLoginMW(handlers.ProfileHandler(app)))) http.HandleFunc(config.ThreadrDir+"/profile/edit/", app.SessionMW(app.RequireLoginMW(handlers.ProfileEditHandler(app)))) http.HandleFunc(config.ThreadrDir+"/like/", app.SessionMW(app.RequireLoginMW(handlers.LikeHandler(app)))) http.HandleFunc(config.ThreadrDir+"/news/", app.SessionMW(handlers.NewsHandler(app))) http.HandleFunc(config.ThreadrDir+"/signup/", app.SessionMW(handlers.SignupHandler(app))) http.HandleFunc(config.ThreadrDir+"/accept_cookie/", app.SessionMW(handlers.AcceptCookieHandler(app))) log.Println("Server starting on :8080") log.Fatal(http.ListenAndServe(":8080", nil)) }