Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
k8ns committed Jan 2, 2020
1 parent 9d058da commit 5c6444b
Show file tree
Hide file tree
Showing 24 changed files with 305 additions and 218 deletions.
42 changes: 42 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

GO = docker run --rm -e GOOS=linux -e GOARCH=amd64 -v $(PWD):/usr/src/app -w /usr/src/app golang:1.13 go
R = docker run --rm --name notes --network dev -e APP_ENV=dev -e GOOS=linux -e GOARCH=amd64 -v $(PWD):/usr/src/app -w /usr/src/app golang:1.13 go
LAMBDA_VERSION = 7

.PHONY: clean test build lambda_build lambda_artifact lambda_deploy deploy

Expand Down Expand Up @@ -29,6 +30,47 @@ deploy:
aws lambda update-function-code --function-name NotesAPI \
--zip-file "fileb://build/out/main.zip"

promote: set_env_prod publish_version point_prod_alias api_gateway_permissions set_env_stage
#NotesAPI:${stageVariables.lambdaAlias}



set_env_prod:
aws lambda update-function-configuration \
--function-name NotesAPI \
--environment Variables={APP_ENV=prod}

publish_version:
aws lambda publish-version \
--function-name "arn:aws:lambda:eu-west-1:720657473133:function:NotesAPI"

point_prod_alias:
aws lambda update-alias \
--function-name NotesAPI \
--function-version ${LAMBDA_VERSION} \
--name prod

api_gateway_permissions:
aws lambda add-permission \
--function-name "arn:aws:lambda:eu-west-1:720657473133:function:NotesAPI:prod" \
--source-arn "arn:aws:execute-api:eu-west-1:720657473133:lccdn1r7ib/*/*/" \
--principal apigateway.amazonaws.com \
--statement-id ef133303-5ce3-4950-884f-abc0324fcf66 \
--action lambda:InvokeFunction

aws lambda add-permission \
--function-name "arn:aws:lambda:eu-west-1:720657473133:function:NotesAPI:prod" \
--source-arn "arn:aws:execute-api:eu-west-1:720657473133:lccdn1r7ib/*/*/*" \
--principal apigateway.amazonaws.com \
--statement-id a42da76e-38f2-498f-97cf-f3bbcc92f3fc \
--action lambda:InvokeFunction

set_env_stage:
aws lambda update-function-configuration \
--function-name NotesAPI \
--environment Variables={APP_ENV=stage}



deploy_test:
curl https://finance-api.kvslab.icu/info
Expand Down
Binary file removed build/out/main.zip
Binary file not shown.
Binary file removed build/out/notes_api
Binary file not shown.
7 changes: 6 additions & 1 deletion cmd/gin.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package main

import (
"github.com/ksopin/notes/internal/config"
"github.com/ksopin/notes/internal/http"
log "github.com/sirupsen/logrus"
"os"
"strings"
)

func main() {
cfg := config.GetConfig(strings.Join([]string{"config/config", os.Getenv("APP_ENV"), "yml"}, "."))

log.SetFormatter(&log.JSONFormatter{})
err := http.Run()
err := http.Run(cfg.Http, cfg.App)
if err != nil {
panic(err)
}
Expand Down
11 changes: 9 additions & 2 deletions cmd/lambda.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package main

import ginlambda "github.com/ksopin/notes/internal/lambda"
import (
"github.com/ksopin/notes/internal/config"
ginlambda "github.com/ksopin/notes/internal/lambda"
"os"
"strings"
)

func main() {
err := ginlambda.Run()
cfg := config.GetConfig(strings.Join([]string{"config/config", os.Getenv("APP_ENV"), "yml"}, "."))

err := ginlambda.Run(cfg.App)
if err != nil {
panic(err)
}
Expand Down
22 changes: 15 additions & 7 deletions config/config.dev.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
app:
project_name: Notes API
code: notes-api
version: 1.0.0
db:
server: mysql_server
username: root
password: dbpsswrd
name: notes
version: 1.0.3
db:
server: mysql_server
username: root
password: dbpsswrd
name: notes
auth:
key_path: data/key.pem
notes:
table:
notes: notes
tags: tags
notes_tags_links: notes_tags

http:
enabled: true
port: 80
port: 80
22 changes: 13 additions & 9 deletions config/config.prod.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
app:
project_name: Notes API
code: notes-api
version: 1.0.1
db:
server: dev-db.c41pzugyuh79.eu-west-1.rds.amazonaws.com
username: appnotes
password: Bo1c6HTgo7YCNig9
name: notes
http:
enabled: true
port: 80
version: 1.0.3
db:
server: dev-db.c41pzugyuh79.eu-west-1.rds.amazonaws.com
username: appnotes
password: Bo1c6HTgo7YCNig9
name: notes
auth:
key_path: data/key.pem
notes:
table:
notes: notes
tags: tags
notes_tags_links: notes_tags
22 changes: 13 additions & 9 deletions config/config.stage.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
app:
project_name: Notes API
code: notes-api
version: stage
db:
server: dev-db.c41pzugyuh79.eu-west-1.rds.amazonaws.com
username: appnotes
password: Bo1c6HTgo7YCNig9
name: notes_stage
http:
enabled: true
port: 80
version: 1.0.3-stage
db:
server: dev-db.c41pzugyuh79.eu-west-1.rds.amazonaws.com
username: appnotes
password: Bo1c6HTgo7YCNig9
name: notes_stage
auth:
key_path: data/key.pem
notes:
table:
notes: notes
tags: tags
notes_tags_links: notes_tags
68 changes: 43 additions & 25 deletions internal/app/notes_manager.go → internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"github.com/ksopin/notes/pkg/auth"
"github.com/ksopin/notes/pkg/db"
"github.com/ksopin/notes/pkg/notes"
"sync"
)
Expand All @@ -13,55 +14,62 @@ type NotFoundErr error
type NotExistsErr error

var (
manager *NotesManager
managerOnce sync.Once
application *App
appOnce sync.Once
)

type NotesManager struct {
type App struct {
storage *notes.Storage
inputFilter *NoteInputFilter
service *auth.Service
}

func NewNotesManager() *NotesManager {
return &NotesManager{
storage: notes.NewStorage(),
func InitApp(cfg *Config) {
appOnce.Do(func() {
application = NewApp(cfg)
})
}



func NewApp(cfg *Config) *App {

// all the magic should be here
// init db

conn := db.GetPersistentDB()

return &App{
storage: notes.NewStorage(conn),
inputFilter: NewNoteInputFilter(),
service: auth.New(db.GetPersistentDB(), cfg.Auth.KeyPath),
}
}

func GetNotesManager() *NotesManager {
managerOnce.Do(func() {
manager = NewNotesManager()
})
return manager
func Get() *App {
if application == nil {
panic("application has not initialized")
}
return application
}

func (m *NotesManager) GetTags(ctx context.Context) ([]*notes.Tag, error) {
func (m *App) GetTags(ctx context.Context) ([]*notes.Tag, error) {
u, err := auth.GetUser(ctx)
if err != nil {
return nil, err
}
return m.storage.GetTags(u.Id)
}

func (m *NotesManager) GetNotes(ctx context.Context, lastId uint, tagIds []uint) ([]*notes.Note, error) {
func (m *App) GetNotes(ctx context.Context, lastId uint, tagIds []uint) ([]*notes.Note, error) {
u, err := auth.GetUser(ctx)
if err != nil {
return nil, err
}
return m.storage.GetNotes(u.Id, lastId, tagIds)
}

//func (m *NotesManager) Exists(id uint) bool {
// u, err := auth.GetUser(ctx)
// if err != nil {
// return nil, err
// }
//
// return m.storage.Exists(u.Id, id)
//}

func (m *NotesManager) GetNote(ctx context.Context, id uint) (*notes.Note, error) {
func (m *App) GetNote(ctx context.Context, id uint) (*notes.Note, error) {

u, err := auth.GetUser(ctx)
if err != nil {
Expand All @@ -79,7 +87,9 @@ func (m *NotesManager) GetNote(ctx context.Context, id uint) (*notes.Note, error
return m.storage.GetNote(u.Id, id)
}

func (m *NotesManager) Save(ctx context.Context, n *notes.Note) error {


func (m *App) Save(ctx context.Context, n *notes.Note) error {

u, err := auth.GetUser(ctx)
if err != nil {
Expand All @@ -104,7 +114,7 @@ func (m *NotesManager) Save(ctx context.Context, n *notes.Note) error {
return m.storage.Save(n)
}

func (m *NotesManager) Delete(ctx context.Context, id uint) error {
func (m *App) Delete(ctx context.Context, id uint) error {

u, err := auth.GetUser(ctx)
if err != nil {
Expand All @@ -121,3 +131,11 @@ func (m *NotesManager) Delete(ctx context.Context, id uint) error {

return m.storage.Delete(u.Id, id)
}

func (m *App) VerifySignature(ctx context.Context, token string) (*auth.User, error) {
return m.service.VerifySignature(ctx, token)
}

func (a *App) Auth(ctx context.Context, creds *auth.Credentials) (string, error) {
return a.service.Auth(ctx, creds)
}
19 changes: 0 additions & 19 deletions internal/app/auth_service.go

This file was deleted.

30 changes: 5 additions & 25 deletions internal/app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,15 @@ package app

import (
"github.com/ksopin/notes/pkg/db"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
"io/ioutil"
)
type Config struct {
App *AppConfig
Db *db.Config
Http *struct {
Enabled bool
Port int
}
}

type AppConfig struct{
type Config struct{
ProjectName string `yaml:"project_name"`
Code string
Version string
}

func GetConfig(configFile string) *Config {
yamlFile, err := ioutil.ReadFile(configFile)
if err != nil {
log.Fatalf("yamlFile.Get %+v ", err)
}
c := &Config{}
err = yaml.Unmarshal(yamlFile, c)
if err != nil {
log.Fatalf("Unmarshal: %+v", err)
Db *db.Config
Auth *struct{
KeyPath string `yaml:"key_path"`
}

return c
}

29 changes: 29 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package config

import (
"github.com/ksopin/notes/internal/app"
"github.com/ksopin/notes/internal/http"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
"io/ioutil"
)

type Config struct {
App *app.Config
Http *http.Config
}


func GetConfig(configFile string) *Config {
yamlFile, err := ioutil.ReadFile(configFile)
if err != nil {
log.Fatalf("yamlFile.Get %+v ", err)
}
c := &Config{}
err = yaml.Unmarshal(yamlFile, c)
if err != nil {
log.Fatalf("Unmarshal: %+v", err)
}

return c
}
Loading

0 comments on commit 5c6444b

Please sign in to comment.