thestrukture/IDE

View on GitHub
api/templates/logger.go

Summary

Maintainability
D
2 days
Test Coverage
F
0%
// File generated by Gopher Sauce
// DO NOT EDIT!!
package templates

import (
    "bytes"
    "fmt"
    "html/template"
    "log"
    "net/http"
    "strings"

    "github.com/thestrukture/IDE/api/assets"
    sessionStore "github.com/thestrukture/IDE/api/sessions"
)

var Prod = true

// Scans template string and uses data provided from request
// to find the line causing the panic.
func DebugTemplate(w http.ResponseWriter, r *http.Request, tmpl string) {
    lastline := 0
    linestring := ""
    defer func() {
        if n := recover(); n != nil {
            log.Println()
            // log.Println(n)
            log.Println("Error on line :", lastline+1, ":"+strings.TrimSpace(linestring))
            //http.Redirect(w,r,"",307)
        }
    }()

    p, err := LoadPage(r.URL.Path)
    filename := tmpl + ".tmpl"
    body, err := assets.Asset(filename)
    session, er := sessionStore.Store.Get(r, "session-")

    if er != nil {
        session, er = sessionStore.Store.New(r, "session-")
    }
    p.Session = session
    p.R = r
    if err != nil {
        log.Print(err)

    } else {

        lines := strings.Split(string(body), "\n")
        // log.Println( lines )
        linebuffer := ""
        waitend := false
        open := 0
        for i, line := range lines {

            processd := false

            if strings.Contains(line, "{{with") || strings.Contains(line, "{{ with") || strings.Contains(line, "with}}") || strings.Contains(line, "with }}") || strings.Contains(line, "{{range") || strings.Contains(line, "{{ range") || strings.Contains(line, "range }}") || strings.Contains(line, "range}}") || strings.Contains(line, "{{if") || strings.Contains(line, "{{ if") || strings.Contains(line, "if }}") || strings.Contains(line, "if}}") || strings.Contains(line, "{{block") || strings.Contains(line, "{{ block") || strings.Contains(line, "block }}") || strings.Contains(line, "block}}") {
                linebuffer += line
                waitend = true

                endstr := ""
                processd = true
                if !(strings.Contains(line, "{{end") || strings.Contains(line, "{{ end") || strings.Contains(line, "end}}") || strings.Contains(line, "end }}")) {

                    open++

                }
                for i := 0; i < open; i++ {
                    endstr += "\n{{end}}"
                }
                //exec
                outp := new(bytes.Buffer)
                t := template.New("PageWrapper")
                t = t.Funcs(TemplateFuncStore)
                t, _ = t.Parse(string(body))
                lastline = i
                linestring = line
                erro := t.Execute(outp, p)
                if erro != nil {
                    log.Println("Error on line :", i+1, line, erro.Error())
                }
            }

            if waitend && !processd && !(strings.Contains(line, "{{end") || strings.Contains(line, "{{ end")) {
                linebuffer += line

                endstr := ""
                for i := 0; i < open; i++ {
                    endstr += "\n{{end}}"
                }
                //exec
                outp := new(bytes.Buffer)
                t := template.New("PageWrapper")
                t = t.Funcs(TemplateFuncStore)
                t, _ = t.Parse(string(body))
                lastline = i
                linestring = line
                erro := t.Execute(outp, p)
                if erro != nil {
                    log.Println("Error on line :", i+1, line, erro.Error())
                }

            }

            if !waitend && !processd {
                outp := new(bytes.Buffer)
                t := template.New("PageWrapper")
                t = t.Funcs(TemplateFuncStore)
                t, _ = t.Parse(string(body))
                lastline = i
                linestring = line
                erro := t.Execute(outp, p)
                if erro != nil {
                    log.Println("Error on line :", i+1, line, erro.Error())
                }
            }

            if !processd && (strings.Contains(line, "{{end") || strings.Contains(line, "{{ end")) {
                open--

                if open == 0 {
                    waitend = false

                }
            }
        }

    }

}

// Gets template string from assets package, and
// uses it with a provided pointer to a struct variable
// to find the line causing panic.
func DebugTemplatePath(tmpl string, intrf interface{}) {
    lastline := 0
    linestring := ""
    defer func() {
        if n := recover(); n != nil {

            log.Println("Error on line :", lastline+1, ":"+strings.TrimSpace(linestring))
            log.Println(n)
            //http.Redirect(w,r,"",307)
        }
    }()

    filename := tmpl
    body, err := assets.Asset(filename)

    if err != nil {
        log.Print(err)

    } else {

        lines := strings.Split(string(body), "\n")
        // log.Println( lines )
        linebuffer := ""
        waitend := false
        open := 0
        for i, line := range lines {

            processd := false

            if strings.Contains(line, "{{with") || strings.Contains(line, "{{ with") || strings.Contains(line, "with}}") || strings.Contains(line, "with }}") || strings.Contains(line, "{{range") || strings.Contains(line, "{{ range") || strings.Contains(line, "range }}") || strings.Contains(line, "range}}") || strings.Contains(line, "{{if") || strings.Contains(line, "{{ if") || strings.Contains(line, "if }}") || strings.Contains(line, "if}}") || strings.Contains(line, "{{block") || strings.Contains(line, "{{ block") || strings.Contains(line, "block }}") || strings.Contains(line, "block}}") {
                linebuffer += line
                waitend = true

                endstr := ""
                if !(strings.Contains(line, "{{end") || strings.Contains(line, "{{ end") || strings.Contains(line, "end}}") || strings.Contains(line, "end }}")) {

                    open++

                }

                for i := 0; i < open; i++ {
                    endstr += "\n{{end}}"
                }
                //exec

                processd = true
                outp := new(bytes.Buffer)
                t := template.New("PageWrapper")
                t = t.Funcs(TemplateFuncStore)
                t, _ = t.Parse(string([]byte(fmt.Sprintf("%s%s", linebuffer, endstr))))
                lastline = i
                linestring = line
                erro := t.Execute(outp, intrf)
                if erro != nil {
                    log.Println("Error on line :", i+1, line, erro.Error())
                }
            }

            if waitend && !processd && !(strings.Contains(line, "{{end") || strings.Contains(line, "{{ end") || strings.Contains(line, "end}}") || strings.Contains(line, "end }}")) {
                linebuffer += line

                endstr := ""
                for i := 0; i < open; i++ {
                    endstr += "\n{{end}}"
                }
                //exec
                outp := new(bytes.Buffer)
                t := template.New("PageWrapper")
                t = t.Funcs(TemplateFuncStore)
                t, _ = t.Parse(string([]byte(fmt.Sprintf("%s%s", linebuffer, endstr))))
                lastline = i
                linestring = line
                erro := t.Execute(outp, intrf)
                if erro != nil {
                    log.Println("Error on line :", i+1, line, erro.Error())
                }

            }

            if !waitend && !processd {
                outp := new(bytes.Buffer)
                t := template.New("PageWrapper")
                t = t.Funcs(TemplateFuncStore)
                t, _ = t.Parse(string([]byte(fmt.Sprintf("%s", linebuffer))))
                lastline = i
                linestring = line
                erro := t.Execute(outp, intrf)
                if erro != nil {
                    log.Println("Error on line :", i+1, line, erro.Error())
                }
            }

            if !processd && (strings.Contains(line, "{{end") || strings.Contains(line, "{{ end") || strings.Contains(line, "end}}") || strings.Contains(line, "end }}")) {
                open--

                if open == 0 {
                    waitend = false

                }
            }
        }

    }

}