refactor(router)!: refactor routes handling to be "servermore"
This commit is contained in:
@@ -1,30 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"flag"
|
|
||||||
"log"
|
|
||||||
|
|
||||||
"extrovert/config"
|
|
||||||
"extrovert/internals"
|
|
||||||
)
|
|
||||||
|
|
||||||
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")
|
|
||||||
|
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
w := internals.StaticWriter{
|
|
||||||
DistDir: dir,
|
|
||||||
StaticDir: staticDir,
|
|
||||||
Pages: config.ROUTES,
|
|
||||||
Context: context.Background(),
|
|
||||||
Logger: *log.Default(),
|
|
||||||
}
|
|
||||||
|
|
||||||
err := w.WriteAll()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"extrovert/config"
|
|
||||||
"extrovert/internals"
|
|
||||||
)
|
|
||||||
|
|
||||||
type VercelConfig struct {
|
|
||||||
OutputDirectory string `json:"outputDirectory"`
|
|
||||||
}
|
|
||||||
|
|
||||||
var logger = log.Default()
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
configPath := flag.String("c", "./vercel.json", "the path to the vercel.json file")
|
|
||||||
staticDir := flag.String("s", "./static", "the directory to copy static files from")
|
|
||||||
port := flag.Int("p", 8080, "the port to run the server")
|
|
||||||
|
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
configFile, err := os.ReadFile(*configPath)
|
|
||||||
if err != nil {
|
|
||||||
logger.Fatalf("Unable to read vercel.json file due to:\n%s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var c VercelConfig
|
|
||||||
err = json.Unmarshal(configFile, &c)
|
|
||||||
if err != nil {
|
|
||||||
logger.Fatalf("Unable to parse vercel.json file due to:\n%s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
w := internals.StaticWriter{
|
|
||||||
DistDir: &c.OutputDirectory,
|
|
||||||
StaticDir: staticDir,
|
|
||||||
Pages: config.ROUTES,
|
|
||||||
Context: context.Background(),
|
|
||||||
Logger: *log.Default(),
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.Print("Writing static files")
|
|
||||||
err = w.WriteAll()
|
|
||||||
if err != nil {
|
|
||||||
logger.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.Print("Starting server")
|
|
||||||
mux := http.NewServeMux()
|
|
||||||
|
|
||||||
config.APIROUTES(mux)
|
|
||||||
mux.Handle("/", http.FileServer(http.Dir(c.OutputDirectory)))
|
|
||||||
|
|
||||||
logger.Printf("Running server at port: %v", *port)
|
|
||||||
err = http.ListenAndServe(fmt.Sprintf(":%v", *port), mux)
|
|
||||||
if err != nil {
|
|
||||||
logger.Fatalf("Server crashed due to:\n%s", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
10
components/warning.templ
Normal file
10
components/warning.templ
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package components
|
||||||
|
|
||||||
|
templ Warning(title string) {
|
||||||
|
<article class="pico-background-amber-100">
|
||||||
|
<header class="pico-background-amber-200 pico-color-amber-900">{ title }</header>
|
||||||
|
<p class="pico-color-amber-900">
|
||||||
|
{ children... }
|
||||||
|
</p>
|
||||||
|
</article>
|
||||||
|
}
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
package config
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"extrovert/api"
|
|
||||||
"extrovert/internals"
|
|
||||||
"extrovert/pages"
|
|
||||||
)
|
|
||||||
|
|
||||||
var ROUTES = []internals.Page{
|
|
||||||
{Path: "index.html", Component: pages.Index()},
|
|
||||||
}
|
|
||||||
|
|
||||||
func APIROUTES(mux *http.ServeMux) {
|
|
||||||
mux.HandleFunc("/robots.txt", api.RobotsTxt)
|
|
||||||
mux.HandleFunc("/ai.txt", api.AiTxt)
|
|
||||||
}
|
|
||||||
4
internals/config.go
Normal file
4
internals/config.go
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
package internals
|
||||||
|
|
||||||
|
const APP_VERSION = "1"
|
||||||
|
const APP_NAME = "project-extrovert"
|
||||||
@@ -15,9 +15,13 @@ templ Page(title string) {
|
|||||||
rel="stylesheet"
|
rel="stylesheet"
|
||||||
href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css"
|
href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css"
|
||||||
/>
|
/>
|
||||||
|
<link
|
||||||
|
rel="stylesheet"
|
||||||
|
href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.colors.min.css"
|
||||||
|
/>
|
||||||
<title>{ title }</title>
|
<title>{ title }</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body style="display:flex;justify-content:center;align-items:center;width:100vw;height:100vh;">
|
||||||
{ children... }
|
{ children... }
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -1,69 +1,53 @@
|
|||||||
package pages
|
package pages
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"extrovert/layouts"
|
"extrovert/layouts"
|
||||||
|
"extrovert/components"
|
||||||
|
"extrovert/internals"
|
||||||
)
|
)
|
||||||
|
|
||||||
templ Index() {
|
func IndexHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
@layouts.Page("013") {
|
_ = internals.GetCookie("twitter-data", w, r)
|
||||||
<style>
|
|
||||||
@media(min-width: 640px) {
|
IndexPage().Render(context.TODO(), w)
|
||||||
article {
|
}
|
||||||
max-width: 50%;
|
|
||||||
}
|
templ IndexPage() {
|
||||||
}
|
@layouts.Page("Project Extrovert") {
|
||||||
aritcle {
|
<div style="max-width:50rem;">
|
||||||
max-width: 100%;
|
<div style="display:flex;flex-direction:column;gap:1rem;">
|
||||||
}
|
<main
|
||||||
</style>
|
style="height:15rem"
|
||||||
<main class="container-fluid" style="display:flex;justify-content:center;align-content:center;">
|
|
||||||
<article>
|
|
||||||
<header>
|
|
||||||
<nav>
|
|
||||||
<ul>
|
|
||||||
<li>Project Extrovert</li>
|
|
||||||
</ul>
|
|
||||||
<ul>
|
|
||||||
<li>
|
|
||||||
<a
|
|
||||||
class="secondary"
|
|
||||||
target="_blank"
|
|
||||||
href="https://github.com/capytalcode/project-extrovert"
|
|
||||||
rel="noreferer nofollow"
|
|
||||||
>GitHub</a>
|
|
||||||
</li>
|
|
||||||
<li>
|
|
||||||
<a
|
|
||||||
class="secondary"
|
|
||||||
target="_blank"
|
|
||||||
href="https://codeberg.org/capytalcode/project-extrovert"
|
|
||||||
rel="noreferer nofollow"
|
|
||||||
>Codeberg</a>
|
|
||||||
</li>
|
|
||||||
</ul>
|
|
||||||
</nav>
|
|
||||||
</header>
|
|
||||||
This is a project by
|
|
||||||
<a
|
|
||||||
target="_blank"
|
|
||||||
href="https://github.com/capytalcode"
|
|
||||||
rel="noreferer nofollow"
|
|
||||||
>
|
>
|
||||||
Capytal Code
|
<form
|
||||||
</a>,
|
style="height:100%;display:flex;gap:2rem;"
|
||||||
keep visting this link to see the project's evoluition and development.
|
|
||||||
<footer>
|
|
||||||
© 2024
|
|
||||||
<a
|
|
||||||
href="https://guz.one"
|
|
||||||
target="_blank"
|
|
||||||
rel="noreferer nofollow"
|
|
||||||
class="secondary"
|
|
||||||
>
|
>
|
||||||
Gustavo L. de Mello
|
<fieldset style="display:flex;flex-direction:column;gap:1rem;width:15rem;">
|
||||||
</a>
|
@components.LoginTwitter()
|
||||||
</footer>
|
<button>Login on Mastodon</button>
|
||||||
</article>
|
</fieldset>
|
||||||
|
<fieldset>
|
||||||
|
<textarea
|
||||||
|
style="height:100%;resize:none;"
|
||||||
|
name="post"
|
||||||
|
placeholder="Write a post..."
|
||||||
|
aria-label="Post input"
|
||||||
|
></textarea>
|
||||||
|
</fieldset>
|
||||||
|
</form>
|
||||||
</main>
|
</main>
|
||||||
|
<footer>
|
||||||
|
@components.Warning("In Development") {
|
||||||
|
<strong>
|
||||||
|
This application is in active development and should not be used in production.
|
||||||
|
</strong>
|
||||||
|
Expect bugs, lost data, logouts, etc. Use it at your own risk.
|
||||||
|
This software is provided "as is", without any warranty of any kind.
|
||||||
|
}
|
||||||
|
</footer>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
30
pages/router.go
Normal file
30
pages/router.go
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package pages
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/a-h/templ"
|
||||||
|
)
|
||||||
|
|
||||||
|
var ROUTES = []Route{
|
||||||
|
{
|
||||||
|
Pattern: "/index.html",
|
||||||
|
Static: true,
|
||||||
|
Page: IndexPage(),
|
||||||
|
Handler: IndexHandler,
|
||||||
|
},
|
||||||
|
|
||||||
|
type RouteHandler = func(http.ResponseWriter, *http.Request)
|
||||||
|
|
||||||
|
type Route struct {
|
||||||
|
Pattern string
|
||||||
|
Static bool
|
||||||
|
Handler RouteHandler
|
||||||
|
Page templ.Component
|
||||||
|
}
|
||||||
|
|
||||||
|
func RegisterAllRoutes(routes []Route, s *http.ServeMux) {
|
||||||
|
for _, r := range routes {
|
||||||
|
s.HandleFunc(r.Pattern, r.Handler)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user