feat: initial setup

This commit is contained in:
Gustavo "Guz" L. de Mello
2024-05-20 18:40:43 -03:00
parent 520597508c
commit f1847f214f
7 changed files with 206 additions and 0 deletions

45
api/hello.go Normal file
View File

@@ -0,0 +1,45 @@
package api
import (
"encoding/json"
"fmt"
"io"
"math/rand/v2"
"net/http"
)
type helloObj struct {
Language string
Hello string
}
func getHelloList() ([]helloObj, error) {
res, err := http.Get("https://raw.githubusercontent.com/novellac/multilanguage-hello-json/master/hello.json")
if err != nil {
return nil, err
}
bytes, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
var hellos []helloObj
err = json.Unmarshal(bytes, &hellos)
if err != nil {
return nil, err
}
return hellos, nil
}
func Hello(w http.ResponseWriter, r *http.Request) {
hellos, err := getHelloList()
var hello string
if err != nil {
hello = "Welcome!"
} else {
hello = hellos[rand.IntN(len(hellos)-1)].Hello
}
fmt.Fprint(w, hello)
}

115
cmd/build-static/main.go Normal file
View File

@@ -0,0 +1,115 @@
package main
import (
"context"
"flag"
"io"
"io/fs"
"log"
"os"
"path/filepath"
"strings"
"github.com/a-h/templ"
"guz.one/pages"
)
const PERMISSIONS = 0755
type Page struct {
path string
component templ.Component
}
type Writer struct {
root *string
pages []Page
context context.Context
}
func (w Writer) writeFile(path string, writer func(ctx context.Context, w io.Writer) error) {
directory := filepath.Dir(path)
err := os.MkdirAll(directory, PERMISSIONS)
if err != nil {
log.Fatal(err)
}
f, err := os.Create(path)
if err != nil {
log.Fatal(err)
}
defer f.Close()
err = writer(w.context, f)
if err != nil {
log.Fatal(err)
}
}
func (w Writer) WriteAll() {
for _, page := range w.pages {
p := filepath.Join(*w.root, page.path)
log.Printf("Writing page %s", p)
w.writeFile(p, page.component.Render)
}
}
func main() {
dir := flag.String("d", "./dist", "the directory to write the files")
staticDir := flag.String("s", "./static", "the directory to copy static files from")
pages := []Page{
{"index.html", pages.Homepage()},
}
w := Writer{dir, pages, context.Background()}
w.WriteAll()
a, err := filepath.Abs(*staticDir)
if err != nil {
log.Fatal(err)
}
copyFile := func(root string, dest *string) func(string, fs.DirEntry, error) error {
return func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
} else if path == root {
return nil
}
c, err := os.ReadFile(path)
if err != nil {
return err
}
p := strings.TrimPrefix(path, root)
log.Printf("Copying static file %s to %s directory", p, *dest)
p = filepath.Join(*dest, p)
err = os.MkdirAll(filepath.Dir(p), PERMISSIONS)
if err != nil {
return err
}
f, err := os.Create(p)
if err != nil {
return err
}
defer f.Close()
b, err := f.Write(c)
if err != nil {
return err
}
log.Printf("Wrote %v bytes in %s", b, p)
return nil
}
}
err = filepath.WalkDir(a, copyFile(a, dir))
if err != nil {
log.Fatal(err)
}
}

22
cmd/dev-server/main.go Normal file
View File

@@ -0,0 +1,22 @@
package main
import (
"context"
"log"
"net/http"
"guz.one/api"
"guz.one/pages"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/api/hello", api.Hello)
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
err := pages.Homepage().Render(context.Background(), w)
_ = err
})
log.Fatal(http.ListenAndServe(":5432", mux))
}

12
layouts/page.templ Normal file
View File

@@ -0,0 +1,12 @@
package layouts
templ Page(title string) {
<html>
<head>
<title>{ title }</title>
</head>
<body>
{ children... }
</body>
</html>
}

10
pages/homepage.templ Normal file
View File

@@ -0,0 +1,10 @@
package pages
import (
"guz.one/layouts"
)
templ Homepage() {
<img src="/logo-013-dark.svg" alt="" width="100" height="100"/>
@layouts.Page("Hello world")
}

1
static/logo-013-dark.svg Normal file
View File

@@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 186.23 93.51"><defs><style>.cls-1{fill:#111;}</style></defs><g id="logos"><g id="dark"><polygon class="cls-1" points="68.37 51.8 22.71 2.1 5.59 17.14 51.25 66.85 68.37 51.8"/><rect class="cls-1" x="69.45" width="23.01" height="61.64"/><polygon class="cls-1" points="145.09 24.19 185.73 24.19 185.73 1.68 105.09 1.68 146.25 70.92 0 71 0.01 93.51 186.23 93.41 145.09 24.19"/></g></g></svg>

After

Width:  |  Height:  |  Size: 440 B

1
static/robots.txt Normal file
View File

@@ -0,0 +1 @@
test