Updated avatar refresh

This commit is contained in:
2017-07-18 07:23:48 +02:00
parent 1706ef9bc4
commit 5d0e7911d8
4 changed files with 59 additions and 125 deletions

View File

@ -1,14 +1,23 @@
package main
import (
"encoding/json"
"flag"
"io"
"log"
"net/http"
)
"os"
"path"
"strings"
func refreshAvatar(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("ok"))
}
"github.com/animenotifier/arn"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"github.com/animenotifier/avatar/lib"
)
var port = "8001"
@ -18,6 +27,41 @@ func init() {
}
func main() {
http.HandleFunc("/", refreshAvatar)
// Switch to main directory
exe, err := os.Executable()
if err != nil {
panic(err)
}
root := path.Dir(exe)
os.Chdir(path.Join(root, "../../"))
// Start server
http.HandleFunc("/", onRequest)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
// onRequest handles requests and refreshes the requested avatar
func onRequest(w http.ResponseWriter, req *http.Request) {
userID := strings.TrimPrefix(req.URL.Path, "/")
user, err := arn.GetUser(userID)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, err.Error())
return
}
// Refresh
lib.RefreshAvatar(user)
// Send JSON response
buffer, err := json.Marshal(user.Avatar)
if err != nil {
io.WriteString(w, err.Error())
}
w.Write(buffer)
}