diff options
author | sinanmohd <sinan@sinanmohd.com> | 2024-07-04 13:13:23 +0530 |
---|---|---|
committer | sinanmohd <sinan@sinanmohd.com> | 2024-07-05 17:02:06 +0530 |
commit | e2f67996a608346ea3f3525ef2febf6ca5d2b78c (patch) | |
tree | 932f766b65ed5a6ff3d57c772292a2b75210d9aa /api | |
parent | e309091d17720b69c53172a41c0ea45ad7b66911 (diff) |
refactor: drop http api, move to sqlc/postgresql
Diffstat (limited to 'api')
-rw-r--r-- | api/exampleReq.go | 57 | ||||
-rw-r--r-- | api/login.go | 61 | ||||
-rw-r--r-- | api/main.go | 44 | ||||
-rw-r--r-- | api/utils.go | 31 |
4 files changed, 0 insertions, 193 deletions
diff --git a/api/exampleReq.go b/api/exampleReq.go deleted file mode 100644 index 54454a1..0000000 --- a/api/exampleReq.go +++ /dev/null @@ -1,57 +0,0 @@ -package api - -import ( - "encoding/json" - "fmt" - "net/http" - - redqdb "sinanmohd.com/redq/db" -) - -type examplApiName struct { - db *redqdb.SafeDB - req *RequestApiName - resp *ResponseApiName -} - -type RequestApiName struct { - BearerToken string -} - -type ResponseApiName struct { - Bearer *redqdb.Bearer -} - -func newExamplApiName(db *redqdb.SafeDB) *examplApiName { - a := &examplApiName{} - a.db = db - - return a -} - -func (a *examplApiName) ServeHTTP(rw http.ResponseWriter, r *http.Request) { - a.req = &RequestApiName{} - a.resp = &ResponseApiName{} - a.resp.Bearer = &redqdb.Bearer{} - - err := unmarshal(r.Body, a.req) - fmt.Println(a.req) - if err != nil { - handleError(err, rw, http.StatusUnprocessableEntity) - return - } - - err = a.resp.Bearer.VerifyAndUpdate(a.db, a.req.BearerToken) - if err != nil { - handleError(err, rw, http.StatusUnauthorized) - return - } - - json, err := json.Marshal(a.resp) - if err != nil { - handleError(err, rw, http.StatusInternalServerError) - return - } - - rw.Write(json) -} diff --git a/api/login.go b/api/login.go deleted file mode 100644 index ef43304..0000000 --- a/api/login.go +++ /dev/null @@ -1,61 +0,0 @@ -package api - -import ( - "encoding/json" - "net/http" - - "github.com/go-playground/validator/v10" - redqdb "sinanmohd.com/redq/db" -) - -type loginAPI struct { - db *redqdb.SafeDB - validate *validator.Validate - req *RequestLogin - resp *ResponseLogin -} - -type RequestLogin struct { - Account *redqdb.Account `validate:"required"` -} - -type ResponseLogin struct { - Account *redqdb.Account -} - -func newLogin(db *redqdb.SafeDB) *loginAPI { - a := &loginAPI{} - a.db = db - a.validate = validator.New(validator.WithRequiredStructEnabled()) - - 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 { - err = a.validate.Struct(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 deleted file mode 100644 index c4645b8..0000000 --- a/api/main.go +++ /dev/null @@ -1,44 +0,0 @@ -package api - -import ( - "fmt" - "net/http" - - redqdb "sinanmohd.com/redq/db" -) - -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) - - http.HandleFunc("GET /{$}", home) - http.ListenAndServe(":8008", nil) -} - -func home(rw http.ResponseWriter, r *http.Request) { - const index string = ` - <!DOCTYPE html> - <html lang="en"> - <head> - <meta charset="UTF-8"> - <title>🚨 redq</title> - </head> - <body> - <center> - <h1 style="font-size: 10em"> - redq is active - </h1> - <p style="font-weight: bold"> - we're soo back 🥳 - </p> - </center> - </body> - </html> - ` - - fmt.Fprint(rw, index) -} diff --git a/api/utils.go b/api/utils.go deleted file mode 100644 index 0fc7d35..0000000 --- a/api/utils.go +++ /dev/null @@ -1,31 +0,0 @@ -package api - -import ( - "encoding/json" - "fmt" - "io" - "log" - "net/http" -) - -func unmarshal(r io.Reader, v any) error { - body, err := io.ReadAll(r) - if err != nil { - return err - } - - err = json.Unmarshal(body, v) - if err != nil { - return err - } - - return nil -} - -func handleError(err error, rw http.ResponseWriter, status int) { - log.Println(err) - - rw.WriteHeader(status) - json := fmt.Sprintf(`{"Error": "%v"}`, http.StatusText(status)) - rw.Write([]byte(json)) -} |