2023-08-19 23:47:36 +02:00
|
|
|
/*
|
|
|
|
YetAnotherToDoList
|
|
|
|
Copyright © 2023 gilex-dev gilex-dev@proton.me
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, version 3.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2023-09-15 23:45:28 +02:00
|
|
|
"fmt"
|
2023-08-19 23:47:36 +02:00
|
|
|
"net/http"
|
2023-09-01 23:42:19 +02:00
|
|
|
"strconv"
|
2023-08-19 23:47:36 +02:00
|
|
|
|
|
|
|
"github.com/99designs/gqlgen/graphql/handler"
|
2023-11-05 17:42:14 +01:00
|
|
|
"github.com/go-chi/chi"
|
|
|
|
"github.com/go-chi/chi/middleware"
|
2023-09-15 23:45:28 +02:00
|
|
|
"somepi.ddns.net/gitea/gilex-dev/YetAnotherToDoList/globals"
|
2023-08-19 23:47:36 +02:00
|
|
|
"somepi.ddns.net/gitea/gilex-dev/YetAnotherToDoList/graph"
|
2023-11-05 17:42:14 +01:00
|
|
|
"somepi.ddns.net/gitea/gilex-dev/YetAnotherToDoList/server/auth"
|
2023-08-19 23:47:36 +02:00
|
|
|
)
|
|
|
|
|
2023-11-05 17:42:14 +01:00
|
|
|
func StartServer(portHTTP int, portHTTPS int, certFile string, keyFile string) {
|
|
|
|
router := chi.NewRouter()
|
|
|
|
router.Use(middleware.StripSlashes)
|
2023-10-26 23:22:42 +02:00
|
|
|
|
2023-11-05 17:42:14 +01:00
|
|
|
handleDevTools(router, portHTTP, portHTTPS) // controlled by 'dev' tag
|
|
|
|
handleFrontend(router) // controlled by 'headless' tag
|
|
|
|
router.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
|
2023-09-15 23:45:28 +02:00
|
|
|
fmt.Fprintf(w, "%s %s", globals.Version, globals.CommitHash)
|
|
|
|
})
|
2023-08-19 23:47:36 +02:00
|
|
|
|
2023-10-24 22:29:07 +02:00
|
|
|
srv := handler.NewDefaultServer(graph.NewExecutableSchema(graph.Config{Resolvers: &graph.Resolver{}}))
|
2023-11-05 17:42:14 +01:00
|
|
|
router.HandleFunc("/auth/login", auth.IssueRefreshTokenHandler)
|
|
|
|
router.HandleFunc("/auth", auth.IssueAccessTokenHandler)
|
|
|
|
|
|
|
|
router.Group(func(r chi.Router) {
|
|
|
|
r.Use(auth.Middleware())
|
|
|
|
r.Handle("/api", srv)
|
|
|
|
r.HandleFunc("/protected", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fmt.Printf("user is %+v\n", auth.ForContext(r.Context()))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
// router.HandleFunc("/auth/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// http.Redirect(w, r, "/auth", http.StatusMovedPermanently)
|
|
|
|
// })
|
|
|
|
|
|
|
|
err := <-listen(portHTTP, portHTTPS, certFile, keyFile, router)
|
|
|
|
globals.Logger.Fatalf("Could not start serving service due to (error: %s)", err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func listen(portHTTP int, portHTTPS int, certFile string, keyFile string, router *chi.Mux) chan error {
|
|
|
|
|
|
|
|
errs := make(chan error)
|
|
|
|
|
|
|
|
// Starting HTTP server
|
|
|
|
if portHTTP != -1 {
|
|
|
|
go func() {
|
|
|
|
globals.Logger.Printf("Staring HTTP service on %d ...", portHTTP)
|
|
|
|
|
|
|
|
if err := http.ListenAndServe(":"+strconv.FormatInt(int64(portHTTP), 10), router); err != nil {
|
|
|
|
errs <- err
|
|
|
|
}
|
|
|
|
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Starting HTTPS server
|
|
|
|
if portHTTPS != -1 && certFile != "" && keyFile != "" {
|
|
|
|
go func() {
|
|
|
|
globals.Logger.Printf("Staring HTTPS service on %d ...", portHTTPS)
|
|
|
|
if err := http.ListenAndServeTLS(":"+strconv.FormatInt(int64(portHTTPS), 10), certFile, keyFile, router); err != nil {
|
|
|
|
errs <- err
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2023-10-26 23:22:42 +02:00
|
|
|
|
2023-11-05 17:42:14 +01:00
|
|
|
return errs
|
2023-08-19 23:47:36 +02:00
|
|
|
}
|