// author: phga // backend for the portfolio package main import ( "encoding/json" "fmt" "html/template" "io/ioutil" "log" "net/http" "path" "regexp" ) // could be enabled after the implementation is complete. // var t = template.Must(getTemplate("layouts/base.html", "index.html", "blog.html", "about.html", "contact.html", )) // A regex used to identify valid Img requests var validImg = regexp.MustCompile("^/info/([\\w_\\- ]+)") var exhibit Exhibition func main() { // initialize the map (otherwise runtime error) exhibit.Pics = make(map[string]Picture) // cheap routing http.HandleFunc("/", handleIndex) http.HandleFunc("/blog", handleBlog) http.HandleFunc("/about", handleAbout) http.HandleFunc("/contact", handleContact) http.HandleFunc("/info/", handleInfo) // provide the inc directory to the useragent http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("../../web/static")))) // listen on port 8080 (I use nginx to proxy this local server) log.Fatalln(http.ListenAndServe(":8080", nil)) } // A picture type Picture struct { Name string Author string Size string Tools string Date string Desc string BlogLink string } type Exhibition struct { Pics map[string]Picture } // Funcions follow that handle the requests func handleIndex(w http.ResponseWriter, r *http.Request) { // that should also be somewhere else when there is another gallery exhibit.parseExhibitions("../../web/data") // Parses all required html files to provide the actual html that is shipped. t, _ := getTemplate("layouts/index.html", "gallery.html", "header.html") t.Execute(w, exhibit) } func handleBlog(w http.ResponseWriter, r *http.Request) { t, _ := getTemplate("layouts/base.html", "soon.html", "header.html") t.Execute(w, nil) } func handleAbout(w http.ResponseWriter, r *http.Request) { t, _ := getTemplate("layouts/base.html", "about.html", "header.html") t.Execute(w, nil) } func handleContact(w http.ResponseWriter, r *http.Request) { t, _ := getTemplate("layouts/base.html", "contact.html", "header.html") t.Execute(w, nil) } // get the img from the exhibiton and write the json encoded // object to the responsewriter func handleInfo(w http.ResponseWriter, r *http.Request) { imgName := validImg.FindStringSubmatch(r.URL.Path) if imgName == nil { http.NotFound(w, r) return } img, ok := exhibit.Pics[imgName[1]] if !ok { http.NotFound(w, r) return } info, err := json.Marshal(img) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } // set the correct content typ (json) w.Header().Set("Content-Type", "application/json") w.Write(info) } // file, _ := json.MarshalIndent(ex.Pics, "", " ") // err := ioutil.WriteFile(dir+"/dg1.json", file, 0644) // if err != nil { // fmt.Println("WRITE", err) // } // load the exhibition data into the struct func (ex *Exhibition) parseExhibitions(dir string) { files, err := ioutil.ReadDir(dir) if err != nil { fmt.Println("E1", err) return } for _, f := range files { if !f.IsDir() { var pics []Picture content, err := ioutil.ReadFile(dir + "/" + f.Name()) if err != nil { fmt.Println("E2", err) } err = json.Unmarshal(content, &pics) if err != nil { fmt.Println("E3", err) } for _, pic := range pics { ex.Pics[pic.Name] = pic } } } } func getTemplate(files ...string) (*template.Template, error) { tmplFolder := "../../web/templates/" for i, f := range files { files[i] = tmplFolder + f } // Name has to be the basename of at least one of the template files t := template.New(path.Base(files[0])) // Add custom helper funcs to template // t.Funcs(template.FuncMap{ // "UnwrapOID": unwrapObjectID, // "GetSurveys": getSurveys, // }) t, err := t.ParseFiles(files...) return t, err }