-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
104 lines (83 loc) · 2.81 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"encoding/json"
"fmt"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/handler/transport"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/BurrrY/obstwiesen-server/graph"
"github.com/BurrrY/obstwiesen-server/internal/config"
fstr "github.com/BurrrY/obstwiesen-server/internal/file_store"
"github.com/go-chi/chi/v5"
"github.com/gorilla/websocket"
"github.com/rs/cors"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"net/http"
"strconv"
)
func setup() {
log.Info("Init Main")
viper.AutomaticEnv()
viper.SetEnvPrefix("OW")
viper.SetDefault(config.DB_PROVIDER, "NONE")
viper.SetDefault(config.DB_CONNSTR, "")
viper.SetDefault(config.DB_NAME, "meadow")
viper.SetDefault(config.FILE_CONNSTR, "./files")
viper.SetDefault(config.FILE_PROVIDER, "disk")
viper.SetDefault(config.PUBLIC_URL, "localhost:8080")
viper.SetDefault(config.GQL_PORT, "8080")
viper.SetDefault(config.GQL_PATH, "/graphql")
viper.SetDefault(config.XORIG, "http://localhost:3000")
viper.SetDefault(config.XORIG_DBG, false)
}
func main() {
setup()
b, _ := json.MarshalIndent(viper.AllSettings(), "", " ")
fmt.Print(string(b), "\n")
r := graph.Resolver{}
r.Setup()
router := chi.NewRouter()
// Add CORS middleware around every request
// See https://github.com/rs/cors for full option listing
router.Use(cors.New(cors.Options{
AllowedOrigins: []string{"http://localhost:8080", viper.GetString(config.XORIG), "http://192.168.178.201:3000"},
AllowCredentials: true,
Debug: viper.GetBool(config.XORIG_DBG),
}).Handler)
srv := handler.NewDefaultServer(graph.NewExecutableSchema(graph.Config{Resolvers: &graph.Resolver{}}))
srv.AddTransport(&transport.Websocket{
Upgrader: websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
// Check against your desired domains here
return r.Host == "localhost"
},
ReadBufferSize: 1024,
WriteBufferSize: 1024,
},
})
fs := http.FileServer(http.Dir("assets"))
router.Handle("/static/*", http.StripPrefix("/static/", fs))
var filestore fstr.FileStorage
tmp2, _ := fstr.GetProvider()
filestore = *tmp2
router.Get("/assets/{dir}/{file}", func(w http.ResponseWriter, r *http.Request) {
file := chi.URLParam(r, "file")
dir := chi.URLParam(r, "dir")
if file == "optimized" {
log.Error("Requested 'optimized' from dir ", dir)
return
}
log.Info("File requested: ", file)
width_str := r.URL.Query().Get("w")
width, _ := strconv.Atoi(width_str)
filepath := filestore.GetImage(file, dir, width)
http.ServeFile(w, r, filepath)
})
router.Handle("/", playground.Handler("Obstwiese", viper.GetString(config.GQL_PATH)))
router.Handle(viper.GetString(config.GQL_PATH), srv)
err := http.ListenAndServe(":"+viper.GetString(config.GQL_PORT), router)
if err != nil {
panic(err)
}
}