add routes
This commit is contained in:
parent
ce6d6f8fb3
commit
f570a799e6
2 changed files with 69 additions and 0 deletions
50
cmd/main.go
Normal file
50
cmd/main.go
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
|
||||
"github.com/juancwu/potoforio/pkg/pages"
|
||||
)
|
||||
|
||||
type TemplateRenderer struct {
|
||||
templates *template.Template
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Load connection string from .env file
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
log.Fatal("failed to load env", err)
|
||||
}
|
||||
|
||||
templates, err := template.New("").ParseGlob("public/views/*.html")
|
||||
if err != nil {
|
||||
log.Fatalf("Error initializing templates: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
e := echo.New()
|
||||
e.Renderer = &TemplateRenderer{
|
||||
templates: templates,
|
||||
}
|
||||
|
||||
e.Use(middleware.Logger())
|
||||
e.Static("/static", "static")
|
||||
|
||||
e.GET("/", pages.Index)
|
||||
e.GET("/projects", pages.Projects)
|
||||
e.GET("/about-me", pages.AboutMe)
|
||||
|
||||
e.Logger.Fatal(e.Start(":5173"))
|
||||
}
|
||||
|
||||
func (t *TemplateRenderer) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
|
||||
return t.templates.ExecuteTemplate(w, name, data)
|
||||
}
|
||||
19
pkg/pages/index.go
Normal file
19
pkg/pages/index.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package pages
|
||||
|
||||
import (
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
type Page struct {}
|
||||
|
||||
func Index(c echo.Context) error {
|
||||
return c.Render(200, "index.html", Page{})
|
||||
}
|
||||
|
||||
func Projects(c echo.Context) error {
|
||||
return c.Render(200, "projects.html", Page{})
|
||||
}
|
||||
|
||||
func AboutMe(c echo.Context) error {
|
||||
return c.Render(200, "about-me.html", Page{})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue