-
Notifications
You must be signed in to change notification settings - Fork 24
azure storage #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
} | ||
|
||
blobService = client.GetBlobService() | ||
ok, err := blobService.ContainerExists(containerName) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use |
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't just use |
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't just use |
||
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) { | ||
|
@@ -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)) | ||
|
There was a problem hiding this comment.
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?