-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initial service and api impl for handling creation of TinyURLs (#8
) * feat: initial service and api impl for handling creation of TinyURLs * fix: rework the ci files to check for migrations only on pull_request and not on merge * fix: split jobs and fix names
- Loading branch information
1 parent
2d4e9bb
commit e5e5d9f
Showing
9 changed files
with
222 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
name: Migrations CI Jobs | ||
|
||
on: | ||
pull_request: | ||
branches: [ "main" ] | ||
|
||
jobs: | ||
migrations: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 0 | ||
|
||
- name: Find modified migrations | ||
run: | | ||
modified_migrations=$(git diff --name-only origin/$GITHUB_BASE_REF...origin/$GITHUB_HEAD_REF 'migrations/*.sql') | ||
echo "$modified_migrations" | ||
echo "::set-output name=file_names::$modified_migrations" | ||
id: modified-migrations | ||
- uses: sbdchd/squawk-action@v1 | ||
with: | ||
pattern: ${{ steps.modified-migrations.outputs.file_names }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package tiny | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/labstack/echo/v4" | ||
openapi_types "github.com/oapi-codegen/runtime/types" | ||
srvType "sanathk.com/tinyurl/pkg/api/services/v1/tiny" | ||
) | ||
|
||
type APIHandler struct { | ||
svc *Service | ||
} | ||
|
||
// TODO: potentially maintain this type on the OpenAPI Spec as it is for responding back to the API? | ||
type APIError struct { | ||
Message string `json:"message"` | ||
Timestamp string `json:"timestamp"` // in UTC | ||
} | ||
|
||
type Error struct { | ||
APIError | ||
} | ||
|
||
func (e Error) Error() string { | ||
return fmt.Sprintf("[%v] %s", e.Timestamp, e.Message) | ||
} | ||
|
||
func NewAPIHandler(ctx context.Context, svc *Service) (*APIHandler, error) { | ||
slog.Info("initializing a TinyURL API Handler") | ||
|
||
return &APIHandler{ | ||
svc: svc, | ||
}, nil | ||
} | ||
|
||
func (a *APIHandler) CreateTinyURL(ec echo.Context) error { | ||
slog.Info("handling a CreateTinyURL API request") | ||
body := srvType.TinyURLRequest{} | ||
|
||
err := ec.Bind(&body) | ||
if err != nil { | ||
slog.Error("could not process request body to create a TinyURL", slog.Any("error", err.Error())) | ||
ae := APIError{ | ||
Message: "could not process request body to create a TinyURL", | ||
Timestamp: time.Now().UTC().String(), | ||
} | ||
return ec.JSON( | ||
http.StatusBadRequest, Error{ae}, | ||
) | ||
} | ||
|
||
var resp TinyURLResponse | ||
req := TinyURLRequest{ | ||
Expiry: body.Expiry.Time, | ||
Original: body.Original, | ||
} | ||
resp, err = a.svc.CreateTinyURL(ec, req) | ||
if err != nil { | ||
slog.Error("could not complete creating TinyURL", slog.Any("error", err.Error())) | ||
ae := APIError{ | ||
// What actionable action can we provide to the user here? | ||
// As this error message by itself is not very useful | ||
Message: "could not complete creating TinyURL", | ||
Timestamp: time.Now().UTC().String(), | ||
} | ||
return ec.JSON( | ||
http.StatusInternalServerError, Error{ae}, | ||
) | ||
} | ||
|
||
return ec.JSON( | ||
http.StatusOK, srvType.TinyURLResponse{ | ||
Expiry: openapi_types.Date{Time: resp.Expiry}, | ||
Original: resp.Original, | ||
Tinyurl: resp.Tinyurl, | ||
}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package tiny | ||
|
||
import ( | ||
"context" | ||
"io/fs" | ||
"log/slog" | ||
"time" | ||
|
||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
type DatabaseInterface interface { | ||
Migrate(files fs.FS, path string) error | ||
} | ||
|
||
type TinyURLRequest struct { | ||
Expiry time.Time | ||
Original string | ||
} | ||
|
||
type TinyURLResponse struct { | ||
Expiry time.Time | ||
Original string | ||
Tinyurl string | ||
} | ||
|
||
type Service struct { | ||
db DatabaseInterface | ||
} | ||
|
||
func NewService(ctx context.Context, db DatabaseInterface) (*Service, error) { | ||
slog.Info("init TinyURL internal service") | ||
return &Service{ | ||
db: db, | ||
}, nil | ||
} | ||
|
||
func (s *Service) CreateTinyURL(ec echo.Context, req TinyURLRequest) (TinyURLResponse, error) { | ||
slog.Info("processing a TinyURL request") | ||
return TinyURLResponse{ | ||
Expiry: req.Expiry, | ||
Original: req.Original, | ||
Tinyurl: "sanathk.com/tinyurl123", | ||
}, nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package apiserver | ||
|
||
import ( | ||
"log/slog" | ||
|
||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
type EchoServer struct { | ||
*echo.Echo | ||
} | ||
|
||
func NewEchoServer() (*EchoServer, error) { | ||
slog.Info("creating an echo server") | ||
return &EchoServer{ | ||
echo.New(), | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters