forked from JordanKnott/taskcafe
-
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.
cleanup: refactor api architecture & add user roles
- Loading branch information
1 parent
a395859
commit eaffaa7
Showing
141 changed files
with
11,932 additions
and
3,237 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 |
---|---|---|
@@ -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 |
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 |
---|---|---|
@@ -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 | ||
|
@@ -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) | ||
} | ||
} |
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,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)) | ||
|
||
} |
Oops, something went wrong.