Advertisement
pcwizz

Untitled

Aug 28th, 2014
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.89 KB | None | 0 0
  1. /*
  2. PCWIZZ LTD Website
  3. Copyright (c) 2014 Morgan Hill <morgan@pcwizzltd.com>
  4.  
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. package main
  19.  
  20. import (
  21.     "html/template"
  22.     "io/ioutil"
  23.     "net/http"
  24.     "os"
  25.     "fmt"
  26.     "encoding/json"
  27. )
  28.  
  29. type Page struct {
  30.     Title string
  31.     Body  template.HTML
  32. }
  33.  
  34. func loadPage(w http.ResponseWriter, title string) (*Page, error) {
  35.     filename := "pages/"+title + ".html"
  36.     s, err := ioutil.ReadFile(filename)
  37.     if err != nil {
  38.         http.Error(w, err.Error(), http.StatusInternalServerError)
  39.         return nil, err
  40.     }
  41.     body := template.HTML(s)
  42.     return &Page{Title: title, Body: body}, nil
  43. }
  44.  
  45. func serveStaticFile(w http.ResponseWriter,r *http.Request,path string){
  46.     file, err := os.Open(path)
  47.     if err != nil {
  48.         http.Error(w, err.Error(), http.StatusInternalServerError)
  49.         return
  50.     }
  51.     fileInfo, err := os.Stat(path)
  52.     if err != nil {
  53.         http.Error(w, err.Error(), http.StatusInternalServerError)
  54.         return
  55.     }
  56.     fileModTime := fileInfo.ModTime()
  57.     http.ServeContent(w,r,path, fileModTime, file)
  58.     file.Close()
  59. }
  60.  
  61. func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
  62.     t, err := template.ParseFiles("templates/" + tmpl + ".html")
  63.     if err != nil{
  64.         http.Error(w, err.Error(), http.StatusInternalServerError)
  65.         return
  66.     }
  67.     t.Execute(w, p)
  68. }
  69.  
  70. func homeHandler(w http.ResponseWriter, r *http.Request) {
  71.     p, _ := loadPage(w,"home")
  72.     renderTemplate(w, "template", p)
  73. }
  74.  
  75. type ContactForm struct{
  76.     contactName string
  77.     companyName string
  78.     contactEmail string
  79.     contactPhoneNumber string
  80.     reason string
  81.     target []string
  82.     details string
  83.  
  84. }
  85.  
  86. func contactHandler(w http.ResponseWriter, r *http.Request) {
  87.     if r.Method == "POST" {
  88.         fmt.Fprintf(w, "%v", r)
  89.         var form ContactForm
  90.         err := json.NewDecoder(r.Body).Decode(&form)
  91.         if err != nil {
  92.             http.Error(w, err.Error(), http.StatusInternalServerError)
  93.         }
  94.         fmt.Fprintf(w, "%s", form)
  95.     }else{
  96.         p, _ := loadPage(w,"contact")
  97.         renderTemplate(w, "template", p)
  98.     }
  99. }
  100. func staticHandler(w http.ResponseWriter, r *http.Request){
  101.     path := r.URL.Path[1:]
  102.     serveStaticFile(w,r,path)
  103. }
  104.  
  105. func main() {
  106.     http.HandleFunc("/", homeHandler)
  107.     http.HandleFunc("/contact", contactHandler)
  108.     http.HandleFunc("/css/", staticHandler)
  109.     http.HandleFunc("/img/", staticHandler)
  110.     http.HandleFunc("/js/", staticHandler)
  111.     http.ListenAndServe(":8080", nil)
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement