diff --git a/cethttp/cethttp.go b/cethttp/cethttp.go index ce39549..bf83606 100644 --- a/cethttp/cethttp.go +++ b/cethttp/cethttp.go @@ -1,145 +1,41 @@ package cethttp import ( - "fmt" + "embed" + "io/fs" "log" "net/http" - "strconv" "sync" "github.com/gorilla/mux" - "github.com/senaduka/cetvorke/database" ) +//go:embed static/* +var staticFiles embed.FS + func StartServer(waitgroup *sync.WaitGroup) { defer waitgroup.Done() r := mux.NewRouter() + // Serve embedded static files + staticFS, err := fs.Sub(staticFiles, "static") + if err != nil { + log.Fatal("error:", err) + } + r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.FS(staticFS)))) + r.HandleFunc("/p/{postID}/{slug}", postHandler) r.HandleFunc("/", indexHandler) r.HandleFunc("/a/", archiveHandler) r.HandleFunc("/y/{year}/", yearHandler) r.HandleFunc("/y/{year}/m/{month}/", monthHandler) + r.HandleFunc("/editor/e/{postID}", editHandler) + r.HandleFunc("/editor/n/", newHandler) + r.HandeeFunc("/editor", editorHandler) - err := http.ListenAndServe(":8018", r) + err = http.ListenAndServe(":8018", r) if err != nil { log.Fatal("error:", err) } } - -func indexHandler(w http.ResponseWriter, r *http.Request) { - links, err := database.GetRecentLinks() - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - response := "
" + link.HTMLLink() + "
\n" - } - - response = html5Page(response) - - w.Write([]byte(response)) -} - -func postHandler(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - fmt.Println(vars) - postID, err := strconv.Atoi(vars["postID"]) - - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - post, err := database.GetPost(postID) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - response := post.HTMLPage() - - response = html5Page(response) - - w.Write([]byte(response)) -} - -func archiveHandler(w http.ResponseWriter, r *http.Request) { - links, title, err := database.GetAllYearLinks() - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - response := "" + link.HTMLArchiveLink() + "
\n" - } - - response = html5Page(response) - - w.Write([]byte(response)) -} - -func yearHandler(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - year, err := strconv.Atoi(vars["year"]) - - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - links, title, err := database.GetMonthLinksByYear(year) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - response := "" + link.HTMLArchiveMonthLink() + "
\n" - } - - response = html5Page(response) - - w.Write([]byte(response)) -} - -func monthHandler(w http.ResponseWriter, r *http.Request) { - vars := mux.Vars(r) - year, err := strconv.Atoi(vars["year"]) - - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - month, err := strconv.Atoi(vars["month"]) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - links, title, err := database.GetLinksByMonth(year, month) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - response := "" + link.HTMLLink() + "
\n" - } - - response = html5Page(response) - w.Write([]byte(response)) -} diff --git a/cethttp/editor.go b/cethttp/editor.go new file mode 100644 index 0000000..8e10285 --- /dev/null +++ b/cethttp/editor.go @@ -0,0 +1,16 @@ +package cethttp + +const editorPage = ` +