Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Linus
apb
Commits
74fe4565
Commit
74fe4565
authored
Oct 09, 2018
by
Linus
🤔
Browse files
Changed some functions to prepare for swaps, removed -listings cl argument and added -data
parent
c1de465f
Changes
3
Hide whitespace changes
Inline
Side-by-side
data.go
View file @
74fe4565
...
...
@@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"io/ioutil"
"sync"
"github.com/BurntSushi/toml"
)
...
...
@@ -15,17 +16,31 @@ type Listing struct {
PostTime
string
}
type
TutSwap
struct
{
Email
string
From
string
To
string
Message
string
PostTime
string
}
type
ListingsStruct
map
[
string
][]
Listing
type
SwapStruct
map
[
string
][]
TutSwap
var
listings
ListingsStruct
var
listingsMutex
=
&
sync
.
Mutex
{}
var
swaps
SwapStruct
var
swapMutex
=
&
sync
.
Mutex
{}
// loadListings tries to load the listings from
the file specified in the config
.
// loadListings tries to load the listings from
listingsFile
.
// If this file does not exist, it will create a new one.
// You should lock the listingsMutex before doing this.
func
loadListings
()
error
{
data
,
err
:=
ioutil
.
ReadFile
(
listingsFile
)
if
err
!=
nil
{
listings
=
ListingsStruct
{}
err
=
save
L
istings
(
listingsFile
)
err
=
save
Struct
(
l
istings
,
listingsFile
)
fmt
.
Println
(
"There was no listings file, so I created a new one."
)
return
err
}
...
...
@@ -35,10 +50,27 @@ func loadListings() error {
return
nil
}
func
saveListings
(
filename
string
)
error
{
// loadSwapListings tries to load the listings from swapListingsFile.
// If this file does not exist, it will create a new one.
// You should lock the swapMutex before doing this.
func
loadSwapListings
()
error
{
data
,
err
:=
ioutil
.
ReadFile
(
swapListingsFile
)
if
err
!=
nil
{
swaps
=
SwapStruct
{}
err
=
saveStruct
(
swaps
,
swapListingsFile
)
fmt
.
Println
(
"There was no swaps file, so I created a new one."
)
return
err
}
if
_
,
err
:=
toml
.
Decode
(
string
(
data
),
&
swaps
);
err
!=
nil
{
return
err
}
return
nil
}
func
saveStruct
(
s
interface
{},
filename
string
)
error
{
var
buf
bytes
.
Buffer
encoder
:=
toml
.
NewEncoder
(
&
buf
)
err
:=
encoder
.
Encode
(
listing
s
)
err
:=
encoder
.
Encode
(
s
)
if
err
!=
nil
{
return
err
}
...
...
functions.go
View file @
74fe4565
...
...
@@ -5,7 +5,6 @@ import (
"net/http"
"strconv"
"strings"
"sync"
"time"
)
...
...
@@ -19,8 +18,6 @@ var regFunctions = map[string]func(http.ResponseWriter, *http.Request) error{
"/info"
:
info
,
}
var
listingsMutex
=
&
sync
.
Mutex
{}
func
errorClosure
(
f
func
(
http
.
ResponseWriter
,
*
http
.
Request
)
error
)
func
(
http
.
ResponseWriter
,
*
http
.
Request
)
{
return
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
err
:=
f
(
w
,
r
)
...
...
@@ -149,7 +146,7 @@ func addListing(w http.ResponseWriter, r *http.Request) error {
listingsMutex
.
Lock
()
defer
listingsMutex
.
Unlock
()
listings
[
subject
]
=
append
(
listings
[
subject
],
listing
)
err
=
save
L
istings
(
listingsFile
)
err
=
save
Struct
(
l
istings
,
listingsFile
)
if
err
!=
nil
{
return
err
}
...
...
@@ -201,7 +198,7 @@ func doDel(w http.ResponseWriter, r *http.Request) error {
numDeleted
+=
l
-
i
}
err
=
save
L
istings
(
listingsFile
)
err
=
save
Struct
(
l
istings
,
listingsFile
)
if
err
!=
nil
{
return
err
}
...
...
main.go
View file @
74fe4565
...
...
@@ -9,6 +9,7 @@ import (
)
var
listingsFile
string
var
swapListingsFile
string
func
hasSubject
(
subject
string
)
bool
{
for
_
,
s
:=
range
subjects
{
...
...
@@ -24,19 +25,21 @@ func main() {
portPtr
:=
flag
.
String
(
"port"
,
strconv
.
Itoa
(
DefaultPort
),
"The port the server runs on"
)
ipStrPtr
:=
flag
.
String
(
"ip"
,
"0.0.0.0"
,
"The ip the server runs on"
)
listings
Path
:=
flag
.
String
(
"
listings
"
,
"/data/
listings.toml
"
,
"The path of your
configuration
file"
)
data
Path
:=
flag
.
String
(
"
data
"
,
"/data/"
,
"The path of your
data
file
s
"
)
flag
.
Parse
()
http
.
HandleFunc
(
"/static/"
,
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
http
.
ServeFile
(
w
,
r
,
r
.
URL
.
Path
[
1
:
])
})
listingsFile
=
*
listings
Path
listingsFile
=
*
dataPath
+
"
listings
.toml"
err
:=
loadListings
()
if
err
!=
nil
{
panic
(
err
)
}
swapListingsFile
=
*
dataPath
+
"tut_swap.toml"
RegisterAll
()
fmt
.
Printf
(
"Starting server at port %s and IP %s
\n
"
,
string
(
*
portPtr
),
string
(
*
ipStrPtr
))
...
...
Linus
🤔
@h
mentioned in commit
370f131c
·
Oct 09, 2018
mentioned in commit
370f131c
mentioned in commit 370f131c2795bbb1671862c4664a0d31941ecf08
Toggle commit list
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment