Notizen zum Macaron Webframework

Ein kleiner - quick and dirty blog (main.go weiter unten)

Example Output after Start:

go run main.go                                                                               
[Macaron] listening on 0.0.0.0:4000 (development)                                                                          
[Macaron] 2016-08-31 21:07:08: Started GET / for 127.0.0.1                                                                 
[Macaron] 2016-08-31 21:07:08: Completed / 200 OK in 1.089785ms                                                            
[Macaron] 2016-08-31 21:07:08: Started GET /css/styles.css for 127.0.0.1                                                   
[Macaron] [Static] Serving /css/styles.css                                                                                 
[Macaron] 2016-08-31 21:07:08: Completed /css/styles.css 304 Not Modified in 162.151µs 
go get gopkg.in/macaron.v1

Basic

// the main site (startsite)
func IndexHandler(ctx *macaron.Context) {
    ctx.Data["Posts"] = allposts
    ctx.HTML(200, "index")
}
templates/
├── 404.html
├── footer.html
├── header.html
├── index.html
└── post.html

Include

{{template "header" .}}
 
<div class="container">
 
    <h3>{{.Post.Title}}</h3>
 
    <p>{{.Post.Content}}</p>
 
</div>
{{template "footer" .}}

Static Files

m.Use(macaron.Static("assets"))
assets/
└── css
    └── styles.css

Conditions

Range

Struktur:

type Post struct {
    ID      int
    Title   string
    Content string
}
{{range .Posts}}
 
<div class="container">
 
  <h3>{{.Title}}</h3>
 
  <p>{{.Content}}</p>
  <p><a href="/post/{{.ID}}">Read More..</a></p>
 
</div>
 
{{end}}

Routes for the Blog Example

m.Get("/", IndexHandler)
m.Get("/post/:id", PostHandler)

Example Handler(s):

func IndexHandler(ctx *macaron.Context) {
    ctx.Data["Posts"] = allposts
    ctx.HTML(200, "index")
}
 
func PostHandler(ctx *macaron.Context) {
    id, _ := strconv.Atoi(ctx.Params(":id"))
    post, ok := allposts[id]
    if !ok {
	ctx.HTML(404, "404")
	return
    }
    ctx.Data["Post"] = post
    ctx.HTML(200, "post")
}

Custom 404

m.NotFound(func(ctx *macaron.Context) {
    ctx.HTML(404, "404")
})
package main
 
import (
    "strconv"
    "gopkg.in/macaron.v1"
)
 
type Post struct {
    ID      int
    Title   string
    Content string
}
 
type Settings struct {
    Name string
}
 
// the main site (startsite)
func IndexHandler(ctx *macaron.Context) {
    ctx.Data["Posts"] = allposts
    ctx.HTML(200, "index")
}
 
// single posting
func PostHandler(ctx *macaron.Context) {
    id, _ := strconv.Atoi(ctx.Params(":id"))
    post, ok := allposts[id]
    if !ok {
        ctx.HTML(404, "404")
	return
    }
    ctx.Data["Post"] = post
    ctx.HTML(200, "post")
}
 
var settings = Settings{"Example Blog"}
var allposts = make(map[int]Post)
 
func main() {
    // fake blog entrys (or from a database)
    allposts[1] = Post{1, "Erster Eintrag", "Read the fucking Manual"}
    allposts[2] = Post{2, "Zweiter Eintrag", "Write the fucking Templating Manual"}
    allposts[3] = Post{3, "Dritter Eintrag", "Write the fucking Templating Manual"}
 
    m := macaron.Classic()
 
    // middleware
 
    m.Use(macaron.Renderer())
    m.Use(macaron.Static("assets"))
 
    m.Use(func(ctx *macaron.Context) {
        ctx.Data["Name"] = settings.Name
    })
 
    // Handle 404 
    m.NotFound(func(ctx *macaron.Context) {
        ctx.HTML(404, "404")
    })
 
    // Routes
    m.Get("/", IndexHandler)
    m.Get("/post/:id", PostHandler)
 
    // Start Server
    m.Run()
}

Httperf

Ergebnis gegen den Fake Blog:

Total: connections 100 requests 100 replies 100 test-duration 9.902 s

Connection rate: 10.1 conn/s (99.0 ms/conn, <=1 concurrent connections)
Connection time [ms]: min 1.0 avg 1.3 max 3.5 median 1.5 stddev 0.3
Connection time [ms]: connect 0.0
Connection length [replies/conn]: 1.000

Request rate: 10.1 req/s (99.0 ms/req)
Request size [B]: 62.0

Reply rate [replies/s]: min 10.0 avg 10.0 max 10.0 stddev 0.0 (1 samples)
Reply time [ms]: response 1.3 transfer 0.0
Reply size [B]: header 117.0 content 844.0 footer 0.0 (total 961.0)
Reply status: 1xx=0 2xx=100 3xx=0 4xx=0 5xx=0

CPU time [s]: user 2.34 system 7.50 (user 23.6% system 75.7% total 99.4%)
Net I/O: 10.1 KB/s (0.1*10^6 bps)

Errors: total 0 client-timo 0 socket-timo 0 connrefused 0 connreset 0
Errors: fd-unavail 0 addrunavail 0 ftab-full 0 other 0
Cookies helfen bei der Bereitstellung von Inhalten. Durch die Nutzung dieser Seiten erklären Sie sich damit einverstanden, dass Cookies auf Ihrem Rechner gespeichert werden. Weitere Information
  • webdev/golang/macaron.txt
  • Zuletzt geändert: 2016/09/02 08:37
  • von haiko