Initial commit

This commit is contained in:
Andre Henriques 2023-09-18 00:26:42 +01:00
commit 1fd025e6d6
17 changed files with 458 additions and 0 deletions

44
.air.toml Normal file
View File

@ -0,0 +1,44 @@
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"
[build]
args_bin = []
bin = "./tmp/main"
cmd = "go build -o ./tmp/main ."
delay = 0
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
exclude_file = []
exclude_regex = ["_test.go"]
exclude_unchanged = false
follow_symlink = false
full_bin = ""
include_dir = []
include_ext = ["go", "tpl", "tmpl", "html"]
include_file = []
kill_delay = "0s"
log = "build-errors.log"
poll = false
poll_interval = 0
rerun = false
rerun_delay = 500
send_interrupt = true
stop_on_error = true
[color]
app = ""
build = "yellow"
main = "magenta"
runner = "green"
watcher = "cyan"
[log]
main_only = false
time = false
[misc]
clean_on_exit = false
[screen]
clear_on_rebuild = false
keep_scroll = true

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
tmp/

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module andr3h3nriqu3s.com/m
go 1.20

196
handler.go Normal file
View File

@ -0,0 +1,196 @@
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"strings"
)
func baseLoadTemplate(base string, path string) (*template.Template, any) {
return template.New(base).ParseFiles(
"./views/"+base,
"./views/"+path,
"./views/partials/header.html",
)
}
func loadTemplate(path string) (*template.Template, any) {
return baseLoadTemplate("layout.html", path)
}
func LoadView(writer http.ResponseWriter, path string, data interface{}) {
tmpl, err := loadTemplate(path)
if err != nil {
fmt.Printf("Failed to load view %s\n", path)
fmt.Println(err)
if path == "500.html" {
writer.Write([]byte("<h1>Failed to load 500.html check console for more info</h1>"))
} else {
LoadView(writer, "500.html", nil)
}
return
}
if err := tmpl.Execute(writer, data); err != nil {
fmt.Printf("Failed to load view %s\n", path)
fmt.Println(err)
writer.WriteHeader(http.StatusInternalServerError)
if path == "500.html" {
writer.Write([]byte("<h1>Failed to load 500.html check console for more info</h1>"))
} else {
LoadView(writer, "500.html", nil)
}
return
}
}
/** Only returns the html without template */
func LoadHtml(writer http.ResponseWriter, path string, data interface{}) {
tmpl, err := baseLoadTemplate("html.html", path)
if err != nil {
fmt.Printf("Failed to load template %s\n", path)
fmt.Println(err)
writer.WriteHeader(http.StatusInternalServerError)
if path == "500.html" {
writer.Write([]byte("<h1>Failed to load 500.html check console for more info</h1>"))
} else {
LoadHtml(writer, "500.html", nil)
}
return
}
if err := tmpl.Execute(writer, data); err != nil {
fmt.Printf("Failed to execute template %s\n", path)
fmt.Println(err)
writer.WriteHeader(http.StatusInternalServerError)
if path == "500.html" {
writer.Write([]byte("<h1>Failed to load 500.html check console for more info</h1>"))
} else {
LoadHtml(writer, "500.html", nil)
}
return
}
}
type Error struct {
code int
msg *string
}
type AnswerType int
const (
NORMAL AnswerType = iota
HTML
JSON
HTMLFULL
)
func LoadBasedOnAnswer(ans AnswerType, w http.ResponseWriter, path string, data map[string]interface{}) {
if ans == NORMAL {
LoadView(w, path, nil)
return
} else if ans == HTML {
LoadHtml(w, path, nil)
return
} else if ans == HTMLFULL {
if data == nil {
LoadHtml(w, path, map[string]interface{}{
"App": true,
})
} else {
data["App"] = true
LoadHtml(w, path, data)
}
return
} else if ans == JSON {
panic("TODO JSON!")
} else {
panic("unreachable")
}
}
type Handler interface {
New()
Startup()
Get(fn func(mode AnswerType, w http.ResponseWriter, r *http.Request) *Error)
}
type Handle struct{}
func handleError(err *Error, answerType AnswerType, w http.ResponseWriter) {
if err != nil {
w.WriteHeader(err.code)
if err.code == 404 {
LoadBasedOnAnswer(answerType, w, "404.html", nil)
return
}
if err.msg != nil {
w.Write([]byte(*err.msg))
}
}
}
func (x Handle) Get(path string, fn func(mode AnswerType, w http.ResponseWriter, r *http.Request) *Error) {
http.HandleFunc("/"+path, func(w http.ResponseWriter, r *http.Request) {
if r.Method != "Get" {
header := r.Header.Get("REQUEST-TYPE")
if header == "html" {
handleError(fn(HTMLFULL, w, r), HTMLFULL, w)
return
}
handleError(fn(NORMAL, w, r), NORMAL, w)
}
})
http.HandleFunc("/api/html/"+path, func(w http.ResponseWriter, r *http.Request) {
if r.Method != "Get" {
handleError(fn(HTML, w, r), HTML, w)
}
})
http.HandleFunc("/api/json/"+path, func(w http.ResponseWriter, r *http.Request) {
if r.Method != "Get" {
handleError(fn(JSON, w, r), JSON, w)
}
})
}
func (x Handle) New() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
w.WriteHeader(http.StatusNotFound)
LoadView(w, "404.html", nil)
return
}
LoadView(w, "index.html", nil)
})
http.HandleFunc("/styles/", func(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path[len("/styles/"):]
if !strings.HasSuffix(path, ".css") {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("File not found"))
return
}
t, err := template.ParseFiles("./views/styles/" + path)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Failed to load template"))
fmt.Println("Error:")
fmt.Println(err)
return
}
w.Header().Set("Content-Type", "text/css; charset=utf-8")
t.Execute(w, nil)
})
}
func (x Handle) Startup() {
fmt.Printf("Starting up!\n")
log.Fatal(http.ListenAndServe(":8000", nil))
}

26
main.go Normal file
View File

@ -0,0 +1,26 @@
package main
import (
"fmt"
"net/http"
)
func main() {
fmt.Println("Starting server on :8000!")
handle := Handle{}
handle.New()
handle.Get("login", func(mode AnswerType, w http.ResponseWriter, r *http.Request) *Error {
if mode == JSON {
return &Error{
code: 404,
};
}
LoadBasedOnAnswer(mode, w, "login.html", nil)
return nil;
})
handle.Startup()
}

13
shell.nix Normal file
View File

@ -0,0 +1,13 @@
let
unstable = import (fetchTarball https://nixos.org/channels/nixos-unstable/nixexprs.tar.xz) { };
in
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
nativeBuildInputs = with unstable; [
go
gopls
air
nodePackages.vscode-css-languageserver-bin
nodePackages.vscode-html-languageserver-bin
];
}

8
templates/400.gohtml Normal file
View File

@ -0,0 +1,8 @@
<html>
<head>
<title>
</title>
</head>
<head>
</head>
</html>

3
templates/404.gohtml Normal file
View File

@ -0,0 +1,3 @@
<h1>
The page you were looking for could not be found!
</h1>

10
templates/index.gohtml Normal file
View File

@ -0,0 +1,10 @@
<html>
<head>
<title>
Stuff TODO change the name
</title>
</head>
<body>
Hi Bitches
</body>
</html>

15
views/404.html Normal file
View File

@ -0,0 +1,15 @@
{{ define "mainbody" }}
<div class="page404">
<div>
<h1>
404
</h1>
<h2>
Page Not found
</h2>
<div class="description">
The page you were looking for does not exist
</div>
</div>
</div>
{{ end }}

6
views/500.html Normal file
View File

@ -0,0 +1,6 @@
{{ define "head" }}
<title>
Error Page
</title>
{{ end }}
{{ define "body" }}Heyyyyyy Err {{ end }}

19
views/html.html Normal file
View File

@ -0,0 +1,19 @@
{{ if .Full }}
<body>
{{ block "body" . }}
{{ block "header.html" . }} {{end}}
<div class="app">
{{ block "mainbody" . }} {{end}}
</div>
{{end}}
</body>
{{ else }}
{{if .App }}
<div class="app">
{{ block "mainbody" . }} {{end}}
</div>
{{ else }}
{{ block "mainbody" . }} {{end}}
{{end}}
{{end}}

6
views/index.html Normal file
View File

@ -0,0 +1,6 @@
{{ define "title"}} Home : AI Stuff {{ end }}
{{ define "mainbody" }}
hey bitches
{{ end }}

26
views/layout.html Normal file
View File

@ -0,0 +1,26 @@
<!DOCTYPE html>
<html>
<head>
{{ block "header" . }}
<title>
{{ block "title" . }}
Ai stuff
{{ end }}
</title>
{{ end }}
<link rel="stylesheet" href="/styles/main.css" >
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&display=swap" rel="stylesheet">
{{ block "other_imports" . }} {{end}}
</head>
<body>
<script src="https://unpkg.com/htmx.org@1.9.5"></script>
{{ block "body" . }}
{{ block "header.html" . }} {{end}}
<div class="app">
{{ block "mainbody" . }} {{end}}
</div>
{{end}}
</body>
</html>

15
views/login.html Normal file
View File

@ -0,0 +1,15 @@
{{ define "title"}} Home : AI Stuff {{ end }}
{{ define "mainbody" }}
<form>
<div>
<label for="email">Email</label>
<input name="email" required />
</div>
<div>
<label for="password">Password</label>
<input name="password" required type="password" />
</div>
</form>
{{ end }}

View File

@ -0,0 +1,10 @@
<nav>
<ul>
<div class="expand"></div>
<li>
<a hx-get="/login" hx-headers='{"REQUEST-TYPE": "html"}' hx-push-url="true" hx-swap="outerHTML" hx-target=".app">
Login
</a>
</li>
</ul>
</nav>

57
views/styles/main.css Normal file
View File

@ -0,0 +1,57 @@
* {
box-sizing: border-box;
font-family: 'Roboto', sans-serif;
}
body {
margin: 0;
padding: 0;
}
/* Nav bar */
nav {
background: #ececec;
margin: 0;
box-shadow: 0 0 8px 1px #888888ef;
height: 60px;
}
nav ul {
display: flex;
margin: 0;
padding: 20px 40px;
}
nav ul li {
list-style: none;
}
nav ul .expand {
flex-grow: 1
}
nav ul li a {
text-decoration: none;
color: black;
}
/* 404 page */
.page404 {
display: grid;
place-items: center;
height: calc(100vh - 60px);
text-align: center;
}
.page404 h1 {
font-size: 10em;
margin: 0;
}
.page404 h2 {
font-size: 5em;
margin: 0;
margin-bottom: 0.3em;
}
.page404 div.description {
font-size: 1.5em;
}