Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
# webapp-go
The sample app from Writing Web Applications on golang.org.

# Get Azure SDK package:

```sh
go get -d github.com/Azure/azure-sdk-for-go/management
```
70 changes: 64 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,85 @@
package main

import (
"bytes"
"errors"
"github.com/Azure/azure-sdk-for-go/storage"
"html/template"
"io/ioutil"
"net/http"
"os"
"regexp"
)

var blobService storage.BlobStorageClient
var containerName = os.Getenv("AZURE_STORAGE_CONTAINER_NAME")

type Page struct {
Title string
Body []byte
}

func initAzureStorage() {
var storageKey = os.Getenv("AZURE_STORAGE_ACCESS_KEY")
var storageName = os.Getenv("AZURE_STORAGE_ACCOUNT")
client, err := storage.NewBasicClient(storageName, storageKey)
if err != nil {
panic(err)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we return an error from this function instead of panicing, and handle the error in main by writing to stderr?

}

blobService = client.GetBlobService()
ok, err := blobService.ContainerExists(containerName)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use CreateContainerIfNotExists instead?

if err != nil {
panic(err)
} else if !ok {
err = blobService.CreateContainer(containerName, storage.ContainerAccessTypePrivate)
if err != nil {
panic(err)
}
}
}

func (p *Page) save() error {
filename := p.Title + ".txt"
return ioutil.WriteFile(filename, p.Body, 0600)
ok, err := blobService.BlobExists(containerName, p.Title)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't just use CreateBlockBlobFromReader?

if err != nil {
return err
}

if ok {
err = blobService.DeleteBlob(containerName, p.Title)
if err != nil {
return err
}
}

err = blobService.CreateBlockBlobFromReader(containerName, p.Title, uint64(len(p.Body)), bytes.NewReader(p.Body))
if err != nil {
return err
}

return nil
}

func loadPage(title string) (*Page, error) {
filename := title + ".txt"
body, err := ioutil.ReadFile(filename)
if err != nil {
ok, err := blobService.BlobExists(containerName, title)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't just use GetBlob and handle error?

if ok {
var body []byte
data, err := blobService.GetBlob(containerName, title)
if err == nil {
body, err = ioutil.ReadAll(data)
}

if data != nil {
data.Close()
}

return &Page{Title: title, Body: body}, err
} else {
if err == nil {
err = errors.New("Page not found.")
}
return nil, err
}
return &Page{Title: title, Body: body}, nil
}

func viewHandler(w http.ResponseWriter, r *http.Request, title string) {
Expand Down Expand Up @@ -81,6 +137,8 @@ func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.Handl
}

func main() {
initAzureStorage()

http.HandleFunc("/view/", makeHandler(viewHandler))
http.HandleFunc("/edit/", makeHandler(editHandler))
http.HandleFunc("/save/", makeHandler(saveHandler))
Expand Down