-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
make shorten api #5
base: master
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package web | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gorilla/context" | ||
"github.com/scoville/scvl/src/domain" | ||
) | ||
|
||
func (web *Web) publishAPIKeyHandler(w http.ResponseWriter, r *http.Request) { | ||
user, ok := context.Get(r, "user").(*domain.User) | ||
if !ok { | ||
http.Error(w, "Unauthorized", http.StatusUnauthorized) | ||
return | ||
} | ||
err := web.engine.UpdateUserAPIKey(user.ID) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
http.Redirect(w, r, "/pages", http.StatusSeeOther) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,8 @@ func (web *Web) Start(port string) error { | |
r.Handle("/", web.authenticate(web.rootHandler)).Methods(http.MethodGet) | ||
r.Handle("/logout", web.authenticate(web.logoutHandler)).Methods(http.MethodPost) | ||
r.Handle("/shorten", web.authenticate(web.shortenHandler)).Methods(http.MethodPost) | ||
r.HandleFunc("/api/shorten", web.shortenByAPIHandler).Methods(http.MethodPost) | ||
r.Handle("/api/publish", web.authenticate(web.publishAPIKeyHandler)).Methods(http.MethodPost) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. path名がこれだと(keyではなく)apiをpublishすることになりそう。 |
||
r.Handle("/pages", web.authenticate(web.pagesHandler)).Methods(http.MethodGet) | ||
r.Handle("/files", web.authenticate(web.filesHandler)).Methods(http.MethodGet) | ||
r.Handle("/files", web.authenticate(web.fileUploadHandler)).Methods(http.MethodPost) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,6 +90,40 @@ func (e *Engine) Shorten(req *ShortenRequest) (page *domain.Page, err error) { | |
return | ||
} | ||
|
||
// ShortenByAPIRequest is the request struct for ShortenByAPI function | ||
type ShortenByAPIRequest struct { | ||
URL string | ||
APIKey string | ||
} | ||
|
||
// ShortenByAPI shorten url | ||
func (e *Engine) ShortenByAPI(req *ShortenByAPIRequest) (page *domain.Page, err error) { | ||
if req.URL == "" { | ||
err = errors.New("url cannot be empty") | ||
return | ||
} | ||
if req.APIKey == "" { | ||
err = errors.New("api_key cannot be empty") | ||
return | ||
} | ||
_, err = e.sqlClient.FindUserByAPIKey(req.APIKey) | ||
if err != nil { | ||
return | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if _, err = e.sqlClient.FindUserByAPIKey(req.APIKey); err != nil { |
||
|
||
page = &domain.Page{ | ||
Slug: domain.GenerateSlug(5), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2箇所以上で使うんだったら定数を定義するのもあり。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. どの部分のことですか? |
||
URL: req.URL, | ||
} | ||
err = e.sqlClient.CreatePage(page) | ||
if err != nil { | ||
return | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if err = e.sqlClient.CreatePage(page); err != nil { |
||
|
||
e.redisClient.SetURL(page.Slug, page.URL) | ||
return | ||
} | ||
|
||
// AccessRequest is the request struct for Access function | ||
type AccessRequest struct { | ||
Slug string | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
delete