personal-website/apps/personalprops/personalprops.go

140 lines
3.2 KiB
Go
Raw Permalink Normal View History

2023-03-15 12:32:41 +00:00
package personalprops
2023-03-17 01:16:51 +00:00
import (
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-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) {
2023-05-07 01:00:22 +00:00
route.GET("writeMock", func(ctx *gin.Context) {
err := p.WriteMock()
if err != nil {
ctx.Status(http.StatusInternalServerError)
}
ctx.Status(http.StatusOK)
})
2023-05-03 21:50:08 +00:00
route.GET("render", func(ctx *gin.Context) {
isMobileParam := ctx.Query("isMobile")
isMobile := isMobileParam == "true"
2023-05-07 01:00:22 +00:00
filePath := ctx.Query("path")
if filePath == "" {
ctx.Status(http.StatusBadRequest)
return
}
ginH, err := p.Render(filePath)
2023-05-03 21:50:08 +00:00
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-05-07 01:00:22 +00:00
func (p *PersonalPropertiesApp) WriteMock() error {
fileHeader := webfilesystem.FileHeader{
MongoId: primitive.NewObjectID(),
Name: "aboutme.props",
Type: "personal-properties",
Icon: "",
2023-04-29 19:37:08 +00:00
}
2023-05-07 01:00:22 +00:00
fileData := PropertiesFileData{
Header: HeaderIsland{
Name: "Test Name",
IconPath: "test icon path",
Info1: "Info1",
Info2: "Info2",
},
Props: []PropIsland{
{
Header: "Test Prop Header",
Props: []PropElement{
{
Key: "Test key",
KeyComments: []string{"Test key comment 1", "Test key comment 1"},
Values: []string{"test value1", "test value2"},
},
},
},
{
Header: "Test Prop Header 2",
Props: []PropElement{
{
Key: "Test key",
KeyComments: []string{"Test key comment 1", "Test key comment 1"},
Values: []string{"test value1", "test value2"},
},
},
},
},
}
2023-05-07 01:00:22 +00:00
_, _, err := p.fs.Write("/home/user/aboutme.props", &fileHeader, fileData)
return err
}
2023-05-05 22:50:57 +00:00
2023-05-07 01:00:22 +00:00
func (p *PersonalPropertiesApp) Render(filePath string) (gin.H, error) {
propsData := PropertiesFileData{}
_, err := p.fs.Read(filePath, &propsData)
if err != nil {
2023-05-05 22:50:57 +00:00
return nil, err
}
2023-05-05 22:50:57 +00:00
return gin.H{
2023-05-07 01:00:22 +00:00
"headerProps": propsData.Header,
"allprops": propsData.Props,
2023-05-05 22:50:57 +00:00
}, nil
}
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-05-05 22:50:57 +00:00
type PropertiesFileData struct {
Header HeaderIsland `bson:"header"`
Props []PropIsland `bson:"props"`
}