Skip to content

Instantly share code, notes, and snippets.

@inhies
Last active October 13, 2023 22:21
Show Gist options
  • Select an option

  • Save inhies/f5b0592b3dc8195aa5a3d551db96496d to your computer and use it in GitHub Desktop.

Select an option

Save inhies/f5b0592b3dc8195aa5a3d551db96496d to your computer and use it in GitHub Desktop.

Revisions

  1. inhies revised this gist Nov 4, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion server.go
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@ import (
    )

    var (
    templateDir = ".//templates"
    templateDir = "./webui/templates"
    staticDir = "./webui/static"
    reloadTemplates bool
    routes = map[string]http.HandlerFunc{
  2. inhies revised this gist Nov 4, 2016. 1 changed file with 29 additions and 23 deletions.
    52 changes: 29 additions & 23 deletions server.go
    Original file line number Diff line number Diff line change
    @@ -2,16 +2,17 @@ package main

    import (
    "fmt"
    "github.com/howeyc/fsnotify"
    "html/template"
    "io/ioutil"
    "log"
    "net/http"
    "os"
    "path/filepath"
    "strings"

    "gopkg.in/fsnotify.v1"
    )

    var (
    templateDir = "./webui/templates"
    templateDir = ".//templates"
    staticDir = "./webui/static"
    reloadTemplates bool
    routes = map[string]http.HandlerFunc{
    @@ -41,39 +42,44 @@ func init() {
    go func() {
    for {
    select {
    case ev := <-watcher.Event:
    fmt.Println("event:", ev)
    case ev := <-watcher.Events:
    log.Println("event:", ev)
    reloadTemplates = true
    case err := <-watcher.Error:
    fmt.Println("error:", err)
    case err := <-watcher.Errors:
    log.Println("error:", err)
    }
    }
    }()

    err = watcher.Watch(templateDir)
    err = watcher.Add(templateDir)
    if err != nil {
    fmt.Println(err)
    }
    }

    func parseTemplates() {
    t = template.New("templates")
    err := filepath.Walk(templateDir,
    func(path string, info os.FileInfo, err error) error {
    if err != nil {
    return err
    }
    if info.IsDir() || !strings.HasSuffix(path, ".html") {
    return nil // Ignore directories and other files
    }
    _, err = t.ParseFiles(path) // Parse the template
    return err
    })

    var allFiles []string

    files, err := ioutil.ReadDir(templateDir)
    if err != nil {
    fmt.Println(err)
    os.Exit(1)
    fmt.Println("Error reading template dir")
    }

    for _, file := range files {
    filename := file.Name()
    if strings.HasSuffix(filename, ".html") {
    allFiles = append(allFiles, templateDir+"/"+filename)
    fmt.Println("Parsing", templateDir+"/"+filename)
    }
    }

    if err != nil {
    log.Fatal(err)
    }
    t = template.Must(template.ParseFiles(allFiles...))

    }

    func main() {
    @@ -85,7 +91,7 @@ func main() {
    http.HandleFunc(route, function)
    }

    http.ListenAndServe(":8080", nil)
    log.Fatal(http.ListenAndServe(":8080", nil))
    }

    // AssetsHandler is a static file server that serves everything in
  3. inhies created this gist Oct 18, 2016.
    107 changes: 107 additions & 0 deletions server.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,107 @@
    package main

    import (
    "fmt"
    "github.com/howeyc/fsnotify"
    "html/template"
    "net/http"
    "os"
    "path/filepath"
    "strings"
    )

    var (
    templateDir = "./webui/templates"
    staticDir = "./webui/static"
    reloadTemplates bool
    routes = map[string]http.HandlerFunc{
    "/": rootHandler,
    }
    t *template.Template
    cwd string
    pages = map[string]Page{
    "index": indexPage{
    Title: "Index",
    },
    }
    )

    type Page interface{}
    type indexPage struct {
    Title string
    }

    func init() {
    watcher, err := fsnotify.NewWatcher()
    if err != nil {
    fmt.Println(err)
    }

    // Process events
    go func() {
    for {
    select {
    case ev := <-watcher.Event:
    fmt.Println("event:", ev)
    reloadTemplates = true
    case err := <-watcher.Error:
    fmt.Println("error:", err)
    }
    }
    }()

    err = watcher.Watch(templateDir)
    if err != nil {
    fmt.Println(err)
    }
    }

    func parseTemplates() {
    t = template.New("templates")
    err := filepath.Walk(templateDir,
    func(path string, info os.FileInfo, err error) error {
    if err != nil {
    return err
    }
    if info.IsDir() || !strings.HasSuffix(path, ".html") {
    return nil // Ignore directories and other files
    }
    _, err = t.ParseFiles(path) // Parse the template
    return err
    })

    if err != nil {
    fmt.Println(err)
    os.Exit(1)
    }
    }

    func main() {
    parseTemplates()
    cwd = "./"
    http.HandleFunc("/static/", assetsHandler)

    for route, function := range routes {
    http.HandleFunc(route, function)
    }

    http.ListenAndServe(":8080", nil)
    }

    // AssetsHandler is a static file server that serves everything in
    // static directory
    func assetsHandler(w http.ResponseWriter, r *http.Request) {
    http.ServeFile(w, r, staticDir+r.URL.Path)
    }

    // RootHandler handles the "/" connections
    func rootHandler(w http.ResponseWriter, r *http.Request) {
    if reloadTemplates {
    parseTemplates()
    reloadTemplates = false
    }
    err := t.ExecuteTemplate(w, "index", pages["index"])
    if err != nil {
    fmt.Println(err)
    }
    }