package main import ( "TheAdversary/api" "TheAdversary/config" "TheAdversary/database" "TheAdversary/server" "fmt" "github.com/gorilla/mux" "html/template" "net/http" "path/filepath" ) func main() { r := mux.NewRouter() r.StrictSlash(true) setupApi(r) setupFrontend(r) db, err := database.NewSqlite3Connection(config.DatabaseFile) if err != nil { panic(err) } database.SetGlobDB(db) if err := http.ListenAndServe(fmt.Sprintf("%s:%s", config.ServerURL, config.ServerPort), r); err != nil { panic(err) } } func setupApi(r *mux.Router) { r.HandleFunc("/api/login", api.Login).Methods(http.MethodPost) r.HandleFunc("/api/authors", api.Authors).Methods(http.MethodGet) r.HandleFunc("/api/tags", api.Tags).Methods(http.MethodGet) r.HandleFunc("/api/recent", api.Recent).Methods(http.MethodGet) r.HandleFunc("/api/search", api.Search).Methods(http.MethodGet) r.HandleFunc("/api/article", api.Article).Methods(http.MethodPost, http.MethodPatch, http.MethodDelete) r.HandleFunc("/api/assets", api.Assets).Methods(http.MethodGet, http.MethodPost, http.MethodDelete) } func setupFrontend(r *mux.Router) { r.HandleFunc("/article/{article}", server.Article).Methods(http.MethodGet) r.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, filepath.Join(config.FrontendDir, "img", "logodark.svg")) }) r.PathPrefix("/sass/").HandlerFunc(server.ServePath) r.PathPrefix("/img/").HandlerFunc(server.ServePath) landingpage := template.Must(template.ParseFiles(filepath.Join(config.FrontendDir, "html", "landingpage.gohtml"))) r.PathPrefix("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/" { landingpage.Execute(w, config.PageBase) } else { server.Error404(w, r) } }) }