Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tensor Titans #18

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Binary file added Backend/.DS_Store
Binary file not shown.
30 changes: 30 additions & 0 deletions Backend/cmd/api/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"genai/infra/database"
"genai/infra/environment"
"genai/infra/rest"
"genai/integrations/aws"
"genai/integrations/diffusion"
"genai/integrations/gpt"
"log"
"os"

"github.com/go-logr/stdr"
"go.opentelemetry.io/otel"
)

func main() {

logger := stdr.New(log.New(os.Stdout, "", log.LstdFlags|log.Lshortfile))
otel.SetLogger(logger)

environment.InitializeEnvs()
database.InitializeGorm()
aws.InitAWS()
diffusion.InitDiffusion()
gpt.InitOpenAI()

rest.InitializeApiRestServer()

}
38 changes: 38 additions & 0 deletions Backend/cmd/cron/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"context"
"genai/infra/database"
"genai/infra/environment"
smartsocial "genai/integrations/smart_social"
"genai/modules/socio"
"log"
"os"
"time"

"github.com/go-co-op/gocron"
"github.com/go-logr/stdr"
"go.opentelemetry.io/otel"
"go.uber.org/zap"
)

func main() {

logger := stdr.New(log.New(os.Stdout, "", log.LstdFlags|log.Lshortfile))
otel.SetLogger(logger)

environment.InitializeEnvs()
database.InitializeGorm()
smartsocial.InitializeSmartSocial()

s := gocron.NewScheduler(time.UTC)

s.Every(3).Seconds().Do(func(ctx context.Context) {
zap.L().Debug("cron running")
socio.SchedulerPickUp(&ctx)
}, context.Background())

go s.StartBlocking()

<-context.Background().Done()
}
26 changes: 26 additions & 0 deletions Backend/common/common.constant.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package common

type ContextTypeEnum int

const (
CTX_QUERIES ContextTypeEnum = iota

CTX_PARAMS
)

type HeaderEnum string

const (
HEADER_CONTENT_TYPE HeaderEnum = "Content-Type"
)

var NEXT_TOKEN string = ""
var PUBLISHED_AFTER string = "2021-01-01T00:00:00Z"

var COST = 1

var CTX_USER = "USER"

var SESSION_TOKEN = "X-AUTHORIZATION"

var VERSION = "b1c17d148455c1fda435ababe9ab1e03bc0d917cc3cf4251916f22c45c83c7df"
33 changes: 33 additions & 0 deletions Backend/common/common.dto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package common

import (
"io"
"mime/multipart"
)

type SuccessDto struct {
Meta AckDto `json:"meta"`
Data interface{} `json:"data"`
}

type AckDto struct {
Success bool `json:"success"`
Message *string `json:"message"`
PaginationParams *PaginationParams `json:"paginationParams,omitempty"`
IsPaginated bool `json:"isPaginated"`
}

type PaginationParams struct {
Page int `json:"page"`
PageSize int `json:"pageSize"`
}

type BlankDto struct {
}

type FileData struct {
File multipart.File
Header *multipart.FileHeader
ReadSeeker io.ReadSeeker
Filename string
}
100 changes: 100 additions & 0 deletions Backend/common/common.handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package common

import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
)

func HandleHTTPGet[OutputDtoType any](serviceFunc func(ctx *context.Context) *OutputDtoType) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {

ctx := r.Context()

queries := r.URL.Query()
ctx = context.WithValue(ctx, CTX_QUERIES, &queries)

response := serviceFunc(&ctx)

w.Header().Set(string(HEADER_CONTENT_TYPE), "application/json")
// w.Header().Set("Access-Control-Allow-Origin", "*")
// w.Header().Set("Access-Control-Allow-Methods", "GET,POST,PUT,PATCH,DELETE,HEAD,OPTIONS")
// w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Origin, Accept, Authorization, Content-Length, X-Requested-With")

json.NewEncoder(w).Encode(response)
}
}

func HandleHTTPPost[InputDtoType any, OutputDtoType any](serviceFunc func(ctx *context.Context, dto *InputDtoType) *OutputDtoType) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {

ctx := r.Context()
pCtx := &ctx

var dto InputDtoType

_ = json.NewDecoder(r.Body).Decode(&dto)

response := serviceFunc(pCtx, &dto)
w.Header().Set(string(HEADER_CONTENT_TYPE), "application/json")
// w.Header().Set("Access-Control-Allow-Origin", "*")
// w.Header().Set("Access-Control-Allow-Methods", "GET,POST,PUT,PATCH,DELETE,HEAD,OPTIONS")
// w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Origin, Accept, Authorization, Content-Length, X-Requested-With")

print(w.Header().Get("Access-Control-Allow-Origin"))
json.NewEncoder(w).Encode(response)
}
}

func HandleHTTPFileUpload[OutputDtoType any](serviceFunc func(ctx *context.Context) *OutputDtoType) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {

ctx := r.Context()

err := r.ParseMultipartForm(10 << 20) // 10MB limit
if err != nil {
panic(err.Error())
}

formValues := r.Form
ctx = context.WithValue(ctx, "FORM_VALUES", &formValues)

fileHeaders := r.MultipartForm.File["image"]

for _, fileHeader := range fileHeaders {

file, err := fileHeader.Open()
if err != nil {
panic(err.Error())
}

buffer := bytes.NewBuffer(nil)

_, err = io.Copy(buffer, file)
if err != nil {
panic(err.Error())
}

file.Seek(0, io.SeekStart)

newFile := &FileData{
File: file,
Header: fileHeader,
Filename: fileHeader.Filename,
}
ctx = context.WithValue(ctx, "CTX_FILES", newFile)
}

response := serviceFunc(&ctx)

w.Header().Set(string(HEADER_CONTENT_TYPE), "application/json")
// w.Header().Set("Access-Control-Allow-Origin", "*")
// w.Header().Set("Access-Control-Allow-Methods", "GET,POST,PUT,PATCH,DELETE,HEAD,OPTIONS")
// w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Origin, Accept, Authorization, Content-Length, X-Requested-With")

json.NewEncoder(w).Encode(response)

}
}
31 changes: 31 additions & 0 deletions Backend/common/common.helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package common

import (
"context"
"net/url"
"strings"
"time"
)

func GetQueryValueFromCtx(ctx *context.Context) *url.Values {

query := (*ctx).Value(CTX_QUERIES).(*url.Values)

return query
}

func ParseToTsQuery(str string) string {
strs := strings.Split(str, " ")

return strings.Join(strs, " & ")
}

func GmtNow() time.Time {
return time.Now().In(time.UTC)
}

func GetFormValues(ctx *context.Context) *url.Values {
formValues := (*ctx).Value("FORM_VALUES").(*url.Values)

return formValues
}
78 changes: 78 additions & 0 deletions Backend/common/common.scopes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package common

import (
"context"
"net/url"
"strconv"

"gorm.io/gorm"
)

func PaginatedScope(ctx *context.Context) func(db *gorm.DB) *gorm.DB {

return func(db *gorm.DB) *gorm.DB {

scopedDb := db
page, pageSize, sortOrder, sortColumn, searchQuery := extractPaginationValue(ctx)

if page <= 0 {
page = 1
}

if pageSize <= 0 {
pageSize = 10
}

offset := (page - 1) * pageSize

scopedDb = scopedDb.Offset(offset).Limit(pageSize)

if sortOrder == "" {
sortOrder = "desc"
}

if sortColumn == "" {
if searchQuery != "" {
searchQuery := ParseToTsQuery(searchQuery)
sortColumn = "ts_rank(search_weighted_doc, plainto_tsquery('" + searchQuery + "'))"
}
} else {
sortColumn = "published_at"
}

scopedDb.Order(sortColumn + " " + sortOrder)
return scopedDb
}

}

func extractPaginationValue(ctx *context.Context) (int, int, string, string, string) {

queryParams := (*ctx).Value(CTX_QUERIES)
if queryParams != nil {

query := queryParams.(*url.Values)

pageStr := query.Get("page")
var page int
if pageStr != "" {
page, _ = strconv.Atoi(pageStr)
}

pageSizeStr := query.Get("page_size")
var pageSize int
if pageStr != "" {
pageSize, _ = strconv.Atoi(pageSizeStr)
}

sortOrder := query.Get("sort_order")

sortColumn := query.Get("sort_column")

searchQuery := query.Get("query")

return page, pageSize, sortOrder, sortColumn, searchQuery
}

return 0, 0, "", "", ""
}
7 changes: 7 additions & 0 deletions Backend/example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
APP_PORT=8000
ENV=dev
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_USER=pice_backend
POSTGRES_PASSWORD=password
POSTGRES_DBNAME=personal
Loading