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
cf89d78a
Commit
cf89d78a
authored
Oct 14, 2018
by
Linus
🤔
Browse files
Some refactoring
parent
8b2a5b4b
Pipeline
#78476
passed with stages
in 25 seconds
Changes
6
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
data.go
View file @
cf89d78a
...
...
@@ -9,14 +9,16 @@ import (
"github.com/BurntSushi/toml"
)
type
Listing
struct
{
// partnerListing represents a tutorium partner listing.
type
partnerListing
struct
{
Email
string
NumPeople
int
Message
string
PostTime
string
}
type
TutSwap
struct
{
// tutSwapListing represents a tutorium swap listing.
type
tutSwapListing
struct
{
Email
string
From
string
To
string
...
...
@@ -24,13 +26,13 @@ type TutSwap struct {
PostTime
string
}
type
ListingsStruct
map
[
string
][]
Listing
type
S
wap
Struct
map
[
string
][]
T
utSwap
type
partnerMap
map
[
string
][]
partner
Listing
type
s
wap
Map
map
[
string
][]
t
utSwap
Listing
var
listings
ListingsStruct
var
listings
partnerMap
var
listingsMutex
=
&
sync
.
Mutex
{}
var
swaps
S
wap
Struct
var
swaps
s
wap
Map
var
swapMutex
=
&
sync
.
Mutex
{}
// loadListings tries to load the listings from listingsFile.
...
...
@@ -39,7 +41,7 @@ var swapMutex = &sync.Mutex{}
func
loadListings
()
error
{
data
,
err
:=
ioutil
.
ReadFile
(
listingsFile
)
if
err
!=
nil
{
listings
=
ListingsStruct
{}
listings
=
partnerMap
{}
err
=
saveStruct
(
listings
,
listingsFile
)
fmt
.
Println
(
"There was no listings file, so I created a new one."
)
return
err
...
...
@@ -56,7 +58,7 @@ func loadListings() error {
func
loadSwapListings
()
error
{
data
,
err
:=
ioutil
.
ReadFile
(
swapListingsFile
)
if
err
!=
nil
{
swaps
=
S
wap
Struct
{}
swaps
=
s
wap
Map
{}
err
=
saveStruct
(
swaps
,
swapListingsFile
)
fmt
.
Println
(
"There was no swaps file, so I created a new one."
)
return
err
...
...
functions.go
View file @
cf89d78a
...
...
@@ -28,7 +28,9 @@ func errorClosure(f func(http.ResponseWriter, *http.Request) error) func(http.Re
}
}
func
RegisterAll
()
{
// the registerAll function registers all names in regFunctions to the
// error closure of their response functions
func
registerAll
()
{
for
key
,
value
:=
range
regFunctions
{
http
.
HandleFunc
(
key
,
errorClosure
(
value
))
}
...
...
main.go
View file @
cf89d78a
...
...
@@ -55,7 +55,7 @@ func main() {
panic
(
err
)
}
R
egisterAll
()
r
egisterAll
()
fmt
.
Printf
(
"Starting server at port %s and IP %s
\n
"
,
string
(
*
portPtr
),
string
(
*
ipStrPtr
))
log
.
Fatal
(
http
.
ListenAndServe
(
*
ipStrPtr
+
":"
+
string
(
*
portPtr
),
nil
))
...
...
search.go
View file @
cf89d78a
...
...
@@ -18,7 +18,7 @@ func search(w http.ResponseWriter, r *http.Request) error {
type
otherTut
struct
{
Name
string
I
d
string
I
D
string
Subject
string
}
...
...
@@ -73,10 +73,10 @@ func doSearch(w http.ResponseWriter, r *http.Request) error {
toPush
:=
otherTut
{}
if
splits
[
1
]
==
""
{
toPush
.
Name
=
"Leeres Tutorium"
toPush
.
I
d
=
""
toPush
.
I
D
=
""
}
else
{
toPush
.
Name
=
"Tut. "
+
splits
[
1
]
toPush
.
I
d
=
splits
[
1
]
toPush
.
I
D
=
splits
[
1
]
}
toPush
.
Subject
=
subj
otherTuts
=
append
(
otherTuts
,
toPush
)
...
...
@@ -84,11 +84,11 @@ func doSearch(w http.ResponseWriter, r *http.Request) error {
}
sort
.
Slice
(
otherTuts
,
func
(
i
,
j
int
)
bool
{
id1
,
err
:=
strconv
.
Atoi
(
otherTuts
[
i
]
.
I
d
)
id1
,
err
:=
strconv
.
Atoi
(
otherTuts
[
i
]
.
I
D
)
if
err
!=
nil
{
id1
=
0
}
id2
,
err
:=
strconv
.
Atoi
(
otherTuts
[
j
]
.
I
d
)
id2
,
err
:=
strconv
.
Atoi
(
otherTuts
[
j
]
.
I
D
)
if
err
!=
nil
{
id2
=
0
}
...
...
@@ -143,7 +143,7 @@ func addListing(w http.ResponseWriter, r *http.Request) error {
if
!
hasSubject
(
strings
.
Split
(
subject
,
"::"
)[
0
])
{
return
renderError
(
"Das angegebene Fach existiert nicht"
,
w
,
r
)
}
listing
:=
Listing
{
email
,
numStudents
,
message
,
getTime
()}
listing
:=
partner
Listing
{
email
,
numStudents
,
message
,
getTime
()}
listingsMutex
.
Lock
()
defer
listingsMutex
.
Unlock
()
listings
[
subject
]
=
append
(
listings
[
subject
],
listing
)
...
...
templates/found.html
View file @
cf89d78a
...
...
@@ -5,7 +5,7 @@
<div
{{
.
OtherTutsHidden
}}
style=
"color: green;"
>
In diesem Fach gibt es Anzeigen in den Tutorien:
{{ range .OtherTuts }}
<a
href=
"/dosearch?subject={{ .Subject }}&tutorium={{ .I
d
}}"
>
{{ .Name }}
</a>
<a
href=
"/dosearch?subject={{ .Subject }}&tutorium={{ .I
D
}}"
>
{{ .Name }}
</a>
{{ end }}
<br><br>
</div>
...
...
tutswap.go
View file @
cf89d78a
...
...
@@ -74,7 +74,7 @@ func addSwap(w http.ResponseWriter, r *http.Request) error {
if
!
hasSubject
(
subject
)
{
return
renderError
(
"Das angegebene Fach existiert nicht"
,
w
,
r
)
}
swap
:=
T
utSwap
{
email
,
from
,
to
,
message
,
getTime
()}
swap
:=
t
utSwap
Listing
{
email
,
from
,
to
,
message
,
getTime
()}
swapMutex
.
Lock
()
defer
swapMutex
.
Unlock
()
swaps
[
subject
]
=
append
(
swaps
[
subject
],
swap
)
...
...
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