tools/cmd/melon-tools/main.go

119 lines
2.6 KiB
Go
Raw Normal View History

2022-03-23 00:39:53 +00:00
package main
import (
2022-09-08 14:48:36 +01:00
"code.mrmelon54.com/melon/tools/module/discord"
"code.mrmelon54.com/melon/tools/module/gitea"
"code.mrmelon54.com/melon/tools/utils"
2022-04-21 09:06:14 +01:00
_ "embed"
"encoding/gob"
2022-03-23 00:39:53 +00:00
"fmt"
"github.com/google/uuid"
2022-03-23 00:39:53 +00:00
"github.com/gorilla/mux"
2022-03-24 21:07:44 +00:00
"github.com/gorilla/sessions"
2022-03-23 00:39:53 +00:00
"github.com/joho/godotenv"
2022-04-21 09:06:14 +01:00
"html/template"
2022-03-23 00:39:53 +00:00
"log"
"net/http"
"os"
"sync"
2022-03-23 00:39:53 +00:00
)
2022-03-24 21:07:44 +00:00
var (
2022-03-28 21:22:34 +01:00
modules = []utils.IModule{
gitea.New(),
discord.New(),
2022-03-24 21:07:44 +00:00
}
sessionStore = sessions.NewCookieStore([]byte(os.Getenv("SESSION_KEY")))
2022-04-21 09:06:14 +01:00
//go:embed pages/index.go.html
indexTemplate string
2022-03-24 21:07:44 +00:00
)
2022-03-23 00:39:53 +00:00
func main() {
err := godotenv.Load()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
stateManager := StateManager{&sync.RWMutex{}, make(map[uuid.UUID]*utils.State)}
2022-03-23 00:39:53 +00:00
router := mux.NewRouter()
router.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) {
rw.Header().Set("Content-Type", "text/html")
2022-03-23 00:39:53 +00:00
rw.WriteHeader(http.StatusOK)
2022-04-21 09:06:14 +01:00
modTmp := make([]template.HTML, 0)
2022-03-28 21:22:34 +01:00
for _, v := range modules {
2022-04-21 09:06:14 +01:00
modTmp = append(modTmp, template.HTML(fmt.Sprintf("<a href=\"%s\">%s</a>", v.GetEndpoint(), v.GetName())))
}
tmp, err := template.New("homepage").Parse(indexTemplate)
if err != nil {
fmt.Println("Template parse error:", err)
return
}
err = tmp.Execute(rw, struct {
Modules []template.HTML
}{
Modules: modTmp,
})
if err != nil {
fmt.Println("Template execute error:", err)
return
2022-03-28 21:22:34 +01:00
}
2022-03-23 00:39:53 +00:00
})
gob.Register(uuid.UUID{})
2022-03-28 21:22:34 +01:00
for _, v := range modules {
a := v.GetEndpoint()
router.HandleFunc(a, func(rw http.ResponseWriter, req *http.Request) {
http.Redirect(rw, req, a+"/", http.StatusTemporaryRedirect)
})
2022-03-28 21:22:34 +01:00
v.SetupModule(router.PathPrefix(v.GetEndpoint()).Subrouter(), stateManager.sessionWrapper)
2022-03-23 00:39:53 +00:00
}
s := &http.Server{
Addr: os.Getenv("LISTEN"),
Handler: router,
}
err = s.ListenAndServe()
if err != nil {
if err == http.ErrServerClosed {
log.Println("Server closed successfully")
} else {
log.Fatalln(err)
}
return
}
}
2022-03-24 21:07:44 +00:00
type StateManager struct {
MSync *sync.RWMutex
States map[uuid.UUID]*utils.State
}
func (m *StateManager) sessionWrapper(cb func(http.ResponseWriter, *http.Request, *utils.State)) func(rw http.ResponseWriter, req *http.Request) {
2022-03-24 21:07:44 +00:00
return func(rw http.ResponseWriter, req *http.Request) {
session, _ := sessionStore.Get(req, "melon-tools-session")
if a, ok := session.Values["session-key"]; ok {
if b, ok := a.(uuid.UUID); ok {
m.MSync.RLock()
c, ok := m.States[b]
m.MSync.RUnlock()
if ok {
cb(rw, req, c)
return
}
}
}
u := utils.NewState()
m.MSync.Lock()
m.States[u.Uuid] = u
m.MSync.Unlock()
session.Values["session-key"] = u.Uuid
_ = session.Save(req, rw)
cb(rw, req, u)
2022-03-24 21:07:44 +00:00
}
}