Skip to content

Commit

Permalink
separated out utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
bytbox committed Jul 31, 2010
1 parent 0f98193 commit 9e609df
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 26 deletions.
4 changes: 2 additions & 2 deletions Makefile
Expand Up @@ -7,8 +7,8 @@ all: obsidian
obsidian: main.${O}
${LD} -o $@ main.${O}

main.${O}: main.go
${GC} -o $@ main.go
main.${O}: main.go util.go
${GC} -o $@ main.go util.go

clean:
rm obsidian *.${O}
30 changes: 6 additions & 24 deletions main.go
Expand Up @@ -3,7 +3,6 @@ package main
import (
"fmt"
"http"
"io/ioutil"
"opts"
"os"
"path"
Expand All @@ -16,27 +15,6 @@ var blogroot = opts.Option("r",
"the root directory for blog data",
"/usr/share/obsidian")

func ReadDir(dirname string) (ret []*os.FileInfo) {
ret, err := ioutil.ReadDir(dirname)
if err != nil {
// the directory doesn't exist - create it
err = os.MkdirAll(dirname, 0755)
}
return
}

func ReadFile(filename string) (contents string) {
contentarry, err := ioutil.ReadFile(filename)
if err != nil {
// the file doesn't exist - create the directory and the file
dirname, _ := path.Split(filename)
err = os.MkdirAll(dirname, 0755)
ioutil.WriteFile(filename, []byte{}, 0644)
}
contents = string(contentarry)
return
}

func main() {
// option setup
opts.Description("lightweight http blog server")
Expand Down Expand Up @@ -69,10 +47,10 @@ var (

func readTemplate(name string) string {
templateDirectory := path.Join(*blogroot,"templates")
return ReadFile(path.Join(templateDirectory, name))
return readFile(path.Join(templateDirectory, name))
}

func readData() {
func readTemplates() {
// read the templates
genTemplate = readTemplate("gen.html")
adminTemplate = readTemplate("admin.html")
Expand All @@ -82,6 +60,10 @@ func readData() {
categoryTemplate = readTemplate("category.html")
}

func readData() {
readTemplates()
}

func TestServer(c *http.Conn, req *http.Request) {
fmt.Fprintf(c,"Hello, world!\n")
}
Expand Down
35 changes: 35 additions & 0 deletions util.go
@@ -0,0 +1,35 @@
package main

import (
"io/ioutil"
"os"
"path"
)

func readDir(dirname string) (ret []*os.FileInfo) {
ret, err := ioutil.ReadDir(dirname)
if err != nil {
// the directory doesn't exist - create it
err = os.MkdirAll(dirname, 0755)
}
return
}

func walkDir(dirname string, v path.Visitor) {
// first make sure the directory exists
readDir(dirname)
// now walk it
path.Walk(dirname, v, nil)
}

func readFile(filename string) (contents string) {
contentarry, err := ioutil.ReadFile(filename)
if err != nil {
// the file doesn't exist - create the directory and the file
dirname, _ := path.Split(filename)
err = os.MkdirAll(dirname, 0755)
ioutil.WriteFile(filename, []byte{}, 0644)
}
contents = string(contentarry)
return
}

0 comments on commit 9e609df

Please sign in to comment.