personal-website/apps/personalprops/personalprops.go

131 lines
2.8 KiB
Go
Raw Normal View History

2023-03-15 12:32:41 +00:00
package personalprops
2023-03-17 01:16:51 +00:00
import (
"errors"
2023-05-03 21:50:08 +00:00
"net/http"
websiteapp "personalwebsite/apps"
2023-04-29 19:37:08 +00:00
"personalwebsite/webfilesystem"
2023-03-17 01:16:51 +00:00
"github.com/gin-gonic/gin"
2023-05-05 22:50:57 +00:00
"github.com/mitchellh/mapstructure"
2023-04-29 19:37:08 +00:00
"go.mongodb.org/mongo-driver/bson/primitive"
2023-03-17 01:16:51 +00:00
)
2023-03-15 12:32:41 +00:00
type PersonalPropertiesApp struct {
2023-04-29 19:37:08 +00:00
fs *webfilesystem.WebFileSystem
2023-03-17 01:16:51 +00:00
manifest websiteapp.ApplicationManifest
2023-03-15 12:32:41 +00:00
}
2023-04-29 19:37:08 +00:00
func NewPersPropsApp(webFs *webfilesystem.WebFileSystem) PersonalPropertiesApp {
2023-03-17 01:16:51 +00:00
newApp := PersonalPropertiesApp{
2023-04-29 19:37:08 +00:00
fs: webFs,
2023-03-17 01:16:51 +00:00
manifest: websiteapp.ApplicationManifest{
AppId: "personal-properties",
WindowName: "About me",
},
}
2023-03-15 12:32:41 +00:00
return newApp
}
2023-05-03 21:50:08 +00:00
func (p *PersonalPropertiesApp) Route(route *gin.RouterGroup) {
route.GET("render", func(ctx *gin.Context) {
isMobileParam := ctx.Query("isMobile")
isMobile := isMobileParam == "true"
ginH, err := p.Render()
if err != nil {
ctx.JSON(http.StatusInternalServerError, "TODO") //TODO
2023-05-05 22:50:57 +00:00
return
2023-05-03 21:50:08 +00:00
}
if isMobile {
ctx.HTML(http.StatusOK, "personal-properties/mobile-app.tmpl", ginH)
} else {
ctx.HTML(http.StatusOK, "personal-properties/app.tmpl", ginH)
}
})
}
2023-03-17 01:16:51 +00:00
func (p *PersonalPropertiesApp) GetManifest() websiteapp.ApplicationManifest {
return p.manifest
}
func (p *PersonalPropertiesApp) GetId() string {
return p.manifest.AppId
}
2023-04-29 19:37:08 +00:00
func (p *PersonalPropertiesApp) Render() (gin.H, error) {
2023-05-05 22:50:57 +00:00
propsFile, err := p.fs.NewRead("/home/user/About Me.props")
2023-04-29 19:37:08 +00:00
if err != nil {
2023-05-05 22:50:57 +00:00
println(err.Error())
2023-04-29 19:37:08 +00:00
return nil, err
}
2023-05-05 22:50:57 +00:00
if propsFile.Type != "pers-props" {
return nil, errors.New("file is not a directory")
}
2023-05-05 22:50:57 +00:00
fileData, err := castToPersPropsData(propsFile.Data)
if err != nil {
2023-05-05 22:50:57 +00:00
println(err.Error())
return nil, err
}
2023-05-05 22:50:57 +00:00
_ = fileData
// file := castToFile(fileRaw)
// return file, nil
// if propsFile.Data == nil || propsFile.Type != "pers-props" {
// return nil, errors.New("bad file")
// }
// props, err := castToPersPropsData(propsFileRaw)
// _ = props
2023-05-01 12:32:41 +00:00
// if err != nil {
// return nil, err
// }
2023-05-05 22:50:57 +00:00
return gin.H{
// "headerProps": props.Header,
// "allprops": props.Props,
}, nil
}
2023-05-05 22:50:57 +00:00
func castToPersPropsData(fileData interface{}) (*PropertiesFileData, error) {
propsData := PropertiesFileData{
Header.Name: fileData.(primitive.D).Map()["name"].(string),
}
err := mapstructure.Decode(fileData.(primitive.D).Map(), &propsData)
if err != nil {
return nil, err
2023-04-29 19:37:08 +00:00
}
2023-05-05 22:50:57 +00:00
return &propsData, nil
2023-04-29 19:37:08 +00:00
2023-03-15 12:32:41 +00:00
}
type HeaderIsland struct {
2023-05-05 22:50:57 +00:00
Name string `bson:"name"`
IconPath string `bson:"iconpath"`
Info1 string `bson:"info1"`
Info2 string `bson:"info2"`
2023-03-15 12:32:41 +00:00
}
2023-03-17 01:16:51 +00:00
2023-03-17 16:13:41 +00:00
type PropElement struct {
2023-03-18 02:16:32 +00:00
Key string
KeyComments []string
Values []string
2023-03-17 01:16:51 +00:00
}
2023-03-18 00:34:56 +00:00
type PropIsland struct {
Header string
Props []PropElement
}
2023-04-29 19:37:08 +00:00
type PropertiesFile struct {
Props []PropIsland `bson:"props"`
}
2023-05-05 22:50:57 +00:00
type PropertiesFileData struct {
Header HeaderIsland `bson:"header"`
Props []PropIsland `bson:"props"`
}