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"
|
||||
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>
|
||||
</head>
|
||||
<body>
|
||||
<body style="display:flex;justify-content:center;align-items:center;width:100vw;height:100vh;">
|
||||
{ children... }
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1,69 +1,53 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"extrovert/layouts"
|
||||
"extrovert/components"
|
||||
"extrovert/internals"
|
||||
)
|
||||
|
||||
templ Index() {
|
||||
@layouts.Page("013") {
|
||||
<style>
|
||||
@media(min-width: 640px) {
|
||||
article {
|
||||
max-width: 50%;
|
||||
}
|
||||
}
|
||||
aritcle {
|
||||
max-width: 100%;
|
||||
}
|
||||
</style>
|
||||
<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"
|
||||
func IndexHandler(w http.ResponseWriter, r *http.Request) {
|
||||
_ = internals.GetCookie("twitter-data", w, r)
|
||||
|
||||
IndexPage().Render(context.TODO(), w)
|
||||
}
|
||||
|
||||
templ IndexPage() {
|
||||
@layouts.Page("Project Extrovert") {
|
||||
<div style="max-width:50rem;">
|
||||
<div style="display:flex;flex-direction:column;gap:1rem;">
|
||||
<main
|
||||
style="height:15rem"
|
||||
>
|
||||
Capytal Code
|
||||
</a>,
|
||||
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"
|
||||
<form
|
||||
style="height:100%;display:flex;gap:2rem;"
|
||||
>
|
||||
Gustavo L. de Mello
|
||||
</a>
|
||||
<fieldset style="display:flex;flex-direction:column;gap:1rem;width:15rem;">
|
||||
@components.LoginTwitter()
|
||||
<button>Login on Mastodon</button>
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
<textarea
|
||||
style="height:100%;resize:none;"
|
||||
name="post"
|
||||
placeholder="Write a post..."
|
||||
aria-label="Post input"
|
||||
></textarea>
|
||||
</fieldset>
|
||||
</form>
|
||||
</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>
|
||||
</article>
|
||||
</main>
|
||||
</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