diff options
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/parser/main.go | 7 | ||||
-rw-r--r-- | cmd/web/handlers/blog.go | 32 | ||||
-rw-r--r-- | cmd/web/handlers/context.go | 5 | ||||
-rw-r--r-- | cmd/web/handlers/routes.go | 14 | ||||
-rw-r--r-- | cmd/web/main.go | 19 |
5 files changed, 77 insertions, 0 deletions
diff --git a/cmd/parser/main.go b/cmd/parser/main.go new file mode 100644 index 0000000..cc0602a --- /dev/null +++ b/cmd/parser/main.go @@ -0,0 +1,7 @@ +package main + +import "fmt" + +func main() { + fmt.Printf("Markdown compiler app\n") +} diff --git a/cmd/web/handlers/blog.go b/cmd/web/handlers/blog.go new file mode 100644 index 0000000..5a874f4 --- /dev/null +++ b/cmd/web/handlers/blog.go @@ -0,0 +1,32 @@ +package handlers + +import ( + "html/template" + "log" + "net/http" +) + +func (ctx *HandlerContext) blogIndex(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/" { + http.NotFound(w, r) + return + } + + files := []string{ + "./ui/html/base.tmpl.html", + "./ui/html/pages/index.tmpl.html", + } + + compiled, err := template.ParseFiles(files...) + if err != nil { + log.Println(err.Error()) + http.Error(w, "Internal Server Error", 500) + return + } + + err = compiled.ExecuteTemplate(w, "base", ctx) + if err != nil { + log.Println(err.Error()) + http.Error(w, "Internal Server Error", 500) + } +} diff --git a/cmd/web/handlers/context.go b/cmd/web/handlers/context.go new file mode 100644 index 0000000..7df2915 --- /dev/null +++ b/cmd/web/handlers/context.go @@ -0,0 +1,5 @@ +package handlers + +type HandlerContext struct { + Webmaster string +} diff --git a/cmd/web/handlers/routes.go b/cmd/web/handlers/routes.go new file mode 100644 index 0000000..5a57440 --- /dev/null +++ b/cmd/web/handlers/routes.go @@ -0,0 +1,14 @@ +package handlers + +import ( + "github.com/gorilla/mux" +) + +func RegisterEndpoints(webmaster string) *mux.Router { + ctx := HandlerContext{webmaster} + + blogRouter := mux.NewRouter() + blogRouter.HandleFunc("/", ctx.blogIndex) + + return blogRouter +} diff --git a/cmd/web/main.go b/cmd/web/main.go new file mode 100644 index 0000000..748e4bb --- /dev/null +++ b/cmd/web/main.go @@ -0,0 +1,19 @@ +package main + +import ( + "flag" + "fmt" + "log" + "net/http" + + "paterissa.net/mblog/cmd/web/handlers" +) + +func main() { + webmaster := flag.String("webmaster", "", "host of the website") + port := flag.Int("port", 5050, "the hosting port") + flag.Parse() + + router := handlers.RegisterEndpoints(*webmaster) + log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), router)) +} |