update: general formatting and cleanup

This commit is contained in:
ngn 2024-08-14 00:32:48 +03:00
parent 25f30b51f0
commit 87a9236e01
15 changed files with 402 additions and 344 deletions

2
.gitignore vendored
View File

@ -1,2 +1,4 @@
docker-compose.yml
compose.yml
content
website

9
Makefile Normal file
View File

@ -0,0 +1,9 @@
all: website
website: */*.go *.go
go build -o $@
format:
gofmt -s -w .
.PHONY: format

View File

@ -1,18 +1,33 @@
# website | MatterLinux Website
# website | MatterLinux website
Soruce code of Matterlinux's offical website, which is
located at [matterlinux.xyz](https://matterlinux.xyz)
### Deployment
Web server can be built and deployed with docker:
Web server can be built and deployed with docker compose using the following
configuration file:
```yaml
version: "3"
services:
website:
image: matterwebsite
restart: unless-stopped
build:
context: ./
ports:
- "127.0.0.1:9878:9878"
volumes:
- "./content:/app/content"
```
docker build --tag matterwebsite .
After saving the configuration file, you can build and run the docker container:
```bash
docker-compose up -d
```
### Managing Content
### Managing content
Website content can be managed by editing JSON and markdown files
under the `content` direcotry.
Matterlinux's website content directory can be found in the
MatterLinux's website content directory can be found in the
[content](https://git.matterlinux.xyz/Matter/content)
repository.

View File

@ -1,12 +0,0 @@
version: "3"
services:
website:
image: matterwebsite
restart: unless-stopped
build:
context: ./
ports:
- "127.0.0.1:9878:9878"
volumes:
- "./content:/app/content"

View File

@ -11,86 +11,86 @@ import (
const CONTENT_PATH string = "content"
type Content struct{
Dir string
Name string
type Content struct {
Dir string
Name string
Title string `json:"title"`
ID string `json:"id"`
Title string `json:"title"`
ID string `json:"id"`
Md string
HTML template.HTML
Md string
HTML template.HTML
Date string `json:"date"`
Author string `json:"author"`
Date string `json:"date"`
Author string `json:"author"`
}
func ListContent(dir string) ([]Content, error) {
var res []Content
var res []Content
dirpath := path.Join(CONTENT_PATH, dir)
entries, err := os.ReadDir(dirpath)
if err != nil {
return nil, errors.New("Cannot readdir: "+dirpath)
}
dirpath := path.Join(CONTENT_PATH, dir)
entries, err := os.ReadDir(dirpath)
if err != nil {
return nil, errors.New("Cannot readdir: " + dirpath)
}
for _, e := range entries {
if e.IsDir() {
subres, err := ListContent(path.Join(dir, e.Name()))
if err != nil {
return res, err
}
res = append(res, subres...)
}
for _, e := range entries {
if e.IsDir() {
subres, err := ListContent(path.Join(dir, e.Name()))
if err != nil {
return res, err
}
res = append(res, subres...)
}
if(!strings.HasSuffix(e.Name(), ".json")) {
continue
}
if !strings.HasSuffix(e.Name(), ".json") {
continue
}
var con Content
jsonfile := path.Join(dirpath, e.Name())
jsoncon, err := os.ReadFile(jsonfile)
var con Content
jsonfile := path.Join(dirpath, e.Name())
jsoncon, err := os.ReadFile(jsonfile)
if err != nil {
return nil, errors.New("Cannot get JSON: "+jsonfile)
}
if err != nil {
return nil, errors.New("Cannot get JSON: " + jsonfile)
}
err = json.Unmarshal(jsoncon, &con)
if err != nil {
return nil, errors.New("Cannot parse JSON: "+jsonfile)
}
err = json.Unmarshal(jsoncon, &con)
if err != nil {
return nil, errors.New("Cannot parse JSON: " + jsonfile)
}
con.Dir = dir
con.Name = strings.Split(e.Name(), ".")[0]
res = append(res, con)
}
con.Dir = dir
con.Name = strings.Split(e.Name(), ".")[0]
res = append(res, con)
}
return res, nil
return res, nil
}
func GetContent(dir string, name string) (Content, error) {
var res Content
jsonfile := path.Join(CONTENT_PATH, dir, name+".json")
mdfile := path.Join(CONTENT_PATH, dir, name+".md")
var res Content
jsonfile := path.Join(CONTENT_PATH, dir, name+".json")
mdfile := path.Join(CONTENT_PATH, dir, name+".md")
res.Dir = dir
res.Name = name
res.Dir = dir
res.Name = name
jsoncontent, err := os.ReadFile(jsonfile)
if err != nil {
return res, errors.New("Cannot read JSON file: "+jsonfile)
}
jsoncontent, err := os.ReadFile(jsonfile)
if err != nil {
return res, errors.New("Cannot read JSON file: " + jsonfile)
}
rawcontent, err := os.ReadFile(mdfile)
if err != nil {
return res, errors.New("Cannot read markdown file: "+mdfile)
}
rawcontent, err := os.ReadFile(mdfile)
if err != nil {
return res, errors.New("Cannot read markdown file: " + mdfile)
}
if(json.Unmarshal(jsoncontent, &res)!= nil) {
return res, errors.New("Cannot parse JSON: "+jsonfile)
}
if json.Unmarshal(jsoncontent, &res) != nil {
return res, errors.New("Cannot parse JSON: " + jsonfile)
}
res.Md = string(rawcontent)
res.HTML = template.HTML(ParseMarkdown(res.Md))
return res, nil
res.Md = string(rawcontent)
res.HTML = template.HTML(ParseMarkdown(res.Md))
return res, nil
}

View File

@ -12,44 +12,44 @@ import (
// python3 -c "import string; print(string.ascii_letters+string.digits+\" /,_-?!'\\\"\")"
var valid string = `abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 /,_-?!'"`
func IsStringValid(s string) bool{
for _, c := range s {
if !strings.Contains(valid, string(c)){
fmt.Printf("%c\n", c)
return false
}
}
return true
func IsStringValid(s string) bool {
for _, c := range s {
if !strings.Contains(valid, string(c)) {
fmt.Printf("%c\n", c)
return false
}
}
return true
}
func RenderError(c *fiber.Ctx, code int) error{
var msg string = "Server Error"
c.Status(code)
func RenderError(c *fiber.Ctx, code int) error {
var msg string = "Server Error"
c.Status(code)
switch code {
case 404:
msg = "Not Found"
}
switch code {
case 404:
msg = "Not Found"
}
return c.Render("error", fiber.Map{
"msg": msg,
})
return c.Render("error", fiber.Map{
"msg": msg,
})
}
func TimeFromString(date string) (time.Time, error) {
res, err := time.Parse("02/01/06", date)
if err == nil {
return res, nil
}
return time.Now(), nil
res, err := time.Parse("02/01/06", date)
if err == nil {
return res, nil
}
return time.Now(), nil
}
func ParseMarkdown(md string) string {
ext := blackfriday.FencedCode
ext |= blackfriday.BackslashLineBreak
ext |= blackfriday.Strikethrough
ext |= blackfriday.Tables
ext |= blackfriday.NoEmptyLineBeforeBlock
ext := blackfriday.FencedCode
ext |= blackfriday.BackslashLineBreak
ext |= blackfriday.Strikethrough
ext |= blackfriday.Tables
ext |= blackfriday.NoEmptyLineBeforeBlock
return string(blackfriday.Run([]byte(md), blackfriday.WithExtensions(ext)))
return string(blackfriday.Run([]byte(md), blackfriday.WithExtensions(ext)))
}

23
log/log.go Normal file
View File

@ -0,0 +1,23 @@
package log
import (
"fmt"
"time"
)
func Log(p string, f string, args ...interface{}) {
now := time.Now()
nstr := now.Format("[02/01/06 15:04:05]")
fmt.Printf("%s -%s- ", nstr, p)
fmt.Printf(f, args...)
fmt.Println()
}
func Info(f string, args ...interface{}) {
Log("INFO", f, args...)
}
func Error(f string, args ...interface{}) {
Log("ERROR", f, args...)
}

57
main.go
View File

@ -1,6 +1,6 @@
/*
* website | MatterLinux Official Website
* website | MatterLinux website
* MatterLinux 2023-2024 (https://matterlinux.xyz)
* This program is free software: you can redistribute it and/or modify
@ -21,44 +21,43 @@
package main
import (
"log"
"git.matterlinux.xyz/Matter/website/lib"
"git.matterlinux.xyz/Matter/website/log"
"git.matterlinux.xyz/Matter/website/routes"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/template/html/v2"
)
func main(){
log.SetFlags(log.Lshortfile | log.Ltime)
func main() {
var err error
engine := html.New("./templates", ".html")
app := fiber.New(fiber.Config{
DisableStartupMessage: true,
Views: engine,
})
app.Static("/", "./public")
app.Static("/assets", "./content/assets")
engine := html.New("./templates", ".html")
app := fiber.New(fiber.Config{
DisableStartupMessage: true,
Views: engine,
})
app.Static("/", "./public")
app.Static("/assets", "./content/assets")
app.Get("/", routes.IndexRoute)
app.Get("/download", routes.DownloadRoute)
app.Get("/", routes.GET_Index)
app.Get("/download", routes.GET_Download)
app.Get("/news", routes.NewsRoute)
app.Get("/news/:id", routes.PostRoute)
app.Get("/wiki", routes.WikiMainRoute)
app.Get("/wiki/:id", routes.WikiRoute)
app.Get("/news", routes.GET_News)
app.Get("/news/:id", routes.GET_New)
app.Get("/hub", routes.ConfigsRoute)
app.Get("/hub/:name", routes.ConfigRoute)
app.Get("/wiki", routes.GET_Wiki)
app.Get("/wiki/:id", routes.GET_WikiPage)
app.Get("*", func(c *fiber.Ctx) error {
return lib.RenderError(c, 404)
})
app.Get("/hub", routes.GET_Configs)
app.Get("/hub/:name", routes.GET_Config)
log.Println("Starting MatterLinux Website on port 9878")
err := app.Listen(":9878")
if err != nil {
log.Fatalf("Error starting server: %s", err)
}
app.Get("*", func(c *fiber.Ctx) error {
return lib.RenderError(c, 404)
})
log.Info("Starting MatterLinux website on port 9878")
if err = app.Listen(":9878"); err != nil {
log.Error("Error starting server: %s", err.Error())
}
}

View File

@ -11,7 +11,7 @@
align-items: center;
justify-content: space-between;
background: var(--dark-second);
border: solid 1px var(--bright-main);
border: solid 1px var(--bright-second);
padding: 10px;
}

View File

@ -1,7 +1,7 @@
main {
display: flex;
flex-direction: column;
padding-top: 50px;
padding-top: 40px;
gap: 10px;
}

View File

@ -4,137 +4,145 @@ import (
"bytes"
"encoding/json"
"fmt"
"log"
"os"
"path"
"strings"
"git.matterlinux.xyz/Matter/website/lib"
"git.matterlinux.xyz/Matter/website/log"
"github.com/bigkevmcd/go-configparser"
"github.com/gofiber/fiber/v2"
)
type Config struct {
Redirect string `json:"redirect"`
Image string `json:"image"`
Url string `json:"url"`
Redirect string `json:"redirect"`
Image string `json:"image"`
Url string `json:"url"`
Name string
Desc string
Author string
Keywords string
Name string
Desc string
Author string
Keywords string
}
func LoadConfig(c *Config) (error) {
agent := fiber.Get(c.Url)
code, body, errs := agent.Bytes()
if len(errs) > 0 {
return fmt.Errorf("request to %s failed", c.Url)
}
func (c *Config) Load() error {
agent := fiber.Get(c.Url)
code, body, errs := agent.Bytes()
if len(errs) > 0 {
return fmt.Errorf("request to %s failed", c.Url)
}
if code != 200 {
return fmt.Errorf("bad response from %s", c.Url)
}
if code != 200 {
return fmt.Errorf("bad response from %s", c.Url)
}
parser := configparser.New()
err := parser.ParseReader(bytes.NewReader(body))
if err != nil {
return fmt.Errorf("failed to parse %s: %s", c.Url, err.Error())
}
parser := configparser.New()
err := parser.ParseReader(bytes.NewReader(body))
if err != nil {
return fmt.Errorf("failed to parse %s: %s", c.Url, err.Error())
}
name, err := parser.Get("details", "name")
if err != nil {
return fmt.Errorf("failed get details/name: %s", c.Url)
}
keywords, err := parser.Get("details", "keywords")
if err != nil {
return fmt.Errorf("failed get details/keywords: %s", c.Url)
}
name, err := parser.Get("details", "name")
if err != nil {
return fmt.Errorf("failed get details/name: %s", c.Url)
}
author, err := parser.Get("details", "author")
if err != nil {
return fmt.Errorf("failed get details/keywords: %s", c.Url)
}
desc, err := parser.Get("details", "desc")
keywords, err := parser.Get("details", "keywords")
if err != nil {
return fmt.Errorf("failed get details/keywords: %s", c.Url)
}
c.Name = name
c.Desc = desc
c.Author = author
c.Keywords = strings.ReplaceAll(keywords, ",", ", ")
author, err := parser.Get("details", "author")
if err != nil {
return fmt.Errorf("failed get details/keywords: %s", c.Url)
}
if(!lib.IsStringValid(c.Name) ||
!lib.IsStringValid(c.Desc) ||
!lib.IsStringValid(c.Author) ||
!lib.IsStringValid(c.Keywords)){
return fmt.Errorf("keywords or name contain illegal chars: %s", c.Url)
}
desc, err := parser.Get("details", "desc")
return nil
c.Name = name
c.Desc = desc
c.Author = author
c.Keywords = strings.ReplaceAll(keywords, ",", ", ")
if !lib.IsStringValid(c.Name) ||
!lib.IsStringValid(c.Desc) ||
!lib.IsStringValid(c.Author) ||
!lib.IsStringValid(c.Keywords) {
return fmt.Errorf("keywords or name contain illegal chars: %s", c.Url)
}
return nil
}
func GetConfigs() ([]Config, error) {
var data map[string][]Config
var data map[string][]Config
configs_path := path.Join(lib.CONTENT_PATH, "configs.json")
raw, err := os.ReadFile(configs_path)
if err != nil {
return nil, err
}
configs_path := path.Join(lib.CONTENT_PATH, "configs.json")
raw, err := os.ReadFile(configs_path)
if err != nil {
return nil, err
}
err = json.Unmarshal(raw, &data)
if err != nil {
return nil, err
}
err = json.Unmarshal(raw, &data)
if err != nil {
return nil, err
}
if _, ok := data["list"]; !ok {
return nil, fmt.Errorf("json data does not contain the list key")
}
if _, ok := data["list"]; !ok {
return nil, fmt.Errorf("json data does not contain the list key")
}
for i := range data["list"] {
err = LoadConfig(&data["list"][i])
if err != nil {
return nil, err
}
}
for i := range data["list"] {
if err = data["list"][i].Load(); err != nil {
return nil, err
}
}
return data["list"], nil
return data["list"], nil
}
func ConfigsRoute(c *fiber.Ctx) error{
con, err := lib.GetContent("", "configs")
if err != nil {
log.Printf("GetContent failed: %s", err.Error())
return lib.RenderError(c, 500)
}
configs, err := GetConfigs()
if err != nil {
log.Printf("GetConfigs failed: %s", err.Error())
return lib.RenderError(c, 500)
}
return c.Render("configs", fiber.Map{
"content": con,
"list": configs,
})
func GET_Configs(c *fiber.Ctx) error {
var (
err error
con lib.Content
configs []Config
)
if con, err = lib.GetContent("", "configs"); err != nil {
log.Error("GetContent failed: %s", err.Error())
return lib.RenderError(c, 500)
}
if configs, err = GetConfigs(); err != nil {
log.Error("GetConfigs failed: %s", err.Error())
return lib.RenderError(c, 500)
}
return c.Render("configs", fiber.Map{
"content": con,
"list": configs,
})
}
func ConfigRoute(c *fiber.Ctx) error{
name := c.Params("name")
func GET_Config(c *fiber.Ctx) error {
var (
err error
name string
configs []Config
)
configs, err := GetConfigs()
if err != nil {
log.Printf("GetConfigs failed: %s", err.Error())
return lib.RenderError(c, 500)
}
name = c.Params("name")
for _, cur := range configs {
if cur.Name == name || cur.Name+".git" == name {
return c.Status(301).Redirect(cur.Redirect)
}
}
if configs, err = GetConfigs(); err != nil {
log.Error("GetConfigs failed: %s", err.Error())
return lib.RenderError(c, 500)
}
return lib.RenderError(c, 404)
for _, cur := range configs {
if cur.Name == name || cur.Name+".git" == name {
return c.Status(301).Redirect(cur.Redirect)
}
}
return lib.RenderError(c, 404)
}

View File

@ -1,20 +1,23 @@
package routes
import (
"log"
"git.matterlinux.xyz/Matter/website/lib"
"git.matterlinux.xyz/Matter/website/log"
"github.com/gofiber/fiber/v2"
)
func DownloadRoute(c *fiber.Ctx) error{
con, err := lib.GetContent("", "download")
if err != nil {
log.Printf("GetContent failed: %s", err.Error())
return lib.RenderError(c, 500)
}
func GET_Download(c *fiber.Ctx) error {
var (
con lib.Content
err error
)
return c.Render("download", fiber.Map{
"content": con,
})
if con, err = lib.GetContent("", "download"); err != nil {
log.Error("GetContent failed: %s", err.Error())
return lib.RenderError(c, 500)
}
return c.Render("download", fiber.Map{
"content": con,
})
}

View File

@ -1,20 +1,23 @@
package routes
import (
"log"
"git.matterlinux.xyz/Matter/website/lib"
"git.matterlinux.xyz/Matter/website/log"
"github.com/gofiber/fiber/v2"
)
func IndexRoute(c *fiber.Ctx) error{
con, err := lib.GetContent("", "index")
if err != nil {
log.Printf("GetContent failed: %s", err.Error())
return lib.RenderError(c, 500)
}
func GET_Index(c *fiber.Ctx) error {
var (
err error
con lib.Content
)
return c.Render("index", fiber.Map{
"readme": con,
})
if con, err = lib.GetContent("", "index"); err != nil {
log.Error("GetContent failed: %s", err.Error())
return lib.RenderError(c, 500)
}
return c.Render("index", fiber.Map{
"readme": con,
})
}

View File

@ -1,69 +1,69 @@
package routes
import (
"log"
"sort"
"git.matterlinux.xyz/Matter/website/lib"
"git.matterlinux.xyz/Matter/website/log"
"github.com/gofiber/fiber/v2"
)
func NewsRoute(c *fiber.Ctx) error {
contents, err := lib.ListContent("news")
if err != nil {
log.Printf("ListContent failed: %s", err.Error())
return lib.RenderError(c, 500)
}
func GET_News(c *fiber.Ctx) error {
contents, err := lib.ListContent("news")
if err != nil {
log.Error("ListContent failed: %s", err.Error())
return lib.RenderError(c, 500)
}
sort.Slice(contents, func(i, j int) bool {
time1, err := lib.TimeFromString(contents[i].Date)
if err != nil {
log.Printf("Bad date while sorting: %s", err.Error())
return false
}
sort.Slice(contents, func(i, j int) bool {
time1, err := lib.TimeFromString(contents[i].Date)
if err != nil {
log.Error("Bad date while sorting: %s", err.Error())
return false
}
time2, err := lib.TimeFromString(contents[j].Date)
if err != nil {
log.Printf("Bad date while sorting: %s", err.Error())
return false
}
return time1.After(time2)
})
time2, err := lib.TimeFromString(contents[j].Date)
if err != nil {
log.Error("Bad date while sorting: %s", err.Error())
return false
}
return c.Render("news", fiber.Map{
"news": contents,
})
return time1.After(time2)
})
return c.Render("news", fiber.Map{
"news": contents,
})
}
func PostRoute(c *fiber.Ctx) error {
postid := c.Params("id")
if len(postid) == 0 {
return lib.RenderError(c, 404)
}
func GET_New(c *fiber.Ctx) error {
postid := c.Params("id")
if len(postid) == 0 {
return lib.RenderError(c, 404)
}
contents, err := lib.ListContent("news")
if err != nil {
log.Printf("ListContent failed: %s", err.Error())
return lib.RenderError(c, 500)
}
contents, err := lib.ListContent("news")
if err != nil {
log.Error("ListContent failed: %s", err.Error())
return lib.RenderError(c, 500)
}
for _, con := range contents {
if(con.ID != postid) {
continue
}
for _, con := range contents {
if con.ID != postid {
continue
}
con, err = lib.GetContent(con.Dir, con.Name)
if err != nil {
log.Printf("ListContent failed: %s", err.Error())
return lib.RenderError(c, 500)
}
con, err = lib.GetContent(con.Dir, con.Name)
if err != nil {
log.Error("ListContent failed: %s", err.Error())
return lib.RenderError(c, 500)
}
return c.Render("post", fiber.Map{
"title": con.Title,
"post": con,
})
}
return c.Render("post", fiber.Map{
"title": con.Title,
"post": con,
})
}
return lib.RenderError(c, 404)
return lib.RenderError(c, 404)
}

View File

@ -1,54 +1,62 @@
package routes
import (
"log"
"git.matterlinux.xyz/Matter/website/lib"
"git.matterlinux.xyz/Matter/website/log"
"github.com/gofiber/fiber/v2"
)
func WikiMainRoute(c *fiber.Ctx) error {
con, err := lib.GetContent("wiki", "main")
if err != nil {
log.Printf("GetContent failed: %s", err.Error())
return lib.RenderError(c, 500)
}
func GET_Wiki(c *fiber.Ctx) error {
var (
con lib.Content
err error
)
return c.Render("post", fiber.Map{
"title": "Wiki",
"post": con,
})
if con, err = lib.GetContent("wiki", "main"); err != nil {
log.Error("GetContent failed: %s", err.Error())
return lib.RenderError(c, 500)
}
return c.Render("post", fiber.Map{
"title": "Wiki",
"post": con,
})
}
func WikiRoute(c *fiber.Ctx) error{
docid := c.Params("id")
if len(docid) == 0 {
return lib.RenderError(c, 404)
}
func GET_WikiPage(c *fiber.Ctx) error {
var (
contents []lib.Content
err error
)
contents, err := lib.ListContent("wiki")
if err != nil {
log.Printf("ListContent failed: %s", err.Error())
return lib.RenderError(c, 500)
}
docid := c.Params("id")
if len(docid) == 0 {
return lib.RenderError(c, 404)
}
for _, con := range contents {
if(con.ID != docid) {
continue
}
if contents, err = lib.ListContent("wiki"); err != nil {
log.Error("ListContent failed: %s", err.Error())
return lib.RenderError(c, 500)
}
con, err = lib.GetContent(con.Dir, con.Name)
if err != nil {
log.Printf("GetContent failed: %s", err.Error())
return lib.RenderError(c, 500)
}
for _, con := range contents {
if con.ID != docid {
continue
}
con.Title = "Wiki: "+con.Title
return c.Render("post", fiber.Map{
"title": con.Title,
"post": con,
})
}
con, err = lib.GetContent(con.Dir, con.Name)
if err != nil {
log.Error("GetContent failed: %s", err.Error())
return lib.RenderError(c, 500)
}
return lib.RenderError(c, 404)
con.Title = "Wiki: " + con.Title
return c.Render("post", fiber.Map{
"title": con.Title,
"post": con,
})
}
return lib.RenderError(c, 404)
}