68 lines
1.1 KiB
Go
Raw Normal View History

2017-07-18 01:55:47 +00:00
package main
import (
2017-07-18 05:23:48 +00:00
"encoding/json"
2017-07-18 01:55:47 +00:00
"flag"
2017-07-18 05:23:48 +00:00
"io"
2017-07-18 01:55:47 +00:00
"log"
"net/http"
2017-07-18 05:23:48 +00:00
"os"
"path"
"strings"
2017-07-18 01:55:47 +00:00
2017-07-18 05:23:48 +00:00
"github.com/animenotifier/arn"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"github.com/animenotifier/avatar/lib"
)
2017-07-18 01:55:47 +00:00
var port = "8001"
func init() {
flag.StringVar(&port, "port", "", "Port the HTTP server should listen on")
flag.Parse()
}
func main() {
2017-07-18 05:23:48 +00:00
// 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)
2017-07-18 01:55:47 +00:00
log.Fatal(http.ListenAndServe(":"+port, nil))
}
2017-07-18 05:23:48 +00:00
// 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)
}