-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
51 lines (43 loc) · 1.87 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"flag"
"github.com/gorilla/mux"
"log"
"net/http"
"os"
"time"
)
// Specifies a value that can be provided via either an environment variable or command line flag, and which will
// default to some particular value if neither are provided.
// Returns a string pointer that will be valid after calling flag.Parse()
func EnvFlagOrDefaultString(flagName string, envName string, desc string, def string) *string {
var value string
value, foundEnv := os.LookupEnv(envName)
if !foundEnv {
value = def
}
return flag.String(flagName, value, desc)
}
func main() {
// Obtain configuration strings
ip := EnvFlagOrDefaultString("ip", "IP", "The IP to listen on", "0.0.0.0")
port := EnvFlagOrDefaultString("port", "PORT", "The port to listen on", "8080")
viewDir := EnvFlagOrDefaultString("viewDir", "VIEW_DIR", "The directory that view (.gohtml) files are found in", "./views")
articleDir := EnvFlagOrDefaultString("articleDir", "ARTICLE_DIR", "The directory that article (.md) files are found in", "./articles")
staticDir := EnvFlagOrDefaultString("staticDir", "STATIC_DIR", "The directory from which static files are served", "./static")
updateInterval := EnvFlagOrDefaultString("updateInterval", "UPDATE_INTERVAL", "The interval at which changes to articles and views are checked for.", "5m")
flag.Parse()
// Parse the interval at which updates occur
parsedUpdateInterval, err := time.ParseDuration(*updateInterval)
if err != nil {
log.Fatal(err)
}
// Initialize the blog update and start the repeated update task.
b := NewBlog(*viewDir, *articleDir)
go b.UpdateTask(parsedUpdateInterval)
r := mux.NewRouter()
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir(*staticDir))))
r.HandleFunc("/", b.GetPostList)
r.HandleFunc("/{year}/{month}/{day}/{permaTitle}", b.GetPost)
log.Fatal(http.ListenAndServe(*ip+":"+*port, r))
}