From b9d7eb05beb57926181f91685a75b3069d1f8cf8 Mon Sep 17 00:00:00 2001 From: r Date: Fri, 27 Dec 2019 08:06:43 +0000 Subject: Add settings page --- service/transport.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'service/transport.go') diff --git a/service/transport.go b/service/transport.go index c42462f..3ab2546 100644 --- a/service/transport.go +++ b/service/transport.go @@ -7,6 +7,7 @@ import ( "net/http" "path" "strconv" + "web/model" "github.com/gorilla/mux" ) @@ -305,6 +306,36 @@ func NewHandler(s Service, staticDir string) http.Handler { } }).Methods(http.MethodGet) + r.HandleFunc("/settings", func(w http.ResponseWriter, req *http.Request) { + ctx := getContextWithSession(context.Background(), req) + + err := s.ServeSettingsPage(ctx, w, nil) + if err != nil { + s.ServeErrorPage(ctx, w, err) + return + } + }).Methods(http.MethodGet) + + r.HandleFunc("/settings", func(w http.ResponseWriter, req *http.Request) { + ctx := getContextWithSession(context.Background(), req) + + visibility := req.FormValue("visibility") + copyScope := req.FormValue("copy_scope") == "true" + settings := &model.Settings{ + DefaultVisibility: visibility, + CopyScope: copyScope, + } + + err := s.SaveSettings(ctx, w, nil, settings) + if err != nil { + s.ServeErrorPage(ctx, w, err) + return + } + + w.Header().Add("Location", req.Header.Get("Referer")) + w.WriteHeader(http.StatusFound) + }).Methods(http.MethodPost) + r.HandleFunc("/signout", func(w http.ResponseWriter, req *http.Request) { // TODO remove session from database w.Header().Add("Set-Cookie", fmt.Sprintf("session_id=;max-age=0")) -- cgit v1.2.3