arch: move web folder into api & move api to top level

This commit is contained in:
Jordan Knott
2020-07-04 18:08:37 -05:00
parent eaffaa70df
commit e5d5e6da01
354 changed files with 20 additions and 1557 deletions

62
cmd/citadel/main.go Normal file
View File

@ -0,0 +1,62 @@
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/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)
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)
}
db.SetMaxOpenConns(25)
db.SetMaxIdleConns(25)
db.SetConnMaxLifetime(5 * time.Minute)
defer db.Close()
fmt.Println("starting graphql server on http://localhost:3333")
fmt.Println("starting graphql playground on http://localhost:3333/__graphql")
r, _ := route.NewRouter(db)
http.ListenAndServe(":3333", r)
}

183
cmd/citadelctl/main.go Normal file
View File

@ -0,0 +1,183 @@
package main
import (
"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"
"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
Position int
}
type colors struct {
Color []color
}
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 <no-reply@citadel.com>"
e.To = []string{"jordan@jordanthedev.com"}
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(result)
e.Send("localhost:1025", smtp.PlainAuth("", "test@gmail.com", "password123", "localhost"))
}
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: "jordan@jordanthedev.com",
PasswordHash: string(hashedPwd),
CreatedAt: createdAt,
RoleCode: "admin",
})
if err != nil {
panic(err)
}
}

56
cmd/send/main.go Normal file
View File

@ -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))
}

66
cmd/worker/main.go Normal file
View File

@ -0,0 +1,66 @@
package main
import (
"context"
"fmt"
"github.com/RichardKnop/machinery/v1"
"github.com/RichardKnop/machinery/v1/config"
"github.com/google/uuid"
"github.com/jmoiron/sqlx"
"github.com/jordanknott/project-citadel/api/pg"
_ "github.com/lib/pq"
)
type MachineTasks struct {
Repository pg.Repository
}
func (m *MachineTasks) UserRegistration(userID string) (bool, error) {
ctx := context.Background()
uid, err := uuid.Parse(userID)
if err != nil {
return false, err
}
user, err := m.Repository.GetUserAccountByID(ctx, uid)
if err != nil {
return false, err
}
if user.Username == "jordan" {
return true, nil
}
return false, 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
}
db, err := sqlx.Connect("postgres", "user=postgres password=test host=0.0.0.0 dbname=citadel sslmode=disable")
repo := pg.NewRepository(db)
tasks := MachineTasks{repo}
server.RegisterTasks(map[string]interface{}{
"userRegistration": tasks.UserRegistration,
})
worker := server.NewWorker("citadel_worker", 10)
fmt.Println("launching worker")
err = worker.Launch()
if err != nil {
// do something with the error
}
}