Skip to content

Commit

Permalink
Merge pull request #108 from mikedewar/settings_file
Browse files Browse the repository at this point in the history
Settings file
  • Loading branch information
nikhan committed Oct 5, 2015
2 parents 91e0367 + 568ebe6 commit f8105fb
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
36 changes: 34 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,53 @@
package main

import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"os"

"github.com/mitchellh/go-homedir"
"github.com/nytlabs/st-core/server"
)

func main() {

s := server.NewServer()
// Unpack settings file, or create a new one if necessary
var settings server.Settings

dir, err := homedir.Dir()
if err != nil {
log.Fatal(err)
}
fname := dir + "/.st.json"
d, err := ioutil.ReadFile(fname)
if err != nil {
if os.IsNotExist(err) {
// make a fresh settings file
log.Println("creating new settings file at", fname)
newSettings := server.NewSettings()
d, err = json.Marshal(newSettings)
err = ioutil.WriteFile(fname, d, 0644)
if err != nil {
log.Fatal(err)
}
} else {
log.Fatal(err)
}
}
err = json.Unmarshal(d, &settings)
if err != nil {
log.Fatal(err)
}

s := server.NewServer(settings)
r := s.NewRouter()

http.Handle("/", r)

log.Println("serving on 7071")
err := http.ListenAndServe(":7071", nil)
err = http.ListenAndServe(":7071", nil)
if err != nil {
log.Panicf(err.Error())
}
Expand Down
18 changes: 17 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,23 @@ const (
PARAM = "param"
)

// user-session specific settings
type Settings struct {
Autosave bool
GithubUserToken string
}

// NewSettings returns the default settings object
func NewSettings() Settings {
return Settings{
Autosave: true,
GithubUserToken: "",
}
}

// The Server maintains a set of handlers that coordinate the creation of Nodes
type Server struct {
settings Settings
groups map[int]*Group // TODO these maps aren't strictly necessary, but save constantly performing depth first searches
parents map[int]int
blocks map[int]*BlockLedger
Expand All @@ -46,7 +61,7 @@ type Server struct {
}

// NewServer starts a new Server. This object is immediately up and running.
func NewServer() *Server {
func NewServer(settings Settings) *Server {
groups := make(map[int]*Group)
groups[0] = &Group{
Label: "root",
Expand All @@ -63,6 +78,7 @@ func NewServer() *Server {
sourceLibrary := core.GetSources()
parents := make(map[int]int)
s := &Server{
settings: settings,
lastID: 0,
parents: parents,
groups: groups,
Expand Down
4 changes: 2 additions & 2 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ func procTestResponse(res *http.Response, t *testing.T, expectedCode int) {
}

func TestEndpoints(t *testing.T) {

s := NewServer()
settings := NewSettings()
s := NewServer(settings)
r := s.NewRouter()
server := httptest.NewServer(r)
defer server.Close()
Expand Down

0 comments on commit f8105fb

Please sign in to comment.