-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
180 lines (150 loc) · 5.75 KB
/
config.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package eprinttools
//
// Service configuration management
//
import (
"database/sql"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
// 3rd Party Package
"gopkg.in/yaml.v3"
)
// Config holds a configuration file structure used by EPrints Extended API
// Configuration file is expected to be in JSON format.
type Config struct {
// Hostname for running service
Hostname string `json:"hostname"`
// BaseURL is the base URL passed to any client configuration
BaseURL string `json:"base_url"`
// Logfile
Logfile string `json:"logfile,omitempty"`
// PeopleCSV points to a curated people.csv file, this file contains crosswalk ids and general name info
PeopleCSV string `json:"people_csv,omitempty"`
// GroupsCSV points to a curated groups.csv file, this file contains crosswalk ids and general group info
GroupsCSV string `json:"groups_csv,omitempty"`
// Repositories are defined by a REPO_ID (string)
// that points at a MySQL Db connection string
Repositories map[string]*DataSource `json:"eprint_repositories"`
// Connections is a map to database connections
Connections map[string]*sql.DB `json:"-"`
// JSONStore is the name of a MySQL 8 database in DNS format
// that holds for each repository. The tables have two columns
// eprint id (INTEGER) and document (JSON COLUMNS).
// The JSONStore is where data is harvested into and where it is
// staged for writing out to a published Object store like S3.
JSONStore string `json:"jsonstore"`
// Jdb holds the MySQL connector to the jsonstore
Jdb *sql.DB `json:"-"`
// Routes holds the mapping of end points to repository id
// instances.
Routes map[string]map[string]func(http.ResponseWriter, *http.Request, string, []string) (int, error) `json:"-"`
// ProjectDir is the directory where you stage harvested content
ProjectDir string `json:"project_dir,omitempty"`
// Htdocs is the directory where aggregated information and
// website content is generated to after running the harvester.
Htdocs string `json:"htdocs,omitempty"`
// PandocServer is the URL to the Pandoc server
// E.g. localhost:8080
PandocServer string `json:"pandoc_server,omitempty"`
}
// DataSource can contain one or more types of datasources. E.g.
// E.g. dsn for MySQL connections and also data for REST API access.
type DataSource struct {
// DSN is used to connect to a MySQL style DB.
DSN string `json:"dsn,omitempty"`
// BaseURL is the URL to use in constructing eprint id, document id
// and file id attribute strings.
BaseURL string `json:"base_url,omitempty"`
// Rest is used to connect to EPrints REST API
// NOTE: assumes Basic Auth for authentication
RestAPI string `json:"rest,omitempty"`
// Write enables the write API for creating
// or replacing EPrint records via SQL database calls.
// The default value is false.
Write bool `json:"write" default:"false"`
// DefaultCollection
DefaultCollection string `json:"default_collection,omitempty"`
// DefaultOfficialURL
DefaultOfficialURL string `json:"default_official_url,omitempty"`
// DefaultRights (i.e. usage statement)
DefaultRights string `json:"default_rights,omitempty"`
// DefaultIsRefereed (i.e. refereed field applied for "article" types.
// Caltech Library defaults this to "TRUE" for type "article".
DefaultRefereed string `json:"default_refereed,omitempty"`
// DefaultStatus is the eprint.eprint_status value to set by default
// on creating new eprint records. Normally this is "inbox" or "buffer"
DefaultStatus string `json:"default_status,omitempty"`
// StripTags bool is true then an EPrint Abstract will have XML/HTML tags
// stripped on import.
StripTags bool `json:"strip_tags,omitempty"`
// TableMap holds the mapping of tables and columns for
// the repository presented.
TableMap map[string][]string `json:"tables,omitempty"`
// PublicOnly is a boolean indicating if the "harvested" content
// should be restricted to public records.
PublicOnly bool `json:"is_public,omitempty"`
}
func DefaultConfig() []byte {
config := new(Config)
if _, err := os.Stat("people.csv"); err == nil {
config.PeopleCSV = "people.csv"
}
if _, err := os.Stat("groups.csv"); err == nil {
config.GroupsCSV = "groups.csv"
}
config.Hostname = "localhost:8484"
config.BaseURL = "http//localhost:8484"
config.JSONStore = "$DB_USER:$DB_PASSWORD@/collections"
config.Htdocs = "htdocs"
config.ProjectDir = "."
config.PandocServer = "localhost:3030"
repo := new(DataSource)
repo.DSN = `$DB_USER:$DB_PASSWORD@/authors`
repo.BaseURL = `http://authors.example.edu`
repo.Write = false
repo.DefaultCollection = `authors`
repo.DefaultRights = "No commercial reproduction, distribution, display or performance rights in this work are provided."
repo.DefaultRefereed = "TRUE"
repo.DefaultStatus = "inbox"
repo.StripTags = true
repo.PublicOnly = true
config.Repositories = map[string]*DataSource{
"authors": repo,
}
src, _ := json.MarshalIndent(config, "", " ")
return src
}
// LoadConfig reads a JSON file and returns a Config structure
// or error.
func LoadConfig(fname string) (*Config, error) {
config := new(Config)
config.Repositories = map[string]*DataSource{}
if src, err := ioutil.ReadFile(fname); err != nil {
return nil, err
} else {
// Since we should be OK, unmarshal in into active config
if strings.HasSuffix(fname, ".yaml") {
if err = yaml.Unmarshal(src, &config); err != nil {
return nil, fmt.Errorf("Unmarshaling %q failed, %s", fname, err)
}
} else {
if err = jsonDecode(src, &config); err != nil {
return nil, fmt.Errorf("Unmarshaling %q failed, %s", fname, err)
}
}
if config.Hostname == "" {
config.Hostname = "localhost:8484"
}
if config.BaseURL == "" {
config.BaseURL = fmt.Sprintf("http://%s", config.Hostname)
}
if config.Htdocs == "" {
config.Htdocs = "htdocs"
}
}
return config, nil
}