Skip to content

Instantly share code, notes, and snippets.

@zznop
Last active August 30, 2020 21:09
Show Gist options
  • Save zznop/ee85946c3e91e6a6aff89b4a458c4de9 to your computer and use it in GitHub Desktop.
Save zznop/ee85946c3e91e6a6aff89b4a458c4de9 to your computer and use it in GitHub Desktop.
Labstack Echo Blog Example
package main;
import(
"github.com/labstack/echo"
"time"
"path/filepath"
"html/template"
"strings"
"os"
"net/http"
"io"
"fmt"
)
type Template struct {
templates *template.Template
}
type blogPost struct {
Title string
Date string
FilePath string
}
// Slice containing info on blog posts
var posts []blogPost
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return t.templates.ExecuteTemplate(w, name, data)
}
func home(c echo.Context) error {
return c.Render(http.StatusOK, "index.html", &posts)
}
func getPostList() error {
root := "posts/"
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
name := info.Name()
if !strings.HasSuffix(name, ".html") {
return nil
}
// Trim extension
extension := filepath.Ext(name)
name = name[0:len(name)-len(extension)]
// Parse date from file name
elements := strings.Split(name, "-")
dateStr := strings.Join(elements[:3], "-")
dateVal, err := time.Parse("01-02-2006", dateStr)
if err != nil {
return err
}
// Parse title from file name
titleRaw := strings.Split(elements[3], "_")
titleVal := strings.Join(titleRaw, " ")
// Append post info to array
post := blogPost{
Title: titleVal,
Date: dateVal.Format("January 2, 2006"),
FilePath: path}
posts = append(posts, post)
return nil;
})
return err
}
func main() {
t := &Template{
templates: template.Must(template.ParseGlob("views/*.html")),
}
e := echo.New()
err := getPostList()
if err != nil {
e.Logger.Fatal(err)
}
fmt.Println(posts)
e.Renderer = t
e.Logger.Info("Web server started")
e.Static("/static", "assets")
e.Static("/posts", "posts")
e.GET("/", home)
e.Logger.Fatal(e.Start("0.0.0.0:80"))
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>zznop labs</title>
<meta name="description" content="">
<meta name="author" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="//fonts.googleapis.com/css?family=Raleway:400,300,600" rel="stylesheet" type="text/css">
<link href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="static/css/normalize.css">
<link rel="stylesheet" href="static/css/skeleton.css">
<link rel="stylesheet" href="static/css/custom.css">
<link rel="icon" type="image/png" href="static/images/favicon.png">
</head>
<body>
<!-- HEADER -->
<header>
<div class="container">
<div class="row">
<div class="twelve columns">
<h1>About</h1>
<h4>I am Brandon Miller, also known as zznop. I like to hack on SEGA Genesis ROMs and develop software
analysis tools. I am interested in reverse engineering, game development, and game console emulation.
Currently, I am working on a emulation framework to aid in reversing SEGA Genesis games.
<h4>
</div>
</div>
</div><!--end of .container-->
</header>
<!--CONTACT-->
<section class="contact">
<div class="container">
<div class="row">
<div class="twelve columns">
<div class="four columns offset-by-four">
<ul>
<li><a href="https://twiter.com/zznop_" title=""><span class="fa fa-twitter"></span></a></li>
<li><a href="https://github.com/zznop" title=""><span class="fa fa-github"></span></a></li>
</ul>
</div>
</div>
</div><!--end of .row-->
</div><!--end of .container-->
</section>
<!--BLOG POSTS -->
<section class="blog">
<div class="container">
<div class="row">
<div class="twelve columns">
<h3>Posts</h3>
</div>
</div><!--end of .row-->
<div class="row bottom">
<div class="twelve columns">
<ul>
{{range .}}
<li>
<a class="blog" href="{{.FilePath}}">{{.Title}}</a>
<h3>{{.Date}}</h3>
</li>
{{end}}
</ul>
</div>
</div><!--end of .row-->
</div><!--end of .container-->
</section>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment