Github / Mail / Google auth example with Postgresql, Redis and Memcached
Requirements
1.18
- Go: 14.2
- PostgreSQL: 1.6.15
- Memcached: 6.2.6
- Redis: Google
and Github
keys
- Setup OAuth
- Github
- Go to the Developer settings
- Create Application
- Enable
User permissions
->Email addresses
->Read Only
in the Permissions - Generate secret token
- Enter the
URIs
that are allowed to be redirect-URIs (e.g.https://localhost:8080/oauth/github/callback
) - Paste both
Client ID
andClient Secret
to thegithub_secret.json
- Go to the Google Cloud Console
- Create project (add content to the consent screen like title and logo) or use existing
Credentials
->Create Credentials
->Oauth Client ID
- Choose the
Web Application
type and give it a name - Enter the
URIs
that are allowed to be redirect-URIs (e.g.https://localhost:8080/oauth/google/callback
) - Paste both
Client ID
andClient Secret
to thegoogle_secret.json
SMTP
- Mail - Go to the Gmail Settings
- Enable
IMAP
in the Forwarding IMAP/POP - Enable Less secure apps Access
OPTIONAL
- Generate an App Password for mail access
FROM May 30, 2022
- Paste the
mail
and thepassword
to the config
docker-compose
Server is ready immediately after containers are up
SMTP_PASSWORD="mail_password" docker-compose up
-
Port Redis
:6380
Passwordpassword
Database0
-
Port PostgreSQL
:5431
Userserver
Passwordpg_password
Databaseauth_example
It is possible to additionally configure the app using environment variables
environment:
POSTGRES_IP: 127.0.0.1 # connect to local database
HOST_PORT: 8082 # change server port
Setup PostgreSQL
migrate -database ${POSTGRESQL_URL} -path migrate/ up
Down
migrate -database ${POSTGRESQL_URL} -path migrate/ down
Build / Run
git clone https://github.com/illiafox/go-auth.git auth
cd auth
make build
make run # /cmd/server/bin
Run arguments
HTTP mode
server -http
With non-standard config and log file paths
server -config config.toml -log log.txt
environment
:
With reading from Available keys can be found in config structure tags
POSTGRES_PORT=4585 server -env
Logs
In addition to the terminal output, logs are also written to the file
# Terminal
20/05/2022 10:50:20 | info Initializing repository
20/05/2022 10:50:20 | info Done {"time": 0.012092004}
20/05/2022 10:50:20 | info Server started at 0.0.0.0:8080
// File (default log.txt)
{"level":"info","ts":"Fri, 20 May 2022 10:23:52 EEST","msg":"Initializing repository"}
{"level":"info","ts":"Fri, 20 May 2022 10:23:52 EEST","msg":"Done","time":0.015016048}
{"level":"info","ts":"Fri, 20 May 2022 10:23:52 EEST","msg":"Server started at 0.0.0.0:8080"}