Copy files from embedFS to storage
This commit is contained in:
parent
1661c67f7f
commit
5899f7f313
2 changed files with 51 additions and 8 deletions
35
src/main.go
35
src/main.go
|
@ -1,24 +1,43 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"embed"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/otiai10/copy"
|
||||
"nikurasu.gay/static-hoster/envloader"
|
||||
"nikurasu.gay/static-hoster/helper"
|
||||
"nikurasu.gay/static-hoster/router"
|
||||
)
|
||||
|
||||
//go:embed static/*
|
||||
var assets embed.FS
|
||||
|
||||
func main() {
|
||||
var env = envloader.Load()
|
||||
if _, err := os.Stat(fmt.Sprintf("%s404.html", env.StaticDir)); errors.Is(err, os.ErrNotExist) {
|
||||
if _, err := os.Stat("/usr/share/static-hoster/html"); errors.Is(err, os.ErrNotExist) {
|
||||
os.Create(fmt.Sprintf("%s404.html", env.StaticDir))
|
||||
} else {
|
||||
copy.Copy("/usr/share/static-hoster/html", env.StaticDir)
|
||||
}
|
||||
if empty, _ := helper.IsEmpty(env.StaticDir); empty {
|
||||
copyEmbed(env.StaticDir)
|
||||
fmt.Println("Created default files")
|
||||
}
|
||||
copyEmbed(env.StaticDir)
|
||||
r := router.Create(env)
|
||||
r.Run(env.Port)
|
||||
}
|
||||
|
||||
func copyEmbed(dest string) {
|
||||
files, err := assets.ReadDir("static")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
for _, file := range files {
|
||||
fileContent, err := assets.ReadFile(fmt.Sprintf("static/%s", file.Name()))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fileName := fmt.Sprintf("%s%s", dest, file.Name())
|
||||
if err := os.WriteFile(fileName, fileContent, 0666); err != nil {
|
||||
log.Printf("Error writing default Files: %s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
24
src/static/404.html
Normal file
24
src/static/404.html
Normal file
|
@ -0,0 +1,24 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link rel="stylesheet" href="style.css">
|
||||
<title>Static Hoster</title>
|
||||
</head>
|
||||
<body>
|
||||
<div class="topBox">
|
||||
<div class="topText">
|
||||
<div class="heading">
|
||||
<h1>Oh no, site not found</h1>
|
||||
</div>
|
||||
<div class="subtext">
|
||||
<p>Error 404 - Not found :(</p>
|
||||
</div>
|
||||
</div>
|
||||
<img id="gopherImg" src="gopherPride.png" alt="A Golang gopher with a pride shirt">
|
||||
</div>
|
||||
<div class="bottomBox"></div>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in a new issue