Initial hello world
This commit is contained in:
44
application/index.go
Normal file
44
application/index.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func index(w http.ResponseWriter, r *http.Request) {
|
||||
lp := filepath.Join("application", "layouts", "main.html")
|
||||
fp := filepath.Join("application", "views", "index.html")
|
||||
|
||||
// Return a 404 if the template doesn't exist
|
||||
info, err := os.Stat(fp)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Return a 404 if the request is for a directory
|
||||
if info.IsDir() {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
tmpl, err := template.ParseFiles(lp, fp)
|
||||
if err != nil {
|
||||
// Log the detailed error
|
||||
log.Print(err.Error())
|
||||
// Return a generic "Internal Server Error" message
|
||||
http.Error(w, http.StatusText(500), 500)
|
||||
return
|
||||
}
|
||||
|
||||
err = tmpl.ExecuteTemplate(w, "main.html", nil)
|
||||
if err != nil {
|
||||
log.Print(err.Error())
|
||||
http.Error(w, http.StatusText(500), 500)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user