Skip to content

Commit

Permalink
cleanup: refactor api architecture & add user roles
Browse files Browse the repository at this point in the history
  • Loading branch information
JordanKnott committed Jul 4, 2020
1 parent a395859 commit eaffaa7
Show file tree
Hide file tree
Showing 141 changed files with 11,932 additions and 3,237 deletions.
4 changes: 4 additions & 0 deletions api/Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
generate:
rm graph/schema.graphqls
for f in graph/schema/*.gql; do cat $f; echo; done > graph/schema.graphqls

start:
docker container start test-db
go run cmd/citadel/main.go
36 changes: 33 additions & 3 deletions api/cmd/citadel/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,50 @@ package main
import (
"fmt"
_ "github.com/lib/pq"
"io/ioutil"
"net/http"
"time"

"github.com/BurntSushi/toml"
"github.com/jmoiron/sqlx"
"github.com/jordanknott/project-citadel/api/router"
"github.com/jordanknott/project-citadel/api/internal/route"
log "github.com/sirupsen/logrus"
)

type Database struct {
Host string
Name string
User string
Password string
}
type AppConfig struct {
Database Database
}

func main() {
dat, err := ioutil.ReadFile("conf/app.toml")
if err != nil {
panic(err)
}

var appConfig AppConfig
_, err = toml.Decode(string(dat), &appConfig)
if err != nil {
panic(err)
}

Formatter := new(log.TextFormatter)
Formatter.TimestampFormat = "02-01-2006 15:04:05"
Formatter.FullTimestamp = true
log.SetFormatter(Formatter)
db, err := sqlx.Connect("postgres", "user=postgres password=test host=0.0.0.0 dbname=citadel sslmode=disable")
log.SetLevel(log.InfoLevel)
connection := fmt.Sprintf("user=%s password=%s host=%s dbname=%s sslmode=disable",
appConfig.Database.User,
appConfig.Database.Password,
appConfig.Database.Host,
appConfig.Database.Name,
)
db, err := sqlx.Connect("postgres", connection)
if err != nil {
log.Panic(err)
}
Expand All @@ -27,6 +57,6 @@ func main() {
defer db.Close()
fmt.Println("starting graphql server on http://localhost:3333")
fmt.Println("starting graphql playground on http://localhost:3333/__graphql")
r, _ := router.NewRouter(db)
r, _ := route.NewRouter(db)
http.ListenAndServe(":3333", r)
}
192 changes: 156 additions & 36 deletions api/cmd/citadelctl/main.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,43 @@
package main

import (
// "context"
// "fmt"
// "io/ioutil"

"bytes"
"context"
"fmt"
"github.com/BurntSushi/toml"
"github.com/jmoiron/sqlx"
"github.com/jordan-wright/email"
"github.com/jordanknott/project-citadel/api/pg"
"github.com/jordanknott/project-citadel/api/router"
_ "github.com/lib/pq"
"golang.org/x/crypto/bcrypt"
"io/ioutil"
"net/smtp"
// "github.com/jmoiron/sqlx"
// "github.com/jordanknott/project-citadel/api/pg"
// "github.com/BurntSushi/toml"
// "github.com/jordanknott/project-citadel/api/router"
// "time"
"text/template"
"time"

"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
)

type Database struct {
Host string
Name string
User string
Password string
}
type AppConfig struct {
Database Database
}

type UserRegistration struct {
Username string
Year string
AppName string
AppURL string
}

type color struct {
Name string
Color string
Expand All @@ -25,39 +48,136 @@ type colors struct {
Color []color
}

func main() {
func SendEmail() {
emailTmpl, err := ioutil.ReadFile("templates/mail/user/registered.tmpl")
if err != nil {
panic(err)
}

user := UserRegistration{Username: "jordanthedev", AppName: "Citadel", AppURL: "http://localhost:3000/", Year: "2020"}
tmpl, err := template.New("registered").Parse(string(emailTmpl))
if err != nil {
panic(err)
}
var tpl bytes.Buffer
if err := tmpl.Execute(&tpl, &user); err != nil {
panic(err)
}

result := tpl.String()

e := email.NewEmail()
e.From = "Jordan Knott <[email protected]>"
e.To = []string{"[email protected]"}
e.Subject = "Jordan Knott (@jordanthedev) invited you to join the team \"Paradox\" on Citadel"
e.Text = []byte("Text Body is, of course, supported!")
e.HTML = []byte("<h1>Fancy HTML is supported, too!</h1>")
e.HTML = []byte(result)
e.Send("localhost:1025", smtp.PlainAuth("", "[email protected]", "password123", "localhost"))
// dur := time.Hour * 24 * 7 * 30
// token, err := router.NewAccessTokenCustomExpiration("21345076-6423-4a00-a6bd-cd9f830e2764", dur)
// if err != nil {
// panic(err)
// }
// fmt.Println(token)

// fmt.Println("seeding database...")

// dat, err := ioutil.ReadFile("data/colors.toml")
// if err != nil {
// panic(err)
// }

// var labelColors colors
// _, err = toml.Decode(string(dat), &labelColors)
// if err != nil {
// panic(err)
// }
// db, err := sqlx.Connect("postgres", "user=postgres password=test host=0.0.0.0 dbname=citadel sslmode=disable")
// repository := pg.NewRepository(db)
// for _, color := range labelColors.Color {
// fmt.Printf("%v\n", color)
// repository.CreateLabelColor(context.Background(), pg.CreateLabelColorParams{color.Name, color.Color, float64(color.Position)})
// }
}

func Seed() {
dur := time.Hour * 24 * 7 * 30
token, err := router.NewAccessTokenCustomExpiration("21345076-6423-4a00-a6bd-cd9f830e2764", dur)
if err != nil {
panic(err)
}
fmt.Println(token)

fmt.Println("seeding database...")

dat, err := ioutil.ReadFile("data/dark_colors.toml")
if err != nil {
panic(err)
}

var labelColors colors
_, err = toml.Decode(string(dat), &labelColors)
if err != nil {
panic(err)
}
db, err := sqlx.Connect("postgres", "user=postgres password=test host=0.0.0.0 dbname=citadel sslmode=disable")
repository := pg.NewRepository(db)
for _, color := range labelColors.Color {
fmt.Printf("%v\n", color)
repository.CreateLabelColor(context.Background(), pg.CreateLabelColorParams{color.Name, color.Color, float64(color.Position)})
}
}
func Migrate() {
dat, err := ioutil.ReadFile("conf/app.toml")
if err != nil {
panic(err)
}

var appConfig AppConfig
_, err = toml.Decode(string(dat), &appConfig)
if err != nil {
panic(err)
}
connection := fmt.Sprintf("user=%s password=%s host=%s dbname=%s sslmode=disable",
appConfig.Database.User,
appConfig.Database.Password,
appConfig.Database.Host,
appConfig.Database.Name,
)
fmt.Println(connection)
db, err := sqlx.Connect("postgres", connection)
if err != nil {
panic(err)
}
defer db.Close()
driver, err := postgres.WithInstance(db.DB, &postgres.Config{})
if err != nil {
panic(err)
}
m, err := migrate.NewWithDatabaseInstance(
"file://migrations",
"postgres", driver)
if err != nil {
panic(err)
}
err = m.Up()
if err != nil {
panic(err)
}
}
func main() {
dat, err := ioutil.ReadFile("conf/app.toml")
if err != nil {
panic(err)
}

var appConfig AppConfig
_, err = toml.Decode(string(dat), &appConfig)
if err != nil {
panic(err)
}
connection := fmt.Sprintf("user=%s password=%s host=%s dbname=%s sslmode=disable",
appConfig.Database.User,
appConfig.Database.Password,
appConfig.Database.Host,
appConfig.Database.Name,
)
fmt.Println(connection)
db, err := sqlx.Connect("postgres", connection)
if err != nil {
panic(err)

}
createdAt := time.Now().UTC()
hashedPwd, err := bcrypt.GenerateFromPassword([]byte("test"), 14)
repo := pg.NewRepository(db)
if err != nil {
panic(err)
}
_, err = repo.CreateUserAccount(context.Background(), pg.CreateUserAccountParams{
Username: "jordan",
Initials: "JK",
Email: "[email protected]",
PasswordHash: string(hashedPwd),
CreatedAt: createdAt,
RoleCode: "admin",
})
if err != nil {
panic(err)
}
}
56 changes: 56 additions & 0 deletions api/cmd/send/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"fmt"

"github.com/RichardKnop/machinery/v1"
"github.com/RichardKnop/machinery/v1/config"
"github.com/RichardKnop/machinery/v1/tasks"
)

func Add(args ...int64) (int64, error) {
sum := int64(0)
for _, arg := range args {
sum += arg
}
return sum, nil
}

func main() {
var cnf = &config.Config{
Broker: "amqp://guest:guest@localhost:5672/",
DefaultQueue: "machinery_tasks",
ResultBackend: "memcache://localhost:11211",
AMQP: &config.AMQPConfig{
Exchange: "machinery_exchange",
ExchangeType: "direct",
BindingKey: "machinery_task",
},
}

fmt.Println("starting server")
server, err := machinery.NewServer(cnf)
if err != nil {
// do something with the error
}

addTask0 := tasks.Signature{
Name: "userRegistration",
Args: []tasks.Arg{
{
Type: "string",
Value: "21345076-6423-4a00-a6bd-cd9f830e2764",
},
},
}

asyncResult, err := server.SendTask(&addTask0)
if err != nil {
fmt.Errorf("Could not send task: %s", err.Error())
}
fmt.Println(asyncResult.GetState())

// results, err := asyncResult.Get(time.Duration(time.Millisecond * 5))
// fmt.Printf("split([\"foo\"]) = %v\n", tasks.HumanReadableResults(results))

}
Loading

0 comments on commit eaffaa7

Please sign in to comment.