tools/cmd/gitea-tools/main.go

59 lines
1.3 KiB
Go
Raw Normal View History

2022-03-23 00:39:53 +00:00
package main
import (
"code.mrmelon54.xyz/sean/melon-tools/module/gitea"
"code.mrmelon54.xyz/sean/melon-tools/utils"
"fmt"
"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"
"log"
"net/http"
"os"
)
2022-03-24 21:07:44 +00:00
var (
modules = map[string]utils.IModule{
"/gitea": gitea.New(),
}
sessionStore = sessions.NewCookieStore([]byte(os.Getenv("SESSION_KEY")))
)
2022-03-23 00:39:53 +00:00
func main() {
err := godotenv.Load()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
router := mux.NewRouter()
router.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusOK)
_, _ = rw.Write([]byte("Melon Tools\n"))
})
for k, v := range modules {
2022-03-24 21:07:44 +00:00
v.SetupModule(router.Path(k).Subrouter(), 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
func sessionWrapper(cb func(http.ResponseWriter, *http.Request, *sessions.Session)) func(rw http.ResponseWriter, req *http.Request) {
return func(rw http.ResponseWriter, req *http.Request) {
session, _ := sessionStore.Get(req, "melon-tools-session")
cb(rw, req, session)
}
}