-
Notifications
You must be signed in to change notification settings - Fork 52
/
generate_api.go
51 lines (42 loc) · 1.52 KB
/
generate_api.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
package api
import (
"encoding/json"
"log"
"net/http"
"github.com/commitdev/zero/internal/config/projectconfig"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
)
func generateProject(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch req.Method {
case "POST":
decoder := json.NewDecoder(req.Body)
var config projectconfig.ZeroProjectConfig
err := decoder.Decode(&config)
if err != nil {
panic(err)
}
log.Println(config.Name)
// createProject(config)
w.WriteHeader(http.StatusCreated)
w.Write([]byte(`{"message": "Post successful"}`))
default:
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(`{"message": "Not found"}`))
}
}
func Commit0Api() {
// React Frontend is served on port 3000 while in development mode.
allowOrigins := handlers.AllowedOrigins([]string{"http://localhost:3000"})
allowedMethods := handlers.AllowedMethods([]string{"POST", "OPTIONS"})
allowedHeaders := handlers.AllowedHeaders([]string{"content-type"})
var router = mux.NewRouter()
var api = router.PathPrefix("/v1/generate").Subrouter()
api.NotFoundHandler = http.HandlerFunc(generateProject)
staticHandler := http.StripPrefix("/static/", http.FileServer(http.Dir("internal/ui/build/static")))
router.PathPrefix("/static/").Handler(staticHandler)
buildHandler := http.FileServer(http.Dir("internal/ui/build"))
router.PathPrefix("/").Handler(buildHandler)
log.Fatal(http.ListenAndServe(":8080", handlers.CORS(allowOrigins, allowedMethods, allowedHeaders)(router)))
}