summaryrefslogtreecommitdiff
path: root/api
diff options
context:
space:
mode:
authorsinanmohd <sinan@sinanmohd.com>2024-03-16 10:35:18 +0530
committersinanmohd <sinan@sinanmohd.com>2024-03-16 10:35:18 +0530
commitfb387fe290d907dd8a1b0e3600c5468043071628 (patch)
tree228430b4d95ea2582c1d31bff49de6b59dbbe946 /api
parent8fd42493709792ffe85f519c75b085ed65123baa (diff)
api/login: init
Diffstat (limited to 'api')
-rw-r--r--api/login.go54
-rw-r--r--api/main.go2
2 files changed, 56 insertions, 0 deletions
diff --git a/api/login.go b/api/login.go
new file mode 100644
index 0000000..ef2195e
--- /dev/null
+++ b/api/login.go
@@ -0,0 +1,54 @@
+package api
+
+import (
+ "encoding/json"
+ "net/http"
+
+ redqdb "sinanmohd.com/redq/db"
+)
+
+type loginAPI struct {
+ db *redqdb.SafeDB
+ req *RequestLogin
+ resp *ResponseLogin
+}
+
+type RequestLogin struct {
+ Account *redqdb.Account
+}
+
+type ResponseLogin struct {
+ Account *redqdb.Account
+}
+
+func newLogin(db *redqdb.SafeDB) *loginAPI {
+ a := &loginAPI{}
+ a.db = db
+
+ return a
+}
+
+func (a *loginAPI) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
+ a.req = &RequestLogin{}
+ a.resp = &ResponseLogin{}
+ err := unmarshal(r.Body, a.req)
+ if err != nil {
+ handleError(err, rw, http.StatusUnprocessableEntity)
+ return
+ }
+
+ err = a.req.Account.Login(a.db)
+ if err != nil {
+ handleError(err, rw, http.StatusUnauthorized)
+ return
+ }
+ a.resp.Account = a.req.Account
+
+ json, err := json.Marshal(a.resp)
+ if err != nil {
+ handleError(err, rw, http.StatusInternalServerError)
+ return
+ }
+
+ rw.Write(json)
+}
diff --git a/api/main.go b/api/main.go
index 29f71c9..c4645b8 100644
--- a/api/main.go
+++ b/api/main.go
@@ -10,6 +10,8 @@ import (
func Run(db *redqdb.SafeDB) {
const prefix string = "POST /_redq/api"
+ login := newLogin(db)
+ http.Handle(prefix+"/ac/login", login)
exampleApi := newExamplApiName(db)
http.Handle(prefix+"/example", exampleApi)