Okay nevermind
This commit is contained in:
		
							parent
							
								
									16e16a033b
								
							
						
					
					
						commit
						965fb51598
					
				@ -1,41 +0,0 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"os"
 | 
			
		||||
 | 
			
		||||
	"git.home.dutchellie.nl/DutchEllie/proper-website-2/internal/pages"
 | 
			
		||||
	"git.home.dutchellie.nl/DutchEllie/proper-website-2/internal/templatelib"
 | 
			
		||||
	"github.com/gin-gonic/gin"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	port := os.Getenv("SRV_PORT")
 | 
			
		||||
	if port == "" {
 | 
			
		||||
		msg := `Website writen in Go!
 | 
			
		||||
 | 
			
		||||
Please include all the proper environment variables:
 | 
			
		||||
- "SRV_PORT" <number of the exposing port>
 | 
			
		||||
		`
 | 
			
		||||
		fmt.Println(msg)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	address := ":" + port
 | 
			
		||||
 | 
			
		||||
	lib, err := templatelib.NewTemplateLibrary("../../website/templates")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		fmt.Println(err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	router, _ := setupRouter(lib)
 | 
			
		||||
 | 
			
		||||
	router.Run(address)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func setupRouter(lib templatelib.TemplateLibrary) (*gin.Engine, error) {
 | 
			
		||||
	router := gin.Default()
 | 
			
		||||
 | 
			
		||||
	pages.RegisterHandlers(router.Group("/"), pages.NewService(pages.NewRepository(&lib)))
 | 
			
		||||
	return router, nil
 | 
			
		||||
}
 | 
			
		||||
@ -1,24 +0,0 @@
 | 
			
		||||
package pages
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"html/template"
 | 
			
		||||
 | 
			
		||||
	"git.home.dutchellie.nl/DutchEllie/proper-website-2/internal/templatelib"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type Repository interface {
 | 
			
		||||
	Page(ctx context.Context, name string) (*template.Template, error)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewRepository(lib *templatelib.TemplateLibrary) Repository {
 | 
			
		||||
	return repository{lib}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type repository struct {
 | 
			
		||||
	library *templatelib.TemplateLibrary
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (r repository) Page(ctx context.Context, name string) (*template.Template, error) {
 | 
			
		||||
	return r.library.Templates[name], nil
 | 
			
		||||
}
 | 
			
		||||
@ -1,26 +0,0 @@
 | 
			
		||||
package pages
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"net/http"
 | 
			
		||||
 | 
			
		||||
	"github.com/gin-gonic/gin"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func RegisterHandlers(r *gin.RouterGroup, service Service) {
 | 
			
		||||
	res := resource{service}
 | 
			
		||||
 | 
			
		||||
	r.GET("/index", res.getindex)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type resource struct {
 | 
			
		||||
	service Service
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (r resource) getindex(c *gin.Context) {
 | 
			
		||||
	page, err := r.service.Page(c.Request.Context(), "index")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		c.AbortWithStatus(http.StatusInternalServerError)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	page.template.Execute(c.Writer, nil)
 | 
			
		||||
}
 | 
			
		||||
@ -1,31 +0,0 @@
 | 
			
		||||
package pages
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"html/template"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type Service interface {
 | 
			
		||||
	Page(ctx context.Context, name string) (Page, error)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Page struct {
 | 
			
		||||
	template *template.Template
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type service struct {
 | 
			
		||||
	repository Repository
 | 
			
		||||
	//logger     log.Logger
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewService(r Repository) Service {
 | 
			
		||||
	return service{r}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (s service) Page(ctx context.Context, name string) (Page, error) {
 | 
			
		||||
	page, err := s.repository.Page(ctx, name)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return Page{}, err
 | 
			
		||||
	}
 | 
			
		||||
	return Page{page}, nil
 | 
			
		||||
}
 | 
			
		||||
@ -1,46 +0,0 @@
 | 
			
		||||
package templatelib
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"html/template"
 | 
			
		||||
	"path/filepath"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type TemplateLibrary struct {
 | 
			
		||||
	Templates map[string]*template.Template
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewTemplateLibrary(dir string) (TemplateLibrary, error) {
 | 
			
		||||
	t := make(map[string]*template.Template)
 | 
			
		||||
 | 
			
		||||
	pages, err := filepath.Glob(filepath.Join(dir, "*.page.tmpl"))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return TemplateLibrary{}, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for _, page := range pages {
 | 
			
		||||
		name := filepath.Base(page)
 | 
			
		||||
		ts, err := template.ParseFiles(page)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return TemplateLibrary{}, err
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// Use the ParseGlob method to add any 'layout' templates to the
 | 
			
		||||
		// template set (in our case, it's just the 'base' layout at the
 | 
			
		||||
		// moment).
 | 
			
		||||
		ts, err = ts.ParseGlob(filepath.Join(dir, "*.layout.tmpl"))
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return TemplateLibrary{}, err
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// Use the ParseGlob method to add any 'partial' templates to the
 | 
			
		||||
		// template set (in our case, it's just the 'footer' partial at the
 | 
			
		||||
		// moment).
 | 
			
		||||
		ts, err = ts.ParseGlob(filepath.Join(dir, "*.partial.tmpl"))
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return TemplateLibrary{}, err
 | 
			
		||||
		}
 | 
			
		||||
		t[name] = ts
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return TemplateLibrary{Templates: t}, nil
 | 
			
		||||
}
 | 
			
		||||
@ -5,7 +5,7 @@
 | 
			
		||||
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
 | 
			
		||||
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
 | 
			
		||||
	<link rel="stylesheet" href="static/style.css">
 | 
			
		||||
	<link rel="stylesheet" href="anisha.css?family=anisha">
 | 
			
		||||
	<link rel="stylesheet" href="static/anisha.css?family=anisha">
 | 
			
		||||
	<title>Index</title>
 | 
			
		||||
</head>
 | 
			
		||||
<body>
 | 
			
		||||
@ -14,10 +14,13 @@
 | 
			
		||||
	</div>
 | 
			
		||||
	<div class="main">
 | 
			
		||||
		<div class="navbar">
 | 
			
		||||
 | 
			
		||||
			<ul>
 | 
			
		||||
				<li><a href="base.html">Home</a></li>
 | 
			
		||||
			</ul>
 | 
			
		||||
		</div>
 | 
			
		||||
		<div class="content">
 | 
			
		||||
 | 
			
		||||
			Dit is eigenlijk 1 van die dingen die steeds kan veranderen
 | 
			
		||||
			Deze doet dat
 | 
			
		||||
		</div>
 | 
			
		||||
	</div>
 | 
			
		||||
</body>
 | 
			
		||||
 | 
			
		||||
@ -17,4 +17,37 @@ body {
 | 
			
		||||
	background-color: rgb(54, 39, 48);
 | 
			
		||||
	font-size: 5em;
 | 
			
		||||
	font-family: anisha;
 | 
			
		||||
	text-align: center;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
.main {
 | 
			
		||||
	margin-top: 5px;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
.navbar {
 | 
			
		||||
	border: 3px solid;
 | 
			
		||||
	border-radius: 4px;
 | 
			
		||||
	border-color: rgb(252, 230, 255);
 | 
			
		||||
	background-color: rgb(54, 39, 48);
 | 
			
		||||
	position: relative;
 | 
			
		||||
	float:left;
 | 
			
		||||
	width: 250px;
 | 
			
		||||
	text-decoration: none;
 | 
			
		||||
	list-style: none;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
.navbar a, a:link, a:visited{
 | 
			
		||||
	text-decoration: none;
 | 
			
		||||
	color:rgb(252, 230, 255)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
.content {
 | 
			
		||||
	border: 3px solid;
 | 
			
		||||
	border-radius: 4px;
 | 
			
		||||
	border-color: rgb(252, 230, 255);
 | 
			
		||||
	background-color: rgb(54, 39, 48);
 | 
			
		||||
	position: relative;
 | 
			
		||||
	float:right;
 | 
			
		||||
	width: 633px;
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@ -1,16 +0,0 @@
 | 
			
		||||
{{ define "base" }}
 | 
			
		||||
<!doctype html>
 | 
			
		||||
<html lang='en'>
 | 
			
		||||
	<head>
 | 
			
		||||
		<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
 | 
			
		||||
		<title>{{template "title" .}}</title>
 | 
			
		||||
	</head>
 | 
			
		||||
	<body>
 | 
			
		||||
		<header>
 | 
			
		||||
			<h1></h1>
 | 
			
		||||
		</header>
 | 
			
		||||
	</body>
 | 
			
		||||
 | 
			
		||||
</html>
 | 
			
		||||
 | 
			
		||||
{{ end }}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user