Skip to content

Commit

Permalink
Dev (#5)
Browse files Browse the repository at this point in the history
* src: handlers: cors: fixes cors
  • Loading branch information
pandadiestro authored Nov 22, 2024
1 parent 5eb3585 commit 159dd97
Show file tree
Hide file tree
Showing 20 changed files with 632 additions and 226 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ jobs:
- name: build
run: ssh -vT "bauer@${{ secrets.SSH_IP }}" "cd ~/clones/metagallery/stiller-backend/docs && mdbook build"
- name: deploy
run: ssh -vT "bauer@${{ secrets.SSH_IP }}" "echo ${{ secrets.SUDO_PWD }} | sudo -S cp -r ~/clones/metagallery/stiller-backend/docs/book /var/www/stillerdocs && echo ${{ secrets.SUDO_PWD }} | sudo -S chown --recursive www-data /var/www/stillerdocs"
run: ssh -vT "bauer@${{ secrets.SSH_IP }}" "sudo cp -r ~/clones/metagallery/stiller-backend/docs/book /var/www/stillerdocs && sudo chown --recursive www-data /var/www/stillerdocs"
2 changes: 2 additions & 0 deletions stiller-backend/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type ConfigType struct {
FilesPath string
DBPath string
Secret []byte
Admin string
BCryptCost int
}

Expand All @@ -20,6 +21,7 @@ func newConfig() *ConfigType {
DBPath: dotenv.GetString("DBPath"),
Secret: []byte(dotenv.GetString("Secret")),
BCryptCost: dotenv.GetInt("BCryptCost"),
Admin: dotenv.GetString("Admin"),
}
}

Expand Down
24 changes: 13 additions & 11 deletions stiller-backend/db/create.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,15 @@ create table avatar (
file integer not null references file (id)
);

create table gallery (
id integer unique primary key autoincrement not null,
owner integer not null references user (id),
template integer not null references template (id),
slug text unique not null,
title text,
description text,
deploy_stage integer not null,
url text unique not null
create table metatemplatefile (
id integer unique primary key autoincrement not null
);

create table template (
id integer unique primary key autoincrement not null,
tier integer not null references tier (id),
thumbnail integer not null references file (id),
templatefile integer not null references file (id),
templatefile integer not null references metatemplatefile(id),
title text not null,
description text not null
);
Expand Down Expand Up @@ -84,12 +77,21 @@ create table template (

create table galleryslot (
gallery integer not null references gallery (id),
slotref integer not null,
slotid text not null,
res integer references file (id),
title text,
description text
);

create table gallery (
id integer unique primary key autoincrement not null,
owner integer not null references user (id),
template integer not null references template (id),
slug text unique not null,
title text,
description text
);

insert into
filetype
values
Expand Down
52 changes: 35 additions & 17 deletions stiller-backend/internal/dbutils/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const (
Image StillerFileType = iota
Video
Object3D
Unreachable
Metatemplate
UnreachableFileType
)

type StillerFile struct {
Expand All @@ -22,13 +23,21 @@ type StillerFile struct {
Deleted bool `json:"deleted"`
}

type StillerTier uint8
const (
Free StillerTier = iota
VanGogh
Picasso
UnreachableTier
)

type StillerUser struct {
Id int `json:"id"`
TierId int `json:"tierid"`
Displayname string `json:"displayname"`
Username string `json:"username"`
Mail string `json:"mail"`
Bpasswd string `json:"bpasswd"`
Id int `json:"id"`
TierId StillerTier `json:"tierid"`
Displayname string `json:"displayname"`
Username string `json:"username"`
Mail string `json:"mail"`
Bpasswd string `json:"bpasswd"`
}

type StillerTemplateBlock struct {
Expand All @@ -39,6 +48,11 @@ type StillerTemplateBlock struct {
Direction float64 `json:"direction"`
}

type StillerMetatemplatefile struct {
Id int
Path string
}

type StillerTemplate struct {
Id int `json:"id"`
TierId int `json:"tier"`
Expand All @@ -48,16 +62,20 @@ type StillerTemplate struct {
Description string `json:"description"`
}

type SlotType uint8
const (
D2 SlotType = iota
D3
)

type StillerSlot struct {
Ref int `json:"ref"`
Pos []float64 `json:"pos"`
Type SlotType `json:"type"`
type StillerGallerySlot struct {
Id string `json:"id"`
Res int `json:"res"`
Title string `json:"title"`
Description string `json:"description"`
}

type StillerGallery struct {
Id int `json:"id"`
OwnerId int `json:"ownerid"`
TemplateId int `json:"templateid"`
Slug string `json:"slug"`
Title string `json:"title"`
Description string `json:"description"`
Slots []StillerGallerySlot `json:"slots"`
}

45 changes: 45 additions & 0 deletions stiller-backend/internal/dbutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package dbutils

import (
"context"
"errors"
"log"
"stiller"

"github.com/leporo/sqlf"
"zombiezen.com/go/sqlite"
"zombiezen.com/go/sqlite/sqlitex"
)
Expand Down Expand Up @@ -38,3 +40,46 @@ func CloseConn(conn *sqlite.Conn) {
db_pool.Put(conn)
}

func PushNewFile(fileptr *StillerFile) (int, error) {
new_dbconn, dbconn_err := NewConn()
if dbconn_err != nil {
return 0, dbconn_err
}

defer CloseConn(new_dbconn)

query_stmt := sqlf.
InsertInto("file").
Set("owner", fileptr.OwnerId).
Set("type", fileptr.Typeof).
Set("path", fileptr.Path).
Set("filename", fileptr.Filename).
Set("ext", fileptr.Ext).
Set("hashed", fileptr.Hashed).
Set("size", fileptr.Size).
Set("deleted", fileptr.Deleted).
Returning("id")

query := query_stmt.String()
query_id_res := int(-1)

exec_err := sqlitex.ExecuteTransient(new_dbconn, query, &sqlitex.ExecOptions{
ResultFunc: func(stmt *sqlite.Stmt) error {
query_id_res = int(stmt.ColumnInt64(0))
return nil
},

Args: query_stmt.Args(),
})

if query_id_res == -1 {
return 0, errors.New("no new file was added")
}

if exec_err != nil {
return 0, exec_err
}

return query_id_res, nil
}

16 changes: 16 additions & 0 deletions stiller-backend/internal/fsutils/templates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package fsutils

import (
"fmt"
"stiller"
)

func GetTemplatePath(id int) string {
return fmt.Sprintf(
"%s/templates/metatemplate.gay.%d",
stiller.StillerConfig.FilesPath,
id,
)
}


92 changes: 58 additions & 34 deletions stiller-backend/internal/handlers/auth/newuser/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

jsonexp "github.com/go-json-experiment/json"
"github.com/julienschmidt/httprouter"
"github.com/leporo/sqlf"
"golang.org/x/crypto/bcrypt"
"zombiezen.com/go/sqlite"
"zombiezen.com/go/sqlite/sqlitex"
Expand All @@ -20,6 +21,7 @@ import (
var (
ErrInQuery = errors.New("query error")
ErrNotEnoughArgs = errors.New("not enough args in query")
ErrUserExists = errors.New("user already exists")
)

func newuserScalarFn(ctx sqlite.Context, args []sqlite.Value) (sqlite.Value, error){
Expand All @@ -36,45 +38,64 @@ func newuserScalarFn(ctx sqlite.Context, args []sqlite.Value) (sqlite.Value, err
}

new_user := dbutils.StillerUser{
TierId: args[0].Int(),
TierId: dbutils.StillerTier(args[0].Int()),
Username: args[1].Text(),
Displayname: args[2].Text(),
Mail: args[3].Text(),
Bpasswd: string(bcrypt_pwd),
}

query := `
insert into
user (tier, displayname, username, mail, bpasswd)
values
(?1, ?2, ?3, ?4, ?5)
returning
id;`
check_stmt := sqlf.
Select("id").
From("user").
Where("username = ?", new_user.Username)

defer check_stmt.Close()

check_id := int(-1)
check_err := sqlitex.ExecuteTransient(db_conn, check_stmt.String(), &sqlitex.ExecOptions{
ResultFunc: func(stmt *sqlite.Stmt) error {
check_id++
return nil
},

Args: check_stmt.Args(),
})

if check_err != nil {
return sqlite.Value{}, check_err
}

if check_id != -1 {
return sqlite.IntegerValue(-1), ErrUserExists
}

query_stmt := sqlf.
InsertInto("user").
Set("tier", new_user.TierId).
Set("displayname", new_user.Displayname).
Set("username", new_user.Username).
Set("mail", new_user.Mail).
Set("bpasswd", new_user.Bpasswd).
Returning("id")

defer query_stmt.Close()

query := query_stmt.String()
new_id := int(-1)
exec_err := sqlitex.ExecuteTransient(db_conn, query, &sqlitex.ExecOptions{
ResultFunc: func(stmt *sqlite.Stmt) error {
new_id = stmt.ColumnInt(0)
return nil
},

Args: []any{
new_user.TierId,
new_user.Username,
new_user.Displayname,
new_user.Mail,
new_user.Bpasswd,
},
Args: query_stmt.Args(),
})

if exec_err != nil {
return sqlite.Value{}, exec_err
}

if new_id == -1 {
return sqlite.Value{}, ErrInQuery
}

return sqlite.IntegerValue(int64(new_id)), nil
}

Expand Down Expand Up @@ -118,29 +139,32 @@ func Nethandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
UserId: -1,
}

query := `select newuser(?1, ?2, ?3, ?4, ?5);`
query_stmt := sqlf.Select(
"newuser(?, ?, ?, ?, ?)",
rpayload.TierId,
rpayload.Username,
rpayload.Username,
rpayload.Mail,
rpayload.Passwd,
)

defer query_stmt.Close()

query := query_stmt.String()
exec_err := sqlitex.ExecuteTransient(new_dbconn, query, &sqlitex.ExecOptions{
ResultFunc: func(stmt *sqlite.Stmt) error {
if stmt.ColumnType(0) == sqlite.TypeText {
return errors.New(stmt.ColumnText(0))
}

new_tk.UserId = stmt.ColumnInt(0)
return nil
},

Args: []any{
rpayload.AvatarId,
rpayload.TierId,
rpayload.Username,
rpayload.Mail,
rpayload.Passwd,
},
Args: query_stmt.Args(),
})

if handleutils.RequestLog(exec_err, "", http.StatusInternalServerError, &w) {
return
}

if new_tk.UserId == -1 {
handleutils.GenericLog(nil, "no new user was created")
w.WriteHeader(http.StatusInternalServerError)
if handleutils.RequestLog(exec_err, "", http.StatusBadRequest, &w) {
return
}

Expand Down
Loading

0 comments on commit 159dd97

Please sign in to comment.