Skip to content
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

APIv1: Start working on new REST API. Refactor logic in domains. #497

Merged
merged 61 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
c002f06
added 404 template
fmartingr Oct 9, 2022
92c24a2
added auth domain
fmartingr Oct 9, 2022
5ce8603
added embed file for frontend
fmartingr Oct 9, 2022
13ecc16
added base config and dependencies
fmartingr Oct 9, 2022
09d9a48
added basic new http server
fmartingr Oct 9, 2022
bf10fa7
added separated server command
fmartingr Oct 9, 2022
c97d620
updated go modules
fmartingr Oct 9, 2022
f9bf322
removed modd file
fmartingr Oct 9, 2022
29d0de4
Added shortcut to send internal server error response
fmartingr Oct 10, 2022
0fab419
Added JWT support to Auth Domain
fmartingr Oct 10, 2022
05aea62
Added JWT support to API
fmartingr Oct 10, 2022
b0c5953
docs: added comments to response struct
fmartingr Oct 12, 2022
6641e69
Merge remote-tracking branch 'origin/master' into feat/api-next
fmartingr Feb 6, 2023
2329620
naming
fmartingr Feb 6, 2023
c1bcd5f
inline returns
fmartingr Feb 6, 2023
ba1b092
updated dependencies
fmartingr Feb 7, 2023
6077016
production logger
fmartingr Feb 7, 2023
c45c75e
bookmarks endpoint
fmartingr Feb 7, 2023
11ccb5c
reverted old views api path
fmartingr Feb 9, 2023
339bff0
frontend for api v1
fmartingr Feb 9, 2023
f4833fb
proper 404 error (not working atm)
fmartingr Feb 9, 2023
ea4d816
use response
fmartingr Feb 9, 2023
bea7bae
removed 404 html
fmartingr Feb 9, 2023
af4deb9
server error handler
fmartingr Feb 9, 2023
e56502e
login and basic auth
fmartingr Feb 9, 2023
b875e7d
adjusted session duration
fmartingr Feb 18, 2023
83fef0d
properly retrieve tags
fmartingr Feb 18, 2023
1077e61
properly delete bookmark
fmartingr Feb 18, 2023
13942b4
cleanup
fmartingr Feb 18, 2023
0df5559
archiver domain
fmartingr Feb 18, 2023
299d057
debug routes
fmartingr Feb 18, 2023
a98ac56
bookmark routes
fmartingr Feb 18, 2023
36594de
expiration by parameter
fmartingr Feb 18, 2023
d5d612e
move to logrus
fmartingr Feb 26, 2023
011ae78
logout
fmartingr Feb 26, 2023
e4c951b
frontend cache
fmartingr Feb 26, 2023
774ac44
updated dependencies
fmartingr Feb 26, 2023
5c062cb
Merge remote-tracking branch 'origin/master' into feat/api-next
fmartingr Jun 11, 2023
db80349
Merge remote-tracking branch 'origin/master' into feat/api-next
fmartingr Jun 11, 2023
873a6ea
Merge remote-tracking branch 'origin/master' into feat/api-next
fmartingr Jul 2, 2023
13ed12f
http: migrated to gin
fmartingr Jul 3, 2023
4a4b3fb
linted
fmartingr Jul 3, 2023
d5b429a
Added version command
fmartingr Jul 3, 2023
286722c
unit tests, docs
fmartingr Jul 4, 2023
88e054e
response test utils and tests
fmartingr Jul 4, 2023
a093ac1
remove logout handler
fmartingr Jul 4, 2023
e26aac2
auth
fmartingr Jul 4, 2023
fd8e5b8
createtag
fmartingr Jul 4, 2023
7268783
improved http test utilities
fmartingr Jul 8, 2023
1767229
assert message equals
fmartingr Jul 9, 2023
cc69aa1
Merge branch 'master' into feat/api-next
fmartingr Jul 9, 2023
1892038
Remove 1.19 from test matrix
fmartingr Jul 12, 2023
2d9c821
moved api to v1 folder
fmartingr Jul 14, 2023
dfa13e6
docs: contribute docs
fmartingr Jul 14, 2023
02a91ed
updated makefile
fmartingr Jul 14, 2023
e278d82
updated usage docs
fmartingr Jul 14, 2023
3794998
warn in server command
fmartingr Jul 14, 2023
765683e
updaed docs with shiori version command
fmartingr Jul 14, 2023
a6b70e8
Updated documentation
fmartingr Jul 14, 2023
e7c6396
deps: update
fmartingr Jul 17, 2023
7508590
Merge remote-tracking branch 'origin/master' into feat/api-next
fmartingr Jul 17, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions .github/workflows/_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go: ["1.19", "1.20"]
go: ["1.20"]
services:
postgres:
image: postgres:15
Expand All @@ -33,10 +33,15 @@ jobs:
name: Go ${{ matrix.go }} unit tests
steps:
- uses: actions/checkout@24cb9080177205b6e8c946b17badbe402adc938f # v3.4.0

- name: Setup go
uses: actions/setup-go@6edd4406fa81c3da01a34fa6f6343087c207a568 # v3.5.0
with:
go-version: ${{ matrix.go }}

- name: Set up gotestfmt
uses: gotesttools/gotestfmt-action@65f1d2228f06cc5e828b84597440fbd063d12ea2 # v2.1.0

- uses: actions/cache@88522ab9f39a2ea568f7027eddc7d8d8bc9d59c8 # 3.3.1
with:
path: |
Expand All @@ -46,8 +51,10 @@ jobs:
restore-keys: |
golangci-lint.cache-{interval_number}-
golangci-lint.cache-
- run: go test ./...

- run: make unittest
env:
SHIORI_TEST_PG_URL: "postgres://shiori:shiori@localhost:5432/shiori?sslmode=disable"
SHIORI_TEST_MYSQL_URL: "shiori:shiori@(localhost:3306)/shiori"

- run: CGO_ENABLED=0 go build -tags osusergo,netgo -ldflags="-s -w -X main.version=$(git describe --tags) -X main.date=$(date --iso-8601=seconds)"
31 changes: 31 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Docs: https://golangci-lint.run/usage/configuration/#config-file

run:
timeout: 5m
skip-dirs:
- internal/mocks

issues:
max-issues-per-linter: 0
max-same-issues: 0

linters-settings:
gofmt:
simplify: true
govet:
enable-all: true
disable:
- fieldalignment

linters:
disable-all: true
enable:
- gofmt
- gosimple
# - govet # Re-enable when all shadow declarations are fixed
- ineffassign
- predeclared
- exportloopref
- staticcheck
- unconvert
- unused
1 change: 1 addition & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ builds:
- binary: shiori
env:
- CGO_ENABLED=0
- GIN_MODE=release
tags:
- netgo
- osusergo
Expand Down
66 changes: 66 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
GO ?= $(shell command -v go 2> /dev/null)
BASH ?= $(shell command -v bash 2> /dev/null)

# Development
SHIORI_DIR ?= dev-data

# Testing
GO_TEST_FLAGS ?= -v -race
GOTESTFMT_FLAGS ?=

# Build
CGO_ENABLED ?= 0
BUILD_TIME := $(shell date -u +%Y%m%d.%H%M%S)
BUILD_HASH := $(shell git describe --tags)
BUILD_TAGS ?= osusergo,netgo
LDFLAGS += -s -w -X main.version=$(BUILD_HASH) -X main.date=$(BUILD_TIME)

# Development
GIN_MODE ?= debug
SHIORI_DEVELOPMENT ?= true

# Help documentatin à la https://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
.PHONY: help
help:
@cat Makefile | grep -v '\.PHONY' | grep -v '\help:' | grep -B1 -E '^[a-zA-Z0-9_.-]+:.*' | sed -e "s/:.*//" | sed -e "s/^## //" | grep -v '\-\-' | sed '1!G;h;$$!d' | awk 'NR%2{printf "\033[36m%-30s\033[0m",$$0;next;}1' | sort

## Cleans up build artifacts
.PHONY: clean
clean:
rm -rf dist

## Runs the legacy http API for local development
.PHONY: serve
serve:
SHIORI_DEVELOPMENT=$(SHIORI_DEVELOPMENT) SHIORI_DIR=$(SHIORI_DIR) go run main.go serve

## Runs server for local development
.PHONY: run-server
run-server:
GIN_MODE=$(GIN_MODE) SHIORI_DEVELOPMENT=$(SHIORI_DEVELOPMENT) SHIORI_DIR=$(SHIORI_DIR) go run main.go server

## Generate swagger docs
.PHONY: swagger
swagger:
swag init

## Run linter
.PHONY: lint
lint:
golangci-lint run

## Run unit tests
.PHONY: unittest
unittest:
GIN_MODE=$(GIN_MODE) GO_TEST_FLAGS="$(GO_TEST_FLAGS)" GOTESTFMT_FLAGS="$(GOTESTFMT_FLAGS)" $(BASH) -xe ./scripts/test.sh

## Build binary
.PHONY: build
build: clean
GIN_MODE=$(GIN_MODE) goreleaser build --rm-dist --snapshot

## Creates a coverage report
.PHONY: coverage
coverage:
$(GO) test $(GO_TEST_FLAGS) -coverprofile=coverage.txt ./...
$(GO) tool cover -html=coverage.txt
827 changes: 827 additions & 0 deletions coverage.txt

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
This is a brief explanation of Shiori's API. For more examples you can import this [collection](https://github.com/go-shiori/shiori/blob/master/docs/postman/shiori.postman_collection.json) in Postman.

> ⚠️ **This is the documentation for the old API. This API is deprecated and will be removed in the future. Please refer and start migrating to the [API v1](./APIv1.md) instead.**

<!-- TOC -->

- [Auth](#auth)
Expand Down
18 changes: 18 additions & 0 deletions docs/APIv1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# API v1

> ℹ️ **This is the documentation for the new API. This API is still in development and though the finished endpoints should not change please consider that breaking changes may occur once its properly released. If you are looking for the current API, please [see here](./API.md).**

The new API is an ongoing effort to migrate the current API to a more modern and standard API.

The main goals of this new API are:
- Ease of development
- Use of a [modern framework](https://gin-gonic.com)
- Use of a [standard API specification](https://swagger.io/specification/)
- Self-documented API using [Swag](https://github.com/swaggo/swag)
- Improved authentication and sessions using [JWT](https://jwt.io)
- Deduplicate code between the webserver and the API by refactoring the logic into domains
- Improve testability by using interfaces and dependency injection

The current status of this new API can be checked [here](https://github.com/go-shiori/shiori/issues/640).

Since the API is self-docummented, you can check the API documentation by [running the server locally](./Contribute.md#running-the-server-locally) and visiting the [`/swagger/index.html` endpoint](http://localhost:8080/swagger/index.html).
49 changes: 47 additions & 2 deletions docs/Contribute.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,50 @@
# Contribute

1. [Running the server locally](#running-the-server-locally)
2. [Updating the API documentation](#updating-the-api-documentation)
3. [Lint the code](#lint-the-code)
4. [Running tests](#running-tests)

## Running the server locally

To run the current development server with the defaults you can run the following command:

```bash
make serve
```

If you want to run the refactored server, you can run the following command:

```bash
make run-server
```

> **ℹ️ Note:** For more information into what the _refactored server_ means, please check this issue: https://github.com/go-shiori/shiori/issues/640

## Updating the API documentation

> **ℹ️ Note:** This only applies for the Rest API documentation under the `internal/http` folder, **not** the one under `internal/webserver`.

If you make any changes to the Rest API endpoints, you need to update the swagger documentation. In order to do that, you need to have installed [swag](https://github.com/swaggo/swag).

Then, run the following command:

```bash
make swagger
```

## Lint the code

In order to lint the code, you need to have installed [golangci-lint](https://golangci-lint.run).

After that, run the following command:

```bash
make lint
```

If any errors are found please fix them before submitting your PR.

## Running tests

In order to run the test suite, you need to have running a local instance of MariaDB and PostgreSQL.
Expand All @@ -12,12 +57,12 @@ docker-compose up -d mariadb postgres
After that, provide the `SHIORI_TEST_PG_URL` and `SHIORI_TEST_MYSQL_URL` environment variables with the connection string to the databases:

```
SHIORI_TEST_PG_URL=postgres://postgres:postgres@127.0.0.1:5432/shiori?sslmode=disable
SHIORI_TEST_PG_URL=postgres://shiori:shiori@127.0.0.1:5432/shiori?sslmode=disable
SHIORI_TEST_MYSQL_URL=shiori:shiori@tcp(127.0.0.1:3306)/shiori
```

Finally, run the tests with the following command:

```bash
go test ./...
make unittest
```
2 changes: 2 additions & 0 deletions docs/Usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ Available Commands:
pocket Import bookmarks from Pocket's exported HTML file
print Print the saved bookmarks
serve Serve web interface for managing bookmarks
server Run the Shiori webserver [alpha]
update Update the saved bookmarks
version Output the shiori version

Flags:
-h, --help help for shiori
Expand Down
Loading