-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.go
86 lines (70 loc) · 1.54 KB
/
app.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package publicbio
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"github.com/gorilla/schema"
"github.com/writeas/web-core/converter"
"io/ioutil"
"log"
"net/http"
"os"
)
const (
serverSoftware = "Public Bio"
softwareURL = "https://publicb.io"
)
var (
// Software version can be set from git env using -ldflags
softwareVer = "0.1.0"
)
type App struct {
cfg *Config
singleUser *Profile
}
func (app *App) multiUser() bool {
return app.singleUser == nil
}
type Config struct {
Host string
Port int
static bool
UserFile string
}
func Serve(cfg *Config) {
app := &App{
cfg: cfg,
}
if cfg.UserFile != "" {
f, err := ioutil.ReadFile(cfg.UserFile)
if err != nil {
log.Fatalf("File error: %v", err)
}
err = json.Unmarshal(f, &app.singleUser)
if err != nil {
log.Fatalf("Unable to read user config: %v", err)
}
fmt.Printf("Results: %v\n", app.singleUser)
} else {
log.Fatal("No user configuration")
}
if app.cfg.static {
if err := renderTemplate(os.Stdout, "profile", app.singleUser); err != nil {
log.Fatal(err)
}
return
}
r := mux.NewRouter()
app.InitRoutes(r)
http.Handle("/", r)
log.Printf("Serving on http://localhost:%d", app.cfg.Port)
http.ListenAndServe(fmt.Sprintf(":%d", app.cfg.Port), nil)
}
func initConverter() {
formDecoder := schema.NewDecoder()
formDecoder.RegisterConverter(converter.NullJSONString{}, converter.JSONNullString)
}
// FormatVersion constructs the version string for the application
func FormatVersion() string {
return serverSoftware + " " + softwareVer
}