Compare commits
merge into: FabianVowie:main
FabianVowie:feature/add-authorization
FabianVowie:feature/add-commit-hash-to-index-metadata
FabianVowie:feature/add-executable-steps
FabianVowie:feature/add-get-files-endpoints
FabianVowie:feature/add-github-ci
FabianVowie:feature/add-http-image-upload
FabianVowie:feature/add-image-processing
FabianVowie:feature/add-logging
FabianVowie:feature/add-metadata-endpoint
FabianVowie:feature/add-more-image-steps
FabianVowie:feature/add-pipeline-endpoints
FabianVowie:feature/add-pipeline-loading
FabianVowie:feature/add-pipelines-to-metadata
FabianVowie:feature/add-rate-limiting
FabianVowie:feature/add-rate-limiting-to-readme
FabianVowie:feature/add-settings
FabianVowie:feature/add-storage-layer
FabianVowie:feature/fix-routes
FabianVowie:feature/general-cleanup
FabianVowie:feature/improve-code-stability
FabianVowie:feature/improve-pipeline-abstraction
FabianVowie:feature/make-middlewares-optional
FabianVowie:feature/restructure-pipelines
FabianVowie:feature/start-readme
FabianVowie:feature/update-config-output-format-options
FabianVowie:feature/update-gitignore
FabianVowie:feature/update-route-registration
FabianVowie:main
FabianVowie:pr-feature/add-authorization
FabianVowie:pr-feature/add-commit-hash-to-index-metadata
FabianVowie:pr-feature/add-executable-steps
FabianVowie:pr-feature/add-get-files-endpoints
FabianVowie:pr-feature/add-github-ci
FabianVowie:pr-feature/add-http-image-upload
FabianVowie:pr-feature/add-image-processing
FabianVowie:pr-feature/add-logging
FabianVowie:pr-feature/add-metadata-endpoint
FabianVowie:pr-feature/add-more-image-steps
FabianVowie:pr-feature/add-pipeline-endpoints
FabianVowie:pr-feature/add-pipeline-loading
FabianVowie:pr-feature/add-pipelines-to-metadata
FabianVowie:pr-feature/add-rate-limiting
FabianVowie:pr-feature/add-rate-limiting-to-readme
FabianVowie:pr-feature/add-settings
FabianVowie:pr-feature/add-storage-layer
FabianVowie:pr-feature/enhance-api-responses
FabianVowie:pr-feature/fix-routes
FabianVowie:pr-feature/general-cleanup
FabianVowie:pr-feature/improve-code-stability
FabianVowie:pr-feature/improve-pipeline-abstraction
FabianVowie:pr-feature/make-middlewares-optional
FabianVowie:pr-feature/restructure-pipelines
FabianVowie:pr-feature/start-readme
FabianVowie:pr-feature/update-config-output-format-options
FabianVowie:pr-feature/update-gitignore
FabianVowie:pr-feature/update-route-registration
fdai6545:feature/add-authorization
fdai6545:feature/add-commit-hash-to-index-metadata
fdai6545:feature/add-executable-steps
fdai6545:feature/add-get-files-endpoints
fdai6545:feature/add-github-ci
fdai6545:feature/add-http-image-upload
fdai6545:feature/add-image-processing
fdai6545:feature/add-logging
fdai6545:feature/add-metadata-endpoint
fdai6545:feature/add-more-image-steps
fdai6545:feature/add-pipeline-endpoints
fdai6545:feature/add-pipeline-loading
fdai6545:feature/add-pipelines-to-metadata
fdai6545:feature/add-rate-limiting
fdai6545:feature/add-rate-limiting-to-readme
fdai6545:feature/add-settings
fdai6545:feature/add-storage-layer
fdai6545:feature/fix-routes
fdai6545:feature/general-cleanup
fdai6545:feature/improve-code-stability
fdai6545:feature/improve-pipeline-abstraction
fdai6545:feature/make-middlewares-optional
fdai6545:feature/restructure-pipelines
fdai6545:feature/start-readme
fdai6545:feature/update-config-output-format-options
fdai6545:feature/update-gitignore
fdai6545:feature/update-route-registration
fdai6545:main
fdai6545:pr-feature/add-authorization
fdai6545:pr-feature/add-commit-hash-to-index-metadata
fdai6545:pr-feature/add-executable-steps
fdai6545:pr-feature/add-get-files-endpoints
fdai6545:pr-feature/add-github-ci
fdai6545:pr-feature/add-http-image-upload
fdai6545:pr-feature/add-image-processing
fdai6545:pr-feature/add-logging
fdai6545:pr-feature/add-metadata-endpoint
fdai6545:pr-feature/add-more-image-steps
fdai6545:pr-feature/add-pipeline-endpoints
fdai6545:pr-feature/add-pipeline-loading
fdai6545:pr-feature/add-pipelines-to-metadata
fdai6545:pr-feature/add-rate-limiting
fdai6545:pr-feature/add-rate-limiting-to-readme
fdai6545:pr-feature/add-settings
fdai6545:pr-feature/add-storage-layer
fdai6545:pr-feature/enhance-api-responses
fdai6545:pr-feature/fix-routes
fdai6545:pr-feature/general-cleanup
fdai6545:pr-feature/improve-code-stability
fdai6545:pr-feature/improve-pipeline-abstraction
fdai6545:pr-feature/make-middlewares-optional
fdai6545:pr-feature/restructure-pipelines
fdai6545:pr-feature/start-readme
fdai6545:pr-feature/update-config-output-format-options
fdai6545:pr-feature/update-gitignore
fdai6545:pr-feature/update-route-registration
pull from: fdai6545:feature/add-more-image-steps
fdai6545:feature/add-authorization
fdai6545:feature/add-commit-hash-to-index-metadata
fdai6545:feature/add-executable-steps
fdai6545:feature/add-get-files-endpoints
fdai6545:feature/add-github-ci
fdai6545:feature/add-http-image-upload
fdai6545:feature/add-image-processing
fdai6545:feature/add-logging
fdai6545:feature/add-metadata-endpoint
fdai6545:feature/add-more-image-steps
fdai6545:feature/add-pipeline-endpoints
fdai6545:feature/add-pipeline-loading
fdai6545:feature/add-pipelines-to-metadata
fdai6545:feature/add-rate-limiting
fdai6545:feature/add-rate-limiting-to-readme
fdai6545:feature/add-settings
fdai6545:feature/add-storage-layer
fdai6545:feature/fix-routes
fdai6545:feature/general-cleanup
fdai6545:feature/improve-code-stability
fdai6545:feature/improve-pipeline-abstraction
fdai6545:feature/make-middlewares-optional
fdai6545:feature/restructure-pipelines
fdai6545:feature/start-readme
fdai6545:feature/update-config-output-format-options
fdai6545:feature/update-gitignore
fdai6545:feature/update-route-registration
fdai6545:main
fdai6545:pr-feature/add-authorization
fdai6545:pr-feature/add-commit-hash-to-index-metadata
fdai6545:pr-feature/add-executable-steps
fdai6545:pr-feature/add-get-files-endpoints
fdai6545:pr-feature/add-github-ci
fdai6545:pr-feature/add-http-image-upload
fdai6545:pr-feature/add-image-processing
fdai6545:pr-feature/add-logging
fdai6545:pr-feature/add-metadata-endpoint
fdai6545:pr-feature/add-more-image-steps
fdai6545:pr-feature/add-pipeline-endpoints
fdai6545:pr-feature/add-pipeline-loading
fdai6545:pr-feature/add-pipelines-to-metadata
fdai6545:pr-feature/add-rate-limiting
fdai6545:pr-feature/add-rate-limiting-to-readme
fdai6545:pr-feature/add-settings
fdai6545:pr-feature/add-storage-layer
fdai6545:pr-feature/enhance-api-responses
fdai6545:pr-feature/fix-routes
fdai6545:pr-feature/general-cleanup
fdai6545:pr-feature/improve-code-stability
fdai6545:pr-feature/improve-pipeline-abstraction
fdai6545:pr-feature/make-middlewares-optional
fdai6545:pr-feature/restructure-pipelines
fdai6545:pr-feature/start-readme
fdai6545:pr-feature/update-config-output-format-options
fdai6545:pr-feature/update-gitignore
fdai6545:pr-feature/update-route-registration
FabianVowie:feature/add-authorization
FabianVowie:feature/add-commit-hash-to-index-metadata
FabianVowie:feature/add-executable-steps
FabianVowie:feature/add-get-files-endpoints
FabianVowie:feature/add-github-ci
FabianVowie:feature/add-http-image-upload
FabianVowie:feature/add-image-processing
FabianVowie:feature/add-logging
FabianVowie:feature/add-metadata-endpoint
FabianVowie:feature/add-more-image-steps
FabianVowie:feature/add-pipeline-endpoints
FabianVowie:feature/add-pipeline-loading
FabianVowie:feature/add-pipelines-to-metadata
FabianVowie:feature/add-rate-limiting
FabianVowie:feature/add-rate-limiting-to-readme
FabianVowie:feature/add-settings
FabianVowie:feature/add-storage-layer
FabianVowie:feature/fix-routes
FabianVowie:feature/general-cleanup
FabianVowie:feature/improve-code-stability
FabianVowie:feature/improve-pipeline-abstraction
FabianVowie:feature/make-middlewares-optional
FabianVowie:feature/restructure-pipelines
FabianVowie:feature/start-readme
FabianVowie:feature/update-config-output-format-options
FabianVowie:feature/update-gitignore
FabianVowie:feature/update-route-registration
FabianVowie:main
FabianVowie:pr-feature/add-authorization
FabianVowie:pr-feature/add-commit-hash-to-index-metadata
FabianVowie:pr-feature/add-executable-steps
FabianVowie:pr-feature/add-get-files-endpoints
FabianVowie:pr-feature/add-github-ci
FabianVowie:pr-feature/add-http-image-upload
FabianVowie:pr-feature/add-image-processing
FabianVowie:pr-feature/add-logging
FabianVowie:pr-feature/add-metadata-endpoint
FabianVowie:pr-feature/add-more-image-steps
FabianVowie:pr-feature/add-pipeline-endpoints
FabianVowie:pr-feature/add-pipeline-loading
FabianVowie:pr-feature/add-pipelines-to-metadata
FabianVowie:pr-feature/add-rate-limiting
FabianVowie:pr-feature/add-rate-limiting-to-readme
FabianVowie:pr-feature/add-settings
FabianVowie:pr-feature/add-storage-layer
FabianVowie:pr-feature/enhance-api-responses
FabianVowie:pr-feature/fix-routes
FabianVowie:pr-feature/general-cleanup
FabianVowie:pr-feature/improve-code-stability
FabianVowie:pr-feature/improve-pipeline-abstraction
FabianVowie:pr-feature/make-middlewares-optional
FabianVowie:pr-feature/restructure-pipelines
FabianVowie:pr-feature/start-readme
FabianVowie:pr-feature/update-config-output-format-options
FabianVowie:pr-feature/update-gitignore
FabianVowie:pr-feature/update-route-registration
51 Commits
main
...
feature/ad
22 changed files with 1326 additions and 102 deletions
-
5.gitignore
-
281README.md
-
28auth/authorization.go
-
50auth/authorization_test.go
-
21config/example.json
-
4go.mod
-
8go.sum
-
16lithium.md
-
36main.go
-
18main_test.go
-
99pipelines/executable_step.go
-
18pipelines/executable_step_test.go
-
61pipelines/pipeline.go
-
541pipelines/pipeline_test.go
-
39pipelines/step.go
-
3pipelines/step_test.go
-
71settings/settings.go
-
49settings/settings_test.go
-
57storage/storage.go
-
23storage/storage_test.go
-
BINtests/files/800x500.jpg
-
BINtests/files/900x900.jpg
@ -0,0 +1,281 @@ |
|||
# Lithium |
|||
|
|||
Micro-service for file storage and processing written in Go. |
|||
|
|||
## Features |
|||
|
|||
- Image processing pipelines for various transformations |
|||
- Web API for storing & retrieving files |
|||
|
|||
## Requirements |
|||
|
|||
- [Go 1.17+](https://go.dev/) |
|||
- [*Docker*](https://docs.docker.com/) (optional) |
|||
|
|||
## Setup |
|||
|
|||
#### 1. Clone repository |
|||
|
|||
```shell |
|||
git clone git@gogs.informatik.hs-fulda.de:FabianVowie/Lithium.git |
|||
``` |
|||
|
|||
#### 2. Pull dependencies |
|||
|
|||
```shell |
|||
go get |
|||
``` |
|||
|
|||
#### 3. Build & start application |
|||
|
|||
```shell |
|||
go run . |
|||
``` |
|||
|
|||
**Run using [Docker](https://docs.docker.com/) container** |
|||
|
|||
```shell |
|||
docker run --rm -p 8000:8000 -v "$PWD":/usr/src/lithium -w /usr/src/lithium golang:1.17 go run . |
|||
``` |
|||
|
|||
## Testing |
|||
|
|||
```shell |
|||
go test ./... |
|||
``` |
|||
|
|||
### Run tests in verbose logging mode |
|||
|
|||
```shell |
|||
go test ./... -v |
|||
``` |
|||
|
|||
### Run tests in [Docker](https://docs.docker.com/) container |
|||
|
|||
```shell |
|||
docker run --rm -v "$PWD":/usr/src/lithium -w /usr/src/lithium golang:1.17 go test ./... |
|||
``` |
|||
|
|||
## API |
|||
|
|||
### `GET` `/` |
|||
|
|||
Show application information. |
|||
|
|||
**Required headers**: |
|||
```shell |
|||
Authorization: Bearer <Token> |
|||
``` |
|||
|
|||
**Example response**: |
|||
|
|||
```json |
|||
{ |
|||
"name": "Lithium", |
|||
"version": "0.1.0" |
|||
} |
|||
``` |
|||
|
|||
### `GET` `/{pipeline}` |
|||
|
|||
Show pipeline information. |
|||
|
|||
**Required headers**: |
|||
```shell |
|||
Authorization: Bearer <Token> |
|||
``` |
|||
|
|||
**Example response**: |
|||
|
|||
```json |
|||
{ |
|||
"name": "example pipeline", |
|||
"slug": "example", |
|||
"type": 0, |
|||
"remove_metadata": false, |
|||
"steps": [ |
|||
{ |
|||
"name": "resize image", |
|||
"type": 0, |
|||
"options": { |
|||
"width": 1280, |
|||
"height": 720, |
|||
"upscale": false |
|||
} |
|||
} |
|||
], |
|||
"output": { |
|||
"format": "jpg", |
|||
"quality": 90 |
|||
} |
|||
} |
|||
``` |
|||
|
|||
## Configuration |
|||
|
|||
Config options can be adjusted via the [`settings.json`](settings.json) file in the root directory. |
|||
|
|||
```json |
|||
{ |
|||
"endpoint": "0.0.0.0:8000", |
|||
"token": "changeme", |
|||
"storage_provider": { |
|||
"type": 0, |
|||
"base_path": "assets" |
|||
} |
|||
} |
|||
``` |
|||
|
|||
## Pipelines |
|||
|
|||
The project uses a pipeline system defined by [JSON](https://en.wikipedia.org/wiki/JSON) files located in the [config](config) folder. |
|||
Take a look at the [example pipeline configuration file](config/example.json). |
|||
|
|||
Available pipeline `type`s: `0` (Image), `1` (Video) |
|||
|
|||
```json |
|||
{ |
|||
"name": "example pipeline", |
|||
"slug": "example", |
|||
"type": 0, |
|||
"removeMetadata": false, |
|||
"steps": [], |
|||
"output": {} |
|||
} |
|||
``` |
|||
|
|||
### Images pipeline |
|||
|
|||
The image pipeline offers the following additional output options. |
|||
|
|||
Available `format` types: `jpg` (or `jpeg`), `png`, `gif`, `tif` (or `tiff`) and `bmp` |
|||
The `quality` field can contain any integer between 1 and 100. |
|||
|
|||
```json |
|||
{ |
|||
"output": { |
|||
"format": "jpg", |
|||
"quality": 90 |
|||
} |
|||
} |
|||
``` |
|||
|
|||
### Available pipeline steps |
|||
|
|||
Each pipeline step consists of an optional `name`, a predefined `type` and configurable `options`. |
|||
See the available options below for more information. |
|||
|
|||
```json |
|||
{ |
|||
"name": "step name", |
|||
"type": 0, |
|||
"options": {} |
|||
} |
|||
``` |
|||
|
|||
#### Resizing images |
|||
|
|||
Resize an image by a given `width` and `height`. |
|||
|
|||
**Step definition**: |
|||
|
|||
```json |
|||
{ |
|||
"type": 0, |
|||
"options": { |
|||
"width": 1280, |
|||
"height": 720 |
|||
} |
|||
} |
|||
``` |
|||
|
|||
#### Rotating images |
|||
|
|||
Rotate an image with a given `angle` in degrees. |
|||
|
|||
**Step definition**: |
|||
|
|||
```json |
|||
{ |
|||
"type": 1, |
|||
"options": { |
|||
"angle": 90.0 |
|||
} |
|||
} |
|||
``` |
|||
|
|||
#### Flipping images |
|||
|
|||
Flip an image with a given direction. |
|||
Allowed values for the `direction` option are `"h"` (horizontal), `"v"` (vertical) |
|||
|
|||
**Step definition**: |
|||
|
|||
```json |
|||
{ |
|||
"type": 2, |
|||
"options": { |
|||
"direction": "h" |
|||
} |
|||
} |
|||
``` |
|||
|
|||
#### Grayscale |
|||
|
|||
Convert the colorspace of an image into grayscale. |
|||
|
|||
**Step definition**: |
|||
|
|||
```json |
|||
{ |
|||
"type": 3 |
|||
} |
|||
``` |
|||
|
|||
#### Fit |
|||
|
|||
Scales down the image to fit the specified maximum width and height. |
|||
|
|||
**Step definition**: |
|||
|
|||
```json |
|||
{ |
|||
"type": 4, |
|||
"options": { |
|||
"height": 300, |
|||
"width": 200 |
|||
} |
|||
} |
|||
``` |
|||
|
|||
#### Invert |
|||
|
|||
Invert image colors. |
|||
|
|||
**Step definition**: |
|||
|
|||
```json |
|||
{ |
|||
"type": 5 |
|||
} |
|||
``` |
|||
|
|||
#### Blur |
|||
|
|||
Blur image using Gaussian functions. |
|||
|
|||
**Step definition**: |
|||
|
|||
```json |
|||
{ |
|||
"type": 6, |
|||
"options": { |
|||
"sigma": 50.0 |
|||
} |
|||
} |
|||
``` |
|||
|
|||
## Authors |
|||
|
|||
- [Fabian Vowie](https://gogs.informatik.hs-fulda.de/FabianVowie) |
|||
- [Roman Zipp](https://gogs.informatik.hs-fulda.de/roman.zipp) |
@ -0,0 +1,28 @@ |
|||
package auth |
|||
|
|||
import ( |
|||
"net/http" |
|||
"strings" |
|||
) |
|||
|
|||
type AuthenticationMiddleware struct { |
|||
secret string |
|||
} |
|||
|
|||
func (middleware AuthenticationMiddleware) Middleware(next http.Handler) http.Handler { |
|||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
|||
authToken := r.Header.Get("Authorization") |
|||
|
|||
if authToken == "" || strings.HasPrefix(authToken, "Bearer ") == false || authToken[7:] != middleware.secret { |
|||
http.Error(w, "Forbidden", http.StatusForbidden) |
|||
} else { |
|||
next.ServeHTTP(w, r) |
|||
} |
|||
}) |
|||
} |
|||
|
|||
func CreateAuthenticationMiddleware(secret string) AuthenticationMiddleware { |
|||
return AuthenticationMiddleware{ |
|||
secret, |
|||
} |
|||
} |
@ -0,0 +1,50 @@ |
|||
package auth |
|||
|
|||
import ( |
|||
"net/http" |
|||
"net/http/httptest" |
|||
"testing" |
|||
|
|||
"github.com/bxcodec/faker/v3" |
|||
"github.com/stretchr/testify/assert" |
|||
) |
|||
|
|||
func TestAuthenticationMiddleware(t *testing.T) { |
|||
token := faker.Word() |
|||
middleware := CreateAuthenticationMiddleware(token) |
|||
|
|||
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
|||
w.WriteHeader(http.StatusOK) |
|||
}) |
|||
|
|||
middlewareHandler := middleware.Middleware(handler) |
|||
|
|||
t.Run("AuthenticationMiddleware returns 403 response when authorization header is incorrect", func(t *testing.T) { |
|||
request, _ := http.NewRequest("GET", "/", nil) |
|||
responseRecorder := httptest.NewRecorder() |
|||
|
|||
middlewareHandler.ServeHTTP(responseRecorder, request) |
|||
|
|||
assert.Equal(t, 403, responseRecorder.Code) |
|||
}) |
|||
|
|||
t.Run("AuthenticationMiddleware returns 403 response when authorization header is missing Bearer prefix", func(t *testing.T) { |
|||
request, _ := http.NewRequest("GET", "/", nil) |
|||
request.Header.Set("Authorization", token) |
|||
responseRecorder := httptest.NewRecorder() |
|||
|
|||
middlewareHandler.ServeHTTP(responseRecorder, request) |
|||
|
|||
assert.Equal(t, 403, responseRecorder.Code) |
|||
}) |
|||
|
|||
t.Run("AuthenticationMiddleware continues when authorization header is correct", func(t *testing.T) { |
|||
request, _ := http.NewRequest("GET", "/", nil) |
|||
request.Header.Set("Authorization", "Bearer "+token) |
|||
responseRecorder := httptest.NewRecorder() |
|||
|
|||
middlewareHandler.ServeHTTP(responseRecorder, request) |
|||
|
|||
assert.Equal(t, 200, responseRecorder.Code) |
|||
}) |
|||
} |
@ -1,16 +0,0 @@ |
|||
# Lithium |
|||
|
|||
Micro-service for file storage and processing. |
|||
|
|||
## Features |
|||
|
|||
- File storing with various providers |
|||
- S3 (or S3 compatible like MinIO) |
|||
- Locally (for development purposes) |
|||
- File processing "pipelines" for various formats |
|||
- Compression, Resizing for images |
|||
- Encoding for videos |
|||
- Remove metadata (e.g. EXIF) |
|||
- File-based configuration for pipelines |
|||
- JSON/YAML/TOML? |
|||
- Web api to store and retrieve files |
@ -0,0 +1,71 @@ |
|||
package settings |
|||
|
|||
import ( |
|||
"encoding/json" |
|||
"os" |
|||
"path/filepath" |
|||
|
|||
"github.com/spf13/afero" |
|||
) |
|||
|
|||
const ( |
|||
Local FileSystemType = iota |
|||
) |
|||
|
|||
type FileSystemType int |
|||
|
|||
type Settings struct { |
|||
Endpoint string `json:"endpoint"` |
|||
Token string `json:"token"` |
|||
StorageProvider StorageSettings `json:"storage_provider"` |
|||
} |
|||
|
|||
type StorageSettings struct { |
|||
Type FileSystemType `json:"type"` |
|||
BasePath string `json:"base_path"` |
|||
} |
|||
|
|||
func parseSettings(data []byte) Settings { |
|||
settings := Settings{} |
|||
|
|||
err := json.Unmarshal(data, &settings) |
|||
if err != nil { |
|||
return Settings{} |
|||
} |
|||
|
|||
return settings |
|||
} |
|||
|
|||
func LoadSettings(fileSystem afero.Fs) (Settings, error) { |
|||
workingDirectory, _ := os.Getwd() |
|||
path := filepath.Join(workingDirectory, "settings.json") |
|||
|
|||
// Load file and parse file
|
|||
data, err := afero.ReadFile(fileSystem, path) |
|||
|
|||
if err == nil { |
|||
return parseSettings(data), nil |
|||
} |
|||
|
|||
// If file does not exist, create default settings
|
|||
defaultSettings := Settings{ |
|||
Endpoint: "127.0.0.1:8000", |
|||
Token: "changeme", |
|||
StorageProvider: StorageSettings{ |
|||
Type: Local, |
|||
BasePath: "assets", |
|||
}, |
|||
} |
|||
|
|||
serializedSettings, err := json.MarshalIndent(defaultSettings, "", "\t") |
|||
if err != nil { |
|||
return Settings{}, err |
|||
} |
|||
|
|||
err = afero.WriteFile(fileSystem, path, serializedSettings, os.ModePerm) |
|||
if err != nil { |
|||
return Settings{}, err |
|||
} |
|||
|
|||
return defaultSettings, nil |
|||
} |
@ -0,0 +1,49 @@ |
|||
package settings |
|||
|
|||
import ( |
|||
"os" |
|||
"path/filepath" |
|||
"testing" |
|||
|
|||
"github.com/spf13/afero" |
|||
"github.com/stretchr/testify/assert" |
|||
) |
|||
|
|||
func TestSettingsParsing(t *testing.T) { |
|||
const file string = `{ |
|||
"endpoint": "0.0.0.0:8000", |
|||
"token": "foobar", |
|||
"storage_provider": { |
|||
"type": 0, |
|||
"base_path": "assets" |
|||
} |
|||
}` |
|||
|
|||
t.Run("Settings parsing is successful", func(t *testing.T) { |
|||
settings := parseSettings([]byte(file)) |
|||
|
|||
assert.Equal(t, "0.0.0.0:8000", settings.Endpoint) |
|||
assert.Equal(t, "foobar", settings.Token) |
|||
assert.Equal(t, "assets", settings.StorageProvider.BasePath) |
|||
}) |
|||
} |
|||
|
|||
func TestSettingsLoading(t *testing.T) { |
|||
t.Run("Settings loading creates default settings.json when none is present", func(t *testing.T) { |
|||
fileSystem := afero.NewMemMapFs() |
|||
workingDirectory, _ := os.Getwd() |
|||
|
|||
path := filepath.Join(workingDirectory, "settings.json") |
|||
|
|||
// Settings file does not exist in the beginning
|
|||
doesFileExist, _ := afero.Exists(fileSystem, path) |
|||
assert.False(t, doesFileExist) |
|||
|
|||
_, err := LoadSettings(fileSystem) |
|||
assert.Nil(t, err) |
|||
|
|||
// Settings file should be present after calling LoadSettings
|
|||
doesFileExist, _ = afero.Exists(fileSystem, path) |
|||
assert.True(t, doesFileExist) |
|||
}) |
|||
} |
Before Width: 800 | Height: 500 | Size: 66 KiB After Width: 800 | Height: 500 | Size: 66 KiB |
Before Width: 900 | Height: 900 | Size: 128 KiB After Width: 900 | Height: 900 | Size: 128 KiB |
Write
Preview
Loading…
Cancel
Save
Reference in new issue