aboutsummaryrefslogtreecommitdiff
path: root/cmd/web/handlers
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/web/handlers')
-rw-r--r--cmd/web/handlers/blog.go34
-rw-r--r--cmd/web/handlers/fs.go22
-rw-r--r--cmd/web/handlers/login.go26
-rw-r--r--cmd/web/handlers/routes.go18
4 files changed, 52 insertions, 48 deletions
diff --git a/cmd/web/handlers/blog.go b/cmd/web/handlers/blog.go
index 8d9a811..14a0abe 100644
--- a/cmd/web/handlers/blog.go
+++ b/cmd/web/handlers/blog.go
@@ -16,14 +16,14 @@ import (
)
type blogContext struct {
- err *log.Logger
- db *sql.DB
+ err *log.Logger
+ db *sql.DB
- Post models.Post
- Rows []models.Post
- Name string
- IsAuth bool
- Offset int
+ Post models.Post
+ Rows []models.Post
+ Name string
+ IsAuth bool
+ Offset int
}
func (ctx *blogContext) viewPost(w http.ResponseWriter, r *http.Request) {
@@ -56,7 +56,7 @@ func (ctx *blogContext) viewPost(w http.ResponseWriter, r *http.Request) {
}
p.FormattedTime = p.Time.Format(time.ANSIC)
- ctx.Post = p
+ ctx.Post = p
files := []string{
"ui/html/base.tmpl.html",
@@ -113,7 +113,7 @@ func (ctx *blogContext) post(w http.ResponseWriter, r *http.Request) {
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Internal Error"))
- return;
+ return
}
err = r.ParseMultipartForm(4 << 20)
@@ -129,7 +129,7 @@ func (ctx *blogContext) post(w http.ResponseWriter, r *http.Request) {
writer := multipart.NewWriter(&buffer)
writer.SetBoundary(boundary)
writer.WriteField("user_id", strconv.Itoa(int(userId)))
-
+
part, err := writer.CreateFormField("raw")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
@@ -137,7 +137,7 @@ func (ctx *blogContext) post(w http.ResponseWriter, r *http.Request) {
ctx.err.Printf("Could not create form field: %v\n", err)
return
}
-
+
_, err = part.Write([]byte(r.Form.Get("raw")))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
@@ -148,11 +148,11 @@ func (ctx *blogContext) post(w http.ResponseWriter, r *http.Request) {
writer.Close()
- proxyReq, err := http.NewRequest(r.Method, "http://127.0.0.1:" + os.Getenv("parser_port"), bytes.NewReader(buffer.Bytes()))
+ proxyReq, err := http.NewRequest(r.Method, "http://127.0.0.1:"+os.Getenv("parser_port"), bytes.NewReader(buffer.Bytes()))
proxyReq.Header = make(http.Header)
for key, val := range r.Header {
if key != "Content-Length" {
- proxyReq.Header[key] = val;
+ proxyReq.Header[key] = val
}
}
@@ -190,10 +190,10 @@ func (ctx *blogContext) index(w http.ResponseWriter, r *http.Request) {
http.NotFound(w, r)
return
}
-
+
_, err := r.Cookie("paterissa_session_token")
if err == nil {
- ctx.IsAuth = true;
+ ctx.IsAuth = true
}
ctx.Offset, _ = strconv.Atoi(r.URL.Query().Get("offset"))
@@ -230,10 +230,10 @@ func (ctx *blogContext) index(w http.ResponseWriter, r *http.Request) {
funcMap := template.FuncMap{
"add": func(a int, b int) int {
- return a + b;
+ return a + b
},
"sub": func(a int, b int) int {
- return a - b;
+ return a - b
},
}
diff --git a/cmd/web/handlers/fs.go b/cmd/web/handlers/fs.go
index 96f11d0..1a68e35 100644
--- a/cmd/web/handlers/fs.go
+++ b/cmd/web/handlers/fs.go
@@ -9,9 +9,9 @@ import (
)
type fsContext struct {
- err *log.Logger
- path string
- contentType string
+ err *log.Logger
+ path string
+ contentType string
}
func (ctx *fsContext) readdir(w http.ResponseWriter, r *http.Request) {
@@ -45,14 +45,14 @@ func (ctx *fsContext) get(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", ctx.contentType)
} else {
switch filepath.Ext(name) {
- case ".css":
- w.Header().Set("Content-Type", "text/css")
- case ".js":
- w.Header().Set("Content-Type", "text/javascript")
- case ".svg":
- w.Header().Set("Content-Type", "image/svg+xml")
- case ".png":
- w.Header().Set("Content-Type", "image/png")
+ case ".css":
+ w.Header().Set("Content-Type", "text/css")
+ case ".js":
+ w.Header().Set("Content-Type", "text/javascript")
+ case ".svg":
+ w.Header().Set("Content-Type", "image/svg+xml")
+ case ".png":
+ w.Header().Set("Content-Type", "image/png")
}
}
diff --git a/cmd/web/handlers/login.go b/cmd/web/handlers/login.go
index f912977..3b1ee92 100644
--- a/cmd/web/handlers/login.go
+++ b/cmd/web/handlers/login.go
@@ -15,7 +15,7 @@ import (
type loginContext struct {
err *log.Logger
- db *sql.DB
+ db *sql.DB
}
func (ctx *loginContext) index(w http.ResponseWriter, r *http.Request) {
@@ -82,7 +82,7 @@ func (ctx *loginContext) login(w http.ResponseWriter, r *http.Request) {
ctx.err.Printf("Failed to retrieve user info from DB: %v\n", err)
return
}
-
+
passOneErr := bcrypt.CompareHashAndPassword([]byte(u.PassOne), []byte(passOne))
passTwoErr := bcrypt.CompareHashAndPassword([]byte(u.PassTwo), []byte(passTwo))
if passOneErr != nil || passTwoErr != nil {
@@ -92,13 +92,13 @@ func (ctx *loginContext) login(w http.ResponseWriter, r *http.Request) {
}
cookie := http.Cookie{
- Name: "paterissa_session_token",
- Value: uuid.New().String(),
- Expires: time.Now().AddDate(0, 0, 1),
- Path: "/",
- Domain: os.Getenv("serv"),
+ Name: "paterissa_session_token",
+ Value: uuid.New().String(),
+ Expires: time.Now().AddDate(0, 0, 1),
+ Path: "/",
+ Domain: os.Getenv("serv"),
HttpOnly: true,
- Secure: true,
+ Secure: true,
}
commit, err := ctx.db.Prepare("INSERT INTO cookies (content, user_id, expiration) VALUES ($1, $2, $3);")
@@ -147,11 +147,11 @@ func (ctx *loginContext) logout(w http.ResponseWriter, r *http.Request) {
}
cookie = &http.Cookie{
- Name: "paterissa_session_token",
- Value: "",
- Path: "/",
- Domain: os.Getenv("serv"),
- MaxAge: -1,
+ Name: "paterissa_session_token",
+ Value: "",
+ Path: "/",
+ Domain: os.Getenv("serv"),
+ MaxAge: -1,
HttpOnly: true,
}
diff --git a/cmd/web/handlers/routes.go b/cmd/web/handlers/routes.go
index a4c9c09..4b80615 100644
--- a/cmd/web/handlers/routes.go
+++ b/cmd/web/handlers/routes.go
@@ -11,25 +11,25 @@ import (
func RegisterEndpoints(app types.Application, db *sql.DB) *http.ServeMux {
auth := middleware.AuthMiddleware{
Err: app.Err,
- Db: db,
+ Db: db,
}
blog := blogContext{
- err: app.Err,
- db: db,
+ err: app.Err,
+ db: db,
Name: app.Env.Webmaster,
}
login := loginContext{
err: app.Err,
- db: db,
+ db: db,
}
audio := fsContext{
- err: app.Err,
- path: app.AudioDir,
+ err: app.Err,
+ path: app.AudioDir,
contentType: "audio/mpeg",
}
static := fsContext{
- err: app.Err,
+ err: app.Err,
path: "static",
}
@@ -46,5 +46,9 @@ func RegisterEndpoints(app types.Application, db *sql.DB) *http.ServeMux {
blogRouter.HandleFunc("/static/get", static.get)
+ blogRouter.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
+ http.ServeFile(w, r, "/static/favicon.ico")
+ })
+
return blogRouter
}