thestrukture/IDE

View on GitHub
api/handlers/html.go

Summary

Maintainability
A
1 hr
Test Coverage
F
4%
// File generated by Gopher Sauce
// DO NOT EDIT!!
package handlers

import (
    "log"
    "net/http"
    "strings"

    gosweb "github.com/cheikhshift/gos/web"
    "github.com/gorilla/sessions"
    templates "github.com/thestrukture/IDE/api/templates"

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

// Access you .gxml's end tags with
// this http.HandlerFunc.
// Use MakeHandler(http.HandlerFunc) to serve your web
// directory from memory.
func MakeHandler(fn func(http.ResponseWriter, *http.Request)) http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {

        if attmpt := ApiAttempt(w, r); !attmpt {
            fn(w, r)
        }

    }
}

func Handler(w http.ResponseWriter, r *http.Request) {
    var p *gosweb.Page
    p, err := templates.LoadPage(r.URL.Path)
    var session *sessions.Session
    var er error
    if session, er = sessionStore.Store.Get(r, "session-"); er != nil {
        session, _ = sessionStore.Store.New(r, "session-")
    }

    if err != nil {
        log.Println(err.Error())

        w.WriteHeader(http.StatusNotFound)

        pag, err := templates.LoadPage("")

        if err != nil {
            log.Println(err.Error())
            //
            return
        }
        pag.R = r
        pag.Session = session
        if p != nil {
            p.Session = nil
            p.Body = nil
            p.R = nil
            p = nil
        }

        if pag.IsResource {
            w.Write(pag.Body)
        } else {
            templates.RenderTemplate(w, pag) //""
        }
        session = nil

        return
    }

    if !p.IsResource {
        w.Header().Set("Content-Type", "text/html")
        p.Session = session
        p.R = r
        templates.RenderTemplate(w, p) //fmt.Sprintf("web%s", r.URL.Path)
        session.Save(r, w)
        // log.Println(w)
    } else {
        w.Header().Set("Cache-Control", "public")
        if strings.Contains(r.URL.Path, ".css") {
            w.Header().Add("Content-Type", "text/css")
        } else if strings.Contains(r.URL.Path, ".js") {
            w.Header().Add("Content-Type", "application/javascript")
        } else {
            w.Header().Add("Content-Type", http.DetectContentType(p.Body))
        }

        w.Write(p.Body)
    }

    p.Session = nil
    p.Body = nil
    p.R = nil
    p = nil
    session = nil

    return
}