From e2f67996a608346ea3f3525ef2febf6ca5d2b78c Mon Sep 17 00:00:00 2001 From: sinanmohd Date: Thu, 4 Jul 2024 13:13:23 +0530 Subject: refactor: drop http api, move to sqlc/postgresql --- api/exampleReq.go | 57 --------------------------------------------------- api/login.go | 61 ------------------------------------------------------- api/main.go | 44 --------------------------------------- api/utils.go | 31 ---------------------------- 4 files changed, 193 deletions(-) delete mode 100644 api/exampleReq.go delete mode 100644 api/login.go delete mode 100644 api/main.go delete mode 100644 api/utils.go (limited to 'api') 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 = ` - - - - - 🚨 redq - - -
-

- redq is active -

-

- we're soo back 🥳 -

-
- - - ` - - 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)) -} -- cgit v1.2.3