-
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.
Merge pull request #16 from bitroll-team/dev
chore(release): v0.3.0
- Loading branch information
Showing
33 changed files
with
997 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
PORT=8080 | ||
DB_ENGINE=sqlite | ||
PRODUCTION=false | ||
SECRET=secret | ||
DB_CONN_STRING=postgres://user:pass@localhost:5432/db?sslmode=disable |
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,74 @@ | ||
name: Release | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
version: | ||
runs-on: ubuntu-22.04 | ||
outputs: | ||
version: ${{ steps.version.outputs.version }} | ||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Install jq | ||
run: sudo apt install jq -y | ||
|
||
- name: Get latest version | ||
id: version | ||
run: | | ||
chmod u+x ./version.sh | ||
./version.sh >> $GITHUB_OUTPUT | ||
release: | ||
runs-on: ubuntu-22.04 | ||
permissions: | ||
contents: write | ||
packages: write | ||
needs: | ||
- version | ||
outputs: | ||
upload_url: ${{ steps.release.outputs.upload_url }} | ||
steps: | ||
- name: Create release | ||
id: release | ||
uses: actions/create-release@v1 | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
with: | ||
tag_name: ${{ needs.version.outputs.version }} | ||
release_name: Release v${{ needs.version.outputs.version }} | ||
draft: false | ||
prerelease: false | ||
|
||
docker: | ||
runs-on: ubuntu-22.04 | ||
permissions: | ||
contents: read | ||
packages: write | ||
needs: | ||
- version | ||
- release | ||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Login | ||
uses: docker/login-action@v2 | ||
with: | ||
registry: ghcr.io | ||
username: ${{ github.actor }} | ||
password: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
- name: Downcase repository name | ||
run: | | ||
echo "REPOSITORY=${GITHUB_REPOSITORY,,}" >> $GITHUB_ENV | ||
- name: Build and push | ||
uses: docker/build-push-action@v3 | ||
with: | ||
context: . | ||
push: true | ||
tags: ghcr.io/${{ env.REPOSITORY }}:latest,ghcr.io/${{ env.REPOSITORY }}:${{ needs.version.outputs.version }} | ||
|
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 @@ | ||
on: | ||
push: | ||
branches: | ||
- dev | ||
|
||
jobs: | ||
tagging: | ||
permissions: | ||
contents: write | ||
|
||
runs-on: ubuntu-22.04 | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- uses: TriPSs/conventional-changelog-action@v3 | ||
name: Changelog | ||
id: changelog | ||
with: | ||
git-user-nane: "{{ Username }}" | ||
git-user-email: "{{ Email }}" | ||
git-message: "chore(release): {version}" | ||
version-file: "version.json" | ||
|
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,37 @@ | ||
name: test | ||
|
||
on: | ||
pull_request: | ||
branches: ["dev"] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: set up go | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version: "1.21" | ||
- name: clean | ||
run: go clean && go clean -cache | ||
- name: build | ||
run: go build . | ||
|
||
test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: set up go | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version: "1.21" | ||
- name: set up docker environment | ||
run: docker compose up -d | ||
- name: clean | ||
run: go clean && go clean -cache | ||
- name: test | ||
run: go test -p 1 -count 1 -v ./... | ||
- name: clean docker environment | ||
run: docker compose down --rmi all -v --remove-orphans | ||
|
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 @@ | ||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
# Environment file | ||
.env |
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,19 @@ | ||
# [0.3.0](https://github.com/bitroll-team/codefest1-users/compare/v0.2.0...v0.3.0) (2023-10-25) | ||
|
||
|
||
### Features | ||
|
||
* login ([6eea5bc](https://github.com/bitroll-team/codefest1-users/commit/6eea5bcb87502d9a0bbba36a8efceb948bff2635)) | ||
* register ([a052388](https://github.com/bitroll-team/codefest1-users/commit/a05238811a55d17e4af1c27646b85c0767ff853d)) | ||
|
||
|
||
|
||
# [0.2.0](https://github.com/bitroll-team/codefest1-users/compare/39a2026613db7c80b09d9b268d58869ccaf24a03...v0.2.0) (2023-10-25) | ||
|
||
|
||
### Features | ||
|
||
* use template ([39a2026](https://github.com/bitroll-team/codefest1-users/commit/39a2026613db7c80b09d9b268d58869ccaf24a03)) | ||
|
||
|
||
|
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 @@ | ||
# build | ||
|
||
FROM golang:1.20.10-alpine3.17 AS build | ||
|
||
RUN apk add upx | ||
WORKDIR /src | ||
COPY go.mod ./ | ||
COPY go.sum ./ | ||
RUN go mod download | ||
|
||
COPY . . | ||
RUN go build -o /opt/app | ||
RUN upx /opt/app | ||
|
||
# runner | ||
|
||
FROM alpine:3.18.4 | ||
|
||
RUN adduser -S --disabled-password -h / -s /sbin/nologin -G nobody -u 3001 runner | ||
USER runner | ||
|
||
EXPOSE 8080 | ||
COPY --from=build /opt/app /opt/app | ||
CMD /opt/app |
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,5 @@ | ||
format: | ||
gofmt -w -l -s . | ||
|
||
lint: | ||
golangci-lint run |
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,35 @@ | ||
package config | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/caarlos0/env/v9" | ||
_ "github.com/joho/godotenv/autoload" | ||
) | ||
|
||
type DBEngine string | ||
|
||
const ( | ||
SQLite DBEngine = "sqlite" | ||
Postgres DBEngine = "postgres" | ||
) | ||
|
||
type Config struct { | ||
Port int `env:"PORT" envDefault:"8080"` | ||
DBEngine DBEngine `env:"DB_ENGINE" envDefault:"sqlite"` | ||
IsProduction bool `env:"PRODUCTION" envDefault:"false"` | ||
Secret string `env:"SECRET" envDefault:"secret"` | ||
DBConnStr string `env:"DB_CONN_STRING" envDefault:"postgres://user:pass@localhost:5432/db?sslmode=disable"` | ||
} | ||
|
||
var Cfg Config | ||
|
||
func ReadEnvVars() Config { | ||
|
||
Cfg = Config{} | ||
if err := env.Parse(&Cfg); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
return Cfg | ||
} |
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,13 @@ | ||
package controller | ||
|
||
import "database/sql" | ||
|
||
type Controller struct { | ||
DB *sql.DB | ||
} | ||
|
||
func SetupController(db *sql.DB) Controller { | ||
var ctrl Controller | ||
ctrl.DB = db | ||
return ctrl | ||
} |
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,38 @@ | ||
package controller | ||
|
||
import ( | ||
"bitroll/codefest1-users/model" | ||
"context" | ||
"time" | ||
|
||
"golang.org/x/crypto/bcrypt" | ||
) | ||
|
||
func (ctrl *Controller) Login(req model.ReqLogin) (string, error) { | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), time.Second) | ||
defer cancel() | ||
|
||
query := "SELECT id,password_hash FROM users WHERE email = $1" | ||
|
||
row := ctrl.DB.QueryRowContext( | ||
ctx, | ||
query, | ||
req.Email, | ||
) | ||
|
||
var userId string | ||
var password_hash string | ||
|
||
if err := row.Scan(&userId, &password_hash); err != nil { | ||
return "", err | ||
} | ||
|
||
// compare | ||
|
||
if err := bcrypt.CompareHashAndPassword([]byte(password_hash), []byte(req.Password)); err != nil { | ||
return "", err | ||
} | ||
|
||
return userId, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package controller | ||
|
||
import ( | ||
"bitroll/codefest1-users/model" | ||
"context" | ||
"golang.org/x/crypto/bcrypt" | ||
"time" | ||
) | ||
|
||
func (ctrl *Controller) Register(req model.ReqRegister) error { | ||
|
||
// pass hash | ||
// TODO: Use secret | ||
|
||
hashed, err := bcrypt.GenerateFromPassword([]byte(req.Password), 8) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// write | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), time.Second) | ||
defer cancel() | ||
|
||
query := ` | ||
INSERT INTO users (username, email, full_name, password_hash) | ||
VALUES ($1, $2, $3, $4) | ||
` | ||
|
||
_, err = ctrl.DB.ExecContext( | ||
ctx, | ||
query, | ||
req.Username, | ||
req.Email, | ||
req.Fullname, | ||
string(hashed), | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package database | ||
|
||
import ( | ||
"database/sql" | ||
"embed" | ||
|
||
"github.com/golang-migrate/migrate/v4" | ||
"github.com/golang-migrate/migrate/v4/database/postgres" | ||
_ "github.com/golang-migrate/migrate/v4/source/file" | ||
"github.com/golang-migrate/migrate/v4/source/iofs" | ||
) | ||
|
||
func RunMigrations(db *sql.DB, fs embed.FS, path string) error { | ||
|
||
driverSrc, err := iofs.New(fs, path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
driverDB, err := postgres.WithInstance(db, &postgres.Config{}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// migrate | ||
|
||
m, err := migrate.NewWithInstance("iofs", driverSrc, "db", driverDB) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// ErrNoChange -> OK | ||
if err := m.Up(); err != nil && err != migrate.ErrNoChange { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.