-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main' into feat/middleware-logger
- Loading branch information
Showing
38 changed files
with
647 additions
and
206 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,8 @@ jobs: | |
|
||
test: | ||
runs-on: ubuntu-latest | ||
env: | ||
ENV: test | ||
|
||
steps: | ||
- name: Checkout repository | ||
|
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 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
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
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,10 @@ | ||
# The local env is also used for default values | ||
ADMIN_SITE_HOST=127.0.0.1 | ||
MAIN_SITE_HOST=0.0.0.0 | ||
PORT=3000 | ||
DB_LOGGING=error | ||
DB_DIALECT=postgres | ||
DATABASE_URL="host=postgres user=postgres password=postgres dbname=onepixel port=5432 sslmode=disable TimeZone=UTC" | ||
ADMIN_API_KEY="8DC4FCD4-DD71-4C18-B9C0-C38EF6790815" | ||
JWT_SIGNING_KEY="ECkLlR74HWIZTG4GAFsmBFoqO7Nr30Bc" | ||
JWT_DURATION_DAYS=90 |
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,2 @@ | ||
ADMIN_SITE_HOST=onepixel.link | ||
MAIN_SITE_HOST=1px.li |
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,3 @@ | ||
DB_LOGGING=info | ||
DB_DIALECT=sqlite | ||
DATABASE_URL="file::memory:?cache=shared" |
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,33 @@ | ||
package config | ||
|
||
import ( | ||
"github.com/joho/godotenv" | ||
"github.com/samber/lo" | ||
"onepixel_backend/src/utils/applogger" | ||
"os" | ||
"path" | ||
"runtime" | ||
) | ||
|
||
func init() { | ||
if os.Getenv("ENV") == "test" { | ||
// for tests, chdir to the project root | ||
_, filename, _, _ := runtime.Caller(0) | ||
dir := path.Join(path.Dir(filename), "../..") // change to suit test file location | ||
lo.Must0(os.Chdir(dir)) | ||
if err := godotenv.Load("onepixel.test.env"); err != nil { | ||
applogger.Error(err) | ||
} | ||
} | ||
|
||
if os.Getenv("ENV") == "production" || os.Getenv("RAILWAY_ENVIRONMENT") == "production" { | ||
if err := godotenv.Load("onepixel.production.env"); err != nil { | ||
applogger.Error(err) | ||
} | ||
} | ||
|
||
// Use defaults from local.env for all missing vars | ||
if err := godotenv.Load("onepixel.local.env"); err != nil { | ||
applogger.Error(err) | ||
} | ||
} |
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,43 @@ | ||
package config | ||
|
||
import ( | ||
"github.com/samber/lo" | ||
"os" | ||
"strconv" | ||
) | ||
|
||
var Env string | ||
|
||
var DBLogging string | ||
var DBDialect string | ||
var DBUrl string | ||
|
||
var Port string | ||
var MainHost string | ||
var AdminHost string | ||
|
||
var AdminApiKey string | ||
|
||
var JwtSigningKey string | ||
var JwtDurationDays int | ||
|
||
// should run after env.go#init as this `vars` is alphabetically after `env` | ||
func init() { | ||
Env, _ = lo.Coalesce( | ||
os.Getenv("RAILWAY_ENVIRONMENT"), | ||
os.Getenv("ENV"), | ||
"local", | ||
) | ||
DBLogging = os.Getenv("DB_LOGGING") | ||
DBDialect = os.Getenv("DB_DIALECT") | ||
DBUrl, _ = lo.Coalesce( | ||
os.Getenv("DATABASE_PRIVATE_URL"), | ||
os.Getenv("DATABASE_URL"), | ||
) | ||
Port = os.Getenv("PORT") | ||
MainHost = os.Getenv("MAIN_SITE_HOST") | ||
AdminHost = os.Getenv("ADMIN_SITE_HOST") | ||
AdminApiKey = os.Getenv("ADMIN_API_KEY") | ||
JwtSigningKey = os.Getenv("JWT_SIGNING_KEY") | ||
JwtDurationDays, _ = strconv.Atoi(os.Getenv("JWT_DURATION_DAYS")) | ||
} |
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 |
---|---|---|
|
@@ -8,7 +8,7 @@ import ( | |
"testing" | ||
) | ||
|
||
var urlsController = CreateUrlsController(lo.Must(db.GetTestDB())) | ||
var urlsController = CreateUrlsController(lo.Must(db.GetDB())) | ||
|
||
func TestUrlsController_CreateSpecificShortUrl(t *testing.T) { | ||
user, _, _ := userController.Create("[email protected]", "123456") | ||
|
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 |
---|---|---|
@@ -1,8 +1,11 @@ | ||
package controllers | ||
|
||
import ( | ||
"onepixel_backend/src/models" | ||
"errors" | ||
"github.com/google/uuid" | ||
"onepixel_backend/src/db/models" | ||
"onepixel_backend/src/security" | ||
"onepixel_backend/src/utils/applogger" | ||
|
||
"gorm.io/gorm" | ||
) | ||
|
@@ -32,6 +35,28 @@ func CreateUsersController(db *gorm.DB) *UsersController { | |
} | ||
} | ||
|
||
func (c *UsersController) InitDefaultUser() { | ||
defaultUser := &models.User{ | ||
Email: "[email protected]", | ||
Password: security.HashPassword(uuid.New().String()), | ||
ID: 0, | ||
} | ||
|
||
// this doesn't really work, passing ID=0 to gorm is borked | ||
// the code here is just for documentation purposes | ||
res := c.db.Save([]models.User{*(defaultUser)}) | ||
if res.Error != nil { | ||
if errors.Is(res.Error, gorm.ErrDuplicatedKey) { | ||
applogger.Warn("Default user already exists") | ||
return | ||
} | ||
applogger.Error("Failed to create default user") | ||
applogger.Panic(res.Error) | ||
} else { | ||
applogger.Info("Default user created") | ||
} | ||
} | ||
|
||
// Create new user | ||
func (c *UsersController) Create(email string, password string) (user *models.User, token string, err error) { | ||
user = &models.User{ | ||
|
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 |
---|---|---|
|
@@ -10,7 +10,7 @@ import ( | |
"testing" | ||
) | ||
|
||
var userController = CreateUsersController(lo.Must(db.GetTestDB())) | ||
var userController = CreateUsersController(lo.Must(db.GetDB())) | ||
|
||
func TestUsersController_Create(t *testing.T) { | ||
user, token, err := userController.Create("[email protected]", "123456") | ||
|
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
Oops, something went wrong.