Skip to content

Commit

Permalink
Merge pull request #504 from trheyi/main
Browse files Browse the repository at this point in the history
[add] sui file server (dev)
  • Loading branch information
trheyi authored Nov 22, 2023
2 parents 293c487 + e8db2a7 commit 06a73db
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 117 deletions.
57 changes: 0 additions & 57 deletions service/fs/spa.go

This file was deleted.

57 changes: 0 additions & 57 deletions service/fs/sui.go

This file was deleted.

8 changes: 8 additions & 0 deletions service/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ func withStaticFileServer(c *gin.Context) {
}
}

// Sui file server
if strings.HasSuffix(c.Request.URL.Path, ".sui") {
data := []byte(`SUI Server: ` + c.Request.URL.Path)
c.Data(200, "text/html; charset=utf-8", data)
c.Done()
return
}

// static file server
AppFileServer.ServeHTTP(c.Writer, c.Request)
c.Abort()
Expand Down
14 changes: 11 additions & 3 deletions sui/core/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,20 @@ func (page *Page) Compile(option *BuildOption) (string, error) {
}

if page.Codes.DATA.Code != "" {
doc.Find("body").AppendHtml(`<script name="data" type="json">` +
fmt.Sprintf("\n%s\n", page.Codes.DATA.Code) +
"</script>\n",
doc.Find("body").AppendHtml("\n\n" + `<script name="data" type="json">` + "\n" +
page.Codes.DATA.Code +
"\n</script>\n\n",
)
}

// add the route data
doc.Find("body").AppendHtml(`<script name="route" type="json">` + "\n" +
fmt.Sprintf(
`{"sui": "%s", "template": "%s", "route": "%s"}`,
page.SuiID, page.TemplateID, page.Route,
) +
"\n</script>\n")

html, err := doc.Html()
if err != nil {
return "", err
Expand Down
98 changes: 98 additions & 0 deletions sui/core/fs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package core

import (
"bytes"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"time"

"github.com/gin-gonic/gin"
"github.com/yaoapp/gou/application"
)

// SuiFile is a custom implementation of http.File
type SuiFile struct {
reader io.Reader
size int64
name string
}

// SuiFileInfo is a custom implementation of os.FileInfo
type SuiFileInfo struct {
size int64
name string
}

// Open is a custom implementation of http.FileSystem
func Open(c *gin.Context, path string, name string) (http.File, error) {
root := application.App.Root()
pathName := filepath.Join(root, path, name)
data := []byte(fmt.Sprintf(`SUI Server: %s`, pathName))
return &SuiFile{
reader: bytes.NewReader(data),
size: int64(len(data)),
name: filepath.Base(name) + ".html",
}, nil
}

// Close is a custom implementation of the Close method for SuiFile
func (file *SuiFile) Close() error {
file.reader = nil
return nil
}

// Read is a custom implementation of the Read method for SuiFile
func (file *SuiFile) Read(b []byte) (n int, err error) {
// Use the custom SuiFile reader
return file.reader.Read(b)
}

// Seek is a custom implementation of the Seek method for SuiFile
func (file *SuiFile) Seek(offset int64, whence int) (int64, error) {
// Use the Seek method of the underlying os.File
return 0, nil
}

// Readdir is a custom implementation of the Readdir method for SuiFile
func (file *SuiFile) Readdir(n int) ([]os.FileInfo, error) {
// Use the Readdir method of the underlying os.File
return nil, nil
}

// Stat is a custom implementation of the Stat method for SuiFile
func (file *SuiFile) Stat() (os.FileInfo, error) {
return &SuiFileInfo{size: file.size, name: file.name}, nil
}

// Size is a custom implementation of os.FileInfo
func (info *SuiFileInfo) Size() int64 {
return info.size
}

// Name is a custom implementation of os.FileInfo
func (info *SuiFileInfo) Name() string {
return info.name
}

// Mode is a custom implementation of os.FileInfo
func (info *SuiFileInfo) Mode() os.FileMode {
return 0
}

// ModTime is a custom implementation of os.FileInfo
func (info *SuiFileInfo) ModTime() time.Time {
return time.Now()
}

// IsDir is a custom implementation of os.FileInfo
func (info *SuiFileInfo) IsDir() bool {
return false
}

// Sys is a custom implementation of os.FileInfo
func (info *SuiFileInfo) Sys() interface{} {
return nil
}
1 change: 1 addition & 0 deletions sui/core/fs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package core

0 comments on commit 06a73db

Please sign in to comment.