/* * Edit the robots.txt file */ func editRobot(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) // Write the new file if csrf.ValidateToken(r, r.FormValue("CSRFToken")) { Robots := Robots{ Content: []byte(r.FormValue("robots")), } _, err := datastore.Put(c, datastore.NewKey(c, "Robots", "Robots.txt", 0, nil), &Robots) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } cache.DeleteCache(r, "getRobot") } // Merge the models type PassedData struct { CSRFToken string Robots []byte } passedData := PassedData{ CSRFToken: csrf.GetToken(r), Robots: getRobot(r), } passedTemplate := new(bytes.Buffer) template.Must(template.ParseFiles("statuscode.ch/robots.txt/templates/editRobot.html")).Execute(passedTemplate, passedData) render.Render(w, r, passedTemplate) }
func suggestion(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) var info string if r.FormValue("ContentString") != "" { if recaptcha.Validate(r, r.RemoteAddr, r.FormValue("recaptcha_challenge_field"), r.FormValue("recaptcha_response_field")) == true { var post PostSuggestion post.Username = r.FormValue("Username") post.Content = []byte(r.FormValue("ContentString")) post.Timestamps = time.Now().Local() datastore.Put(c, datastore.NewIncompleteKey(c, "PostSuggestion", nil), &post) info = "Böylesine Muazzam Bir Bilgi Önerdiğin İçin Çok Teşekkür Ediyorum." } else { info = "Captcha Kodu Yanlış! Lütfen Tekrar Dene!" } } type PassedData struct { Info string } passedData := PassedData{ Info: info, } passedTemplate := new(bytes.Buffer) template.Must(template.ParseFiles("templates/suggestion.html")).Execute(passedTemplate, passedData) render.Render(w, r, passedTemplate) }
// Manages the files in the blobstore func manageFiles(w http.ResponseWriter, r *http.Request) { // Get the files c := appengine.NewContext(r) q := datastore.NewQuery("__BlobInfo__").Order("-creation") var files []BlobInfo keys, err := q.GetAll(c, &files) if err != nil { //TODO: Errorhandling return } models := make([]BlobInfo, len(files)) for i := 0; i < len(files); i++ { models[i].Filename = files[i].Filename models[i].Key = keys[i].StringID() } // Merge the models type PassedData struct { Files []BlobInfo CSRFToken string } passedData := PassedData{ Files: models, CSRFToken: csrf.GetToken(r), } passedTemplate := new(bytes.Buffer) template.Must(template.ParseFiles("statuscode.ch/files/templates/manageFiles.html")).Execute(passedTemplate, passedData) render.Render(w, r, passedTemplate) }
func root(w http.ResponseWriter, r *http.Request) { var postSeq int trimPath := strings.Trim(r.URL.Path, "/g/") arg, err := strconv.Atoi(trimPath) if trimPath != "" && err == nil { postSeq = arg } else { counter := getCount(w, r) fmt.Println(counter) postSeq = randInt(1, int(counter)+1) } post := getPost(w, r, postSeq) if post.Sequence != 0 { type PassedData struct { Post Post } passedData := PassedData{ Post: post, } passedTemplate := new(bytes.Buffer) template.Must(template.ParseFiles("templates/index.html")).Execute(passedTemplate, passedData) render.Render(w, r, passedTemplate) } else { fmt.Fprintln(w, "Öyle bir bilgi yok bro.") } }
func root(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) current_user := user.Current(c) passedTemplate := new(bytes.Buffer) template.Must(template.ParseFiles("templates/index.html")).Execute(passedTemplate, current_user) render.Render(w, r, passedTemplate) }
// Uploads a file to the blobstore func uploadFile(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) uploadURL, _ := blobstore.UploadURL(c, "/admin/files/handleUpload", nil) passedTemplate := new(bytes.Buffer) template.Must(template.ParseFiles("statuscode.ch/files/templates/upload.html")).Execute(passedTemplate, uploadURL) render.Render(w, r, passedTemplate) }
// Show the specified page (or return an 404) func ShowPage(w http.ResponseWriter, r *http.Request, identifier string) { exists, page := getPage(identifier, r) if exists == false { // Page don't exists, throw an 404 errorHandler.NotFound404(w, r) return } render.Render(w, r, bytes.NewBuffer([]byte(page.ContentString))) }
// This shows an error to the user and log it func Error(w http.ResponseWriter, r *http.Request, err ...string) { // Log it c := appengine.NewContext(r) c.Errorf("%v", err) // Show it to the user passedTemplate := new(bytes.Buffer) template.Must(template.ParseFiles("templates/error.html")).Execute(passedTemplate, err) render.Render(w, r, passedTemplate, http.StatusInternalServerError) }
// Manages the pages func managePages(w http.ResponseWriter, r *http.Request) { // Merge the data type PassedData struct { CSRFToken string Pages []Pages } passedData := PassedData{ CSRFToken: csrf.GetToken(r), Pages: getAllPages(w, r), } passedTemplate := new(bytes.Buffer) template.Must(template.ParseFiles("statuscode.ch/pages/templates/managePages.html")).Execute(passedTemplate, passedData) render.Render(w, r, passedTemplate) }
func manageSuggestions(w http.ResponseWriter, r *http.Request) { type PassedData struct { CSRFToken string Suggestions []PostSuggestion } passedData := PassedData{ CSRFToken: csrf.GetToken(r), Suggestions: getAllSuggestions(w, r), } passedTemplate := new(bytes.Buffer) template.Must(template.ParseFiles("templates/admin/sidebar.html")).Execute(passedTemplate, nil) template.Must(template.ParseFiles("templates/admin/suggestion/index.html")).Execute(passedTemplate, passedData) render.Render(w, r, passedTemplate) }
func editPost(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) // post id'sini al trimPath := strings.Trim(r.URL.Path, "/admin/post/edit/") postSeq, _ := strconv.Atoi(trimPath) postSeq64 := int64(postSeq) var p []Post q := datastore.NewQuery("Post").Filter("Sequence=", postSeq64) keys, _ := q.GetAll(c, &p) if p != nil { post := p[0] if csrf.ValidateToken(r, r.FormValue("CSRFToken")) { if r.Method == "POST" { c := appengine.NewContext(r) post.Content = []byte(r.FormValue("ContentString")) datastore.Put(c, datastore.NewKey(c, "Post", "", keys[0].IntID(), nil), &post) cache.DeleteCache(r, "post-"+strconv.Itoa(postSeq)) } } post.ContentString = string(post.Content) type PassedData struct { CSRFToken string Post Post } passedData := PassedData{ CSRFToken: csrf.GetToken(r), Post: post, } passedTemplate := new(bytes.Buffer) template.Must(template.ParseFiles("templates/admin/sidebar.html")).Execute(passedTemplate, nil) template.Must(template.ParseFiles("templates/admin/post/edit.html")).Execute(passedTemplate, passedData) render.Render(w, r, passedTemplate) } else { fmt.Fprintln(w, "Böyle bir bilgi yok bro") } }
// Search site func search(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) // Get the Settings key := datastore.NewKey(c, "SearchSettings", "Settings", 0, nil) var settings Settings datastore.Get(c, key, &settings) // Create a new http client, supplying the API key we // generated to identify our application, and the urlfetch // transport necessary to make HTTP calls on App Engine transport := &transport.APIKey{ Key: settings.APIKey, Transport: &urlfetch.Transport{Context: c}} client := &http.Client{Transport: transport} // Create the service customsearchService, _ := customsearch.New(client) search, err := customsearchService.Cse.List(r.FormValue("q")).Cx(settings.CX).Do() if err != nil { errorHandler.Error(w, r, err.Error()) return } // Create a model results := search.Items resultModel := make([]Result, len(results)) for i := 0; i < len(results); i++ { resultModel[i].Link = results[i].Link resultModel[i].Snippet = results[i].Snippet } // Merge the models type PassedData struct { Results []Result Query string } passedData := PassedData{ Results: resultModel, Query: r.FormValue("q"), } passedTemplate := new(bytes.Buffer) template.Must(template.ParseFiles("statuscode.ch/search/templates/search.html")).Execute(passedTemplate, passedData) render.Render(w, r, passedTemplate) }
// Edit a page func editPage(w http.ResponseWriter, r *http.Request) { // Get identifier identifier := strings.Replace(r.URL.Path, "/admin/pages/edit/", "", 1) // Check if page exists exists, page := getPage(identifier, r) if exists == false { http.Redirect(w, r, "/admin/pages/", http.StatusFound) return } // Edit page if csrf.ValidateToken(r, r.FormValue("CSRFToken")) && r.FormValue("url") != "" { c := appengine.NewContext(r) page := Pages{ URL: r.FormValue("url"), Title: r.FormValue("title"), Content: []byte(r.FormValue("content")), } _, err := datastore.Put(c, datastore.NewKey(c, "Pages", r.FormValue("url"), 0, nil), &page) if err != nil { errorHandler.Error(w, r, err.Error()) return } cache.DeleteCache(r, "getPage-"+r.FormValue("url")) cache.DeleteCache(r, "getPage-"+identifier) cache.DeleteCache(r, "getAllPages") http.Redirect(w, r, "/admin/pages/", http.StatusFound) return } // Merge the data type PassedData struct { CSRFToken string Page Pages } passedData := PassedData{ CSRFToken: csrf.GetToken(r), Page: page, } passedTemplate := new(bytes.Buffer) template.Must(template.ParseFiles("statuscode.ch/pages/templates/editPage.html")).Execute(passedTemplate, passedData) render.Render(w, r, passedTemplate) }
func editSuggestion(w http.ResponseWriter, r *http.Request) { trimPath := strings.Trim(r.URL.Path, "/admin/post/suggestion/edit/") postID, _ := strconv.Atoi(trimPath) postID64 := int64(postID) c := appengine.NewContext(r) key := datastore.NewKey(c, "PostSuggestion", "", postID64, nil) var suggestion PostSuggestion datastore.Get(c, key, &suggestion) if suggestion.Content != nil { if csrf.ValidateToken(r, r.FormValue("CSRFToken")) { if r.Method == "POST" { c := appengine.NewContext(r) suggestion.Content = []byte(r.FormValue("ContentString")) datastore.Put(c, datastore.NewKey(c, "PostSuggestion", "", postID64, nil), &suggestion) http.Redirect(w, r, r.Referer(), http.StatusFound) } } suggestion.ID = postID64 suggestion.ContentString = string(suggestion.Content) type PassedData struct { CSRFToken string Suggestion PostSuggestion } passedData := PassedData{ CSRFToken: csrf.GetToken(r), Suggestion: suggestion, } passedTemplate := new(bytes.Buffer) template.Must(template.ParseFiles("templates/admin/sidebar.html")).Execute(passedTemplate, nil) template.Must(template.ParseFiles("templates/admin/suggestion/edit.html")).Execute(passedTemplate, passedData) render.Render(w, r, passedTemplate) } else { fmt.Fprintln(w, "Böyle bir bilgi yok bro") } }
func managePosts(w http.ResponseWriter, r *http.Request) { if csrf.ValidateToken(r, r.FormValue("CSRFToken")) { if r.Method == "POST" { http.Redirect(w, r, "/admin/post/edit/"+r.FormValue("Sequence"), http.StatusFound) } } type PassedData struct { CSRFToken string } passedData := PassedData{ CSRFToken: csrf.GetToken(r), } passedTemplate := new(bytes.Buffer) template.Must(template.ParseFiles("templates/admin/sidebar.html")).Execute(passedTemplate, nil) template.Must(template.ParseFiles("templates/admin/post/index.html")).Execute(passedTemplate, passedData) render.Render(w, r, passedTemplate) }
// Contact page func contact(w http.ResponseWriter, r *http.Request) { var status template.HTML status = "" // Parse the post data submit, subject, content, recaptcha_challenge_field, recaptcha_response_field, sender := r.FormValue("submit"), r.FormValue("subject"), r.FormValue("content"), r.FormValue("recaptcha_challenge_field"), r.FormValue("recaptcha_response_field"), r.FormValue("sender") // Check if there is content if subject != "" && content != "" && recaptcha_challenge_field != "" && recaptcha_response_field != "" && sender != "" { // Check the captcha if (recaptcha.Validate(r, r.RemoteAddr, recaptcha_challenge_field, recaptcha_response_field)) == true { c := appengine.NewContext(r) msg := &mail.Message{ Sender: "appengine@" + appengine.AppID(c) + ".appspotmail.com", To: []string{"*****@*****.**"}, Subject: subject, Body: sender + "sent you a mail:" + content, } if err := mail.Send(c, msg); err != nil { c.Errorf("Couldn't send email: %v", err) } status = `<div class="alert alert-success"> <a class="close" data-dismiss="alert">×</a> <strong>Woahhh!</strong> You just sent me a mail :-) </div>` } else { status = `<div class="alert alert-error"> <a class="close" data-dismiss="alert">×</a> <strong>Oops!</strong> The captcha was wrong - just try it again! </div>` } } else { if submit == "submit" { status = `<div class="alert alert-error"> <a class="close" data-dismiss="alert">×</a> <strong>Oops!</strong> You should enter all the required fields! </div>` } } passedTemplate := new(bytes.Buffer) template.Must(template.ParseFiles("statuscode.ch/contact/templates/contact.html")).Execute(passedTemplate, status) render.Render(w, r, passedTemplate) }
func newPost(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) u := user.Current(c) if u != nil { if csrf.ValidateToken(r, r.FormValue("CSRFToken")) { if r.Method == "POST" { var post Post keyP := datastore.NewIncompleteKey(c, "Post", nil) var counter Counter keyC := datastore.NewKey(c, "Counter", "", 1, nil) datastore.Get(c, keyC, &counter) counter.Count = counter.Count + 1 // Add Cache Counter mCount := new(bytes.Buffer) encCount := gob.NewEncoder(mCount) encCount.Encode(counter) cache.AddCache(r, "Counter", mCount.Bytes()) c := appengine.NewContext(r) post.Content = []byte(r.FormValue("Content")) post.Sequence = counter.Count datastore.Put(c, keyP, &post) datastore.Put(c, keyC, &counter) } } } type PassedData struct { CSRFToken string } passedData := PassedData{ CSRFToken: csrf.GetToken(r), } passedTemplate := new(bytes.Buffer) template.Must(template.ParseFiles("templates/admin/sidebar.html")).Execute(passedTemplate, nil) template.Must(template.ParseFiles("templates/admin/post/new.html")).Execute(passedTemplate, passedData) render.Render(w, r, passedTemplate) }
// Create a new page func newPage(w http.ResponseWriter, r *http.Request) { if csrf.ValidateToken(r, r.FormValue("CSRFToken")) && r.FormValue("url") != "" { c := appengine.NewContext(r) page := Pages{ URL: r.FormValue("url"), Title: r.FormValue("title"), Content: []byte(r.FormValue("content")), } _, err := datastore.Put(c, datastore.NewKey(c, "Pages", r.FormValue("url"), 0, nil), &page) if err != nil { errorHandler.Error(w, r, err.Error()) return } cache.DeleteCache(r, "getAllPages") http.Redirect(w, r, "/admin/pages/", http.StatusFound) } passedTemplate := new(bytes.Buffer) template.Must(template.ParseFiles("statuscode.ch/pages/templates/newPage.html")).Execute(passedTemplate, csrf.GetToken(r)) render.Render(w, r, passedTemplate) }
// Admin page with the search settings func searchSettings(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) // Write the new settings if csrf.ValidateToken(r, r.FormValue("CSRFToken")) { Settings := Settings{ APIKey: r.FormValue("apikey"), CX: r.FormValue("cx"), } _, err := datastore.Put(c, datastore.NewKey(c, "SearchSettings", "Settings", 0, nil), &Settings) if err != nil { errorHandler.Error(w, r, err.Error()) return } } // Get the Settings key := datastore.NewKey(c, "SearchSettings", "Settings", 0, nil) var settings Settings datastore.Get(c, key, &settings) // Merge the models type PassedData struct { CSRFToken string APIKey string CX string } passedData := PassedData{ CSRFToken: csrf.GetToken(r), APIKey: settings.APIKey, CX: settings.CX, } passedTemplate := new(bytes.Buffer) template.Must(template.ParseFiles("statuscode.ch/search/templates/settings.html")).Execute(passedTemplate, passedData) render.Render(w, r, passedTemplate) }
// Manages the menu func menu(w http.ResponseWriter, r *http.Request) { // Parse the post data if csrf.ValidateToken(r, r.FormValue("CSRFToken")) { cache.DeleteCache(r, "getMenu") c := appengine.NewContext(r) // Check if the user wants to move up if r.FormValue("moveUp") != "" { //get the item to move var navigation Navigation ID, _ := strconv.Atoi(r.FormValue("moveUp")) key := datastore.NewKey(c, "Menu", "", int64(ID), nil) if err := datastore.Get(c, key, &navigation); err != nil { errorHandler.Error(w, r, err.Error()) return } // Move the item over 1 down itemBelowQuery := datastore.NewQuery("Menu").Filter("Order =", navigation.Order-1) var itemBelow []Navigation itemBelowKey, err := itemBelowQuery.GetAll(c, &itemBelow) for i := 0; i < len(itemBelow); i++ { itemBelowStruct := Navigation{ NavTitle: itemBelow[i].NavTitle, NavLink: itemBelow[i].NavLink, Order: itemBelow[i].Order + 1, } _, err = datastore.Put(c, datastore.NewKey(c, "Menu", "", itemBelowKey[i].IntID(), nil), &itemBelowStruct) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } // Move the item 1 up originalItem := Navigation{ NavTitle: navigation.NavTitle, NavLink: navigation.NavLink, Order: navigation.Order - 1, } _, err = datastore.Put(c, datastore.NewKey(c, "Menu", "", int64(ID), nil), &originalItem) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } // Check if the user wants to move down if r.FormValue("moveDown") != "" { //get the item to move var navigation Navigation ID, _ := strconv.Atoi(r.FormValue("moveDown")) key := datastore.NewKey(c, "Menu", "", int64(ID), nil) if err := datastore.Get(c, key, &navigation); err != nil { errorHandler.Error(w, r, err.Error()) return } // Move the item below 1 up itemBelowQuery := datastore.NewQuery("Menu").Filter("Order =", navigation.Order+1) var itemBelow []Navigation itemBelowKey, err := itemBelowQuery.GetAll(c, &itemBelow) for i := 0; i < len(itemBelow); i++ { itemBelowStruct := Navigation{ NavTitle: itemBelow[i].NavTitle, NavLink: itemBelow[i].NavLink, Order: itemBelow[i].Order - 1, } _, err = datastore.Put(c, datastore.NewKey(c, "Menu", "", itemBelowKey[i].IntID(), nil), &itemBelowStruct) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } // Move the item 1 down originalItem := Navigation{ NavTitle: navigation.NavTitle, NavLink: navigation.NavLink, Order: navigation.Order + 1, } _, err = datastore.Put(c, datastore.NewKey(c, "Menu", "", int64(ID), nil), &originalItem) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } // Check if the user wants to delete a menu if r.FormValue("delete") != "" { // delete var navigation Navigation ID, _ := strconv.Atoi(r.FormValue("delete")) key := datastore.NewKey(c, "Menu", "", int64(ID), nil) if err := datastore.Get(c, key, &navigation); err != nil { errorHandler.Error(w, r, err.Error()) return } // Move the item below 1 down itemBelowQuery := datastore.NewQuery("Menu").Filter("Order >", navigation.Order) var itemBelow []Navigation itemBelowKey, err := itemBelowQuery.GetAll(c, &itemBelow) for i := 0; i < len(itemBelow); i++ { itemBelowStruct := Navigation{ NavTitle: itemBelow[i].NavTitle, NavLink: itemBelow[i].NavLink, Order: itemBelow[i].Order - 1, } _, err = datastore.Put(c, datastore.NewKey(c, "Menu", "", itemBelowKey[i].IntID(), nil), &itemBelowStruct) if err != nil { errorHandler.Error(w, r, err.Error()) } } // Delete datastore.Delete(c, key) } NavTitle, NavLink := r.FormValue("NavTitle"), r.FormValue("NavLink") // Check if there is any data if NavTitle != "" && NavLink != "" { c := appengine.NewContext(r) dbQuery := datastore.NewQuery("Menu").Order("Order") dbCount, _ := dbQuery.Count(c) menu := Navigation{ NavTitle: NavTitle, NavLink: NavLink, Order: dbCount + 1, } // We the ID as Key _, err := datastore.Put(c, datastore.NewIncompleteKey(c, "Menu", nil), &menu) if err != nil { errorHandler.Error(w, r, err.Error()) return } } } // Get the menus c := appengine.NewContext(r) q := datastore.NewQuery("Menu").Order("Order") navigation, keys := getAllMenu(r) models := make([]NavigationVM, len(navigation)) for i := 0; i < len(navigation); i++ { if navigation[i].Order == 1 { models[i].First = true } count, _ := q.Count(c) if navigation[i].Order == count { models[i].Latest = true } models[i].Key = keys[i].IntID() models[i].NavTitle = navigation[i].NavTitle models[i].NavLink = navigation[i].NavLink } // Merge the data type PassedData struct { CSRFToken string Navigation []NavigationVM } passedData := PassedData{ CSRFToken: csrf.GetToken(r), Navigation: models, } passedTemplate := new(bytes.Buffer) template.Must(template.ParseFiles("statuscode.ch/menu/templates/manage.html")).Execute(passedTemplate, passedData) render.Render(w, r, passedTemplate) }
func adminRoot(w http.ResponseWriter, r *http.Request) { passedTemplate := new(bytes.Buffer) template.Must(template.ParseFiles("templates/admin/sidebar.html")).Execute(passedTemplate, nil) template.Must(template.ParseFiles("templates/admin/index.html")).Execute(passedTemplate, nil) render.Render(w, r, passedTemplate) }
// This triggers an 404 func NotFound404(w http.ResponseWriter, r *http.Request) { passedTemplate := new(bytes.Buffer) template.Must(template.ParseFiles("templates/404.html")).Execute(passedTemplate, nil) render.Render(w, r, passedTemplate, http.StatusNotFound) }
// This triggers an 403 func Fordbidden403(w http.ResponseWriter, r *http.Request) { passedTemplate := new(bytes.Buffer) template.Must(template.ParseFiles("templates/403.html")).Execute(passedTemplate, nil) render.Render(w, r, passedTemplate, http.StatusForbidden) }