Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- PCWIZZ LTD Website
- Copyright (c) 2014 Morgan Hill <morgan@pcwizzltd.com>
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- package main
- import (
- "html/template"
- "io/ioutil"
- "net/http"
- "os"
- "fmt"
- "encoding/json"
- )
- type Page struct {
- Title string
- Body template.HTML
- }
- func loadPage(w http.ResponseWriter, title string) (*Page, error) {
- filename := "pages/"+title + ".html"
- s, err := ioutil.ReadFile(filename)
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return nil, err
- }
- body := template.HTML(s)
- return &Page{Title: title, Body: body}, nil
- }
- func serveStaticFile(w http.ResponseWriter,r *http.Request,path string){
- file, err := os.Open(path)
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- fileInfo, err := os.Stat(path)
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- fileModTime := fileInfo.ModTime()
- http.ServeContent(w,r,path, fileModTime, file)
- file.Close()
- }
- func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
- t, err := template.ParseFiles("templates/" + tmpl + ".html")
- if err != nil{
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- t.Execute(w, p)
- }
- func homeHandler(w http.ResponseWriter, r *http.Request) {
- p, _ := loadPage(w,"home")
- renderTemplate(w, "template", p)
- }
- type ContactForm struct{
- contactName string
- companyName string
- contactEmail string
- contactPhoneNumber string
- reason string
- target []string
- details string
- }
- func contactHandler(w http.ResponseWriter, r *http.Request) {
- if r.Method == "POST" {
- fmt.Fprintf(w, "%v", r)
- var form ContactForm
- err := json.NewDecoder(r.Body).Decode(&form)
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- }
- fmt.Fprintf(w, "%s", form)
- }else{
- p, _ := loadPage(w,"contact")
- renderTemplate(w, "template", p)
- }
- }
- func staticHandler(w http.ResponseWriter, r *http.Request){
- path := r.URL.Path[1:]
- serveStaticFile(w,r,path)
- }
- func main() {
- http.HandleFunc("/", homeHandler)
- http.HandleFunc("/contact", contactHandler)
- http.HandleFunc("/css/", staticHandler)
- http.HandleFunc("/img/", staticHandler)
- http.HandleFunc("/js/", staticHandler)
- http.ListenAndServe(":8080", nil)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement