Commit 74fe4565 authored by Linus's avatar Linus 🤔
Browse files

Changed some functions to prepare for swaps, removed -listings cl argument and added -data

parent c1de465f
......@@ -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 = saveListings(listingsFile)
err = saveStruct(listings, 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(listings)
err := encoder.Encode(s)
if err != nil {
return err
}
......
......@@ -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 = saveListings(listingsFile)
err = saveStruct(listings, listingsFile)
if err != nil {
return err
}
......@@ -201,7 +198,7 @@ func doDel(w http.ResponseWriter, r *http.Request) error {
numDeleted += l - i
}
err = saveListings(listingsFile)
err = saveStruct(listings, listingsFile)
if err != nil {
return err
}
......
......@@ -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")
listingsPath := flag.String("listings", "/data/listings.toml", "The path of your configuration file")
dataPath := flag.String("data", "/data/", "The path of your data files")
flag.Parse()
http.HandleFunc("/static/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, r.URL.Path[1:])
})
listingsFile = *listingsPath
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))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment