diff options
| author | Alex <alex@scerba.org> | 2024-03-13 17:09:48 -0400 | 
|---|---|---|
| committer | Alex Scerba <alex@scerba.org> | 2024-10-29 13:20:01 -0400 | 
| commit | f97d476b495126fd2474b8e3a2968658395d2a0f (patch) | |
| tree | 866ce2531ea9ada192e01faeecd42fc02bc5b0c1 /cmd/http/handle.go | |
| parent | 99d92792daedc0501886edc7c4e4a9f3768f9b8a (diff) | |
Complete restructure
Diffstat (limited to 'cmd/http/handle.go')
| -rw-r--r-- | cmd/http/handle.go | 55 | 
1 files changed, 55 insertions, 0 deletions
diff --git a/cmd/http/handle.go b/cmd/http/handle.go new file mode 100644 index 0000000..10aed2c --- /dev/null +++ b/cmd/http/handle.go @@ -0,0 +1,55 @@ +package main + +import ( +	"net/http" +	"strings" +) + +func (app *application) home(w http.ResponseWriter, r *http.Request) { +	w.Header().Set("Content-Type", "text/html; charset=utf-8") + +	path := strings.Split(r.URL.Path, "/") +	if path[1] != "" { +		app.notFound(w) +		return +	} else { +		p, err := app.aggregate("html/projects") +		if err != nil { +			app.serverError(w, err) +			return +		} + +		err = renderTemplate(w, "index", p) +		if err != nil { +			app.serverError(w, err) +			return +		} +	} +} + +func (app *application) post(w http.ResponseWriter, r *http.Request) { +	w.Header().Set("Content-Type", "text/html; charset=utf-8") + +	path := strings.Split(r.URL.Path, "/") +	if len(path) > 4 { +		app.notFound(w) +	} else if len(path) == 4 && path[3] == "" { +		http.Redirect(w, r, "/"+path[1]+"/"+path[2], http.StatusFound) +	} else { +		post, err := app.readFile("html" + strings.TrimSuffix(r.URL.Path, "/") + ".tmpl.html") +		if err != nil { +			app.notFound(w) +			return +		} + +		var posts []*Post +		posts = append(posts, post) +		p := &Posts{Contents: posts} + +		err = renderTemplate(w, path[1]+"/"+path[2], p) +		if err != nil { +			app.serverError(w, err) +			return +		} +	} +}  | 
