Last active
December 17, 2022 17:35
-
-
Save samonzeweb/a840e79b8f81e773b2a970c541389775 to your computer and use it in GitHub Desktop.
Go and Vue.js development
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
// ---------- | |
// How to developp with Vue.js and golang (or other JS framework) | |
// (awful almost raw notes) | |
// | |
// ---------- | |
// Create a projet ClientVue with vue-cli | |
// | |
// For developpment with webpack add a proxy to | |
// ClientVue/config/index.js , then Node will proxy | |
// api request to Go backend. | |
// | |
// proxyTable: { | |
// // proxy all requests starting with /api to go server | |
// '/api': { | |
// target: 'http://localhost:5000', | |
// changeOrigin: true, | |
// pathRewrite: { | |
// //'^/api': '/other-api' | |
// }, | |
// }, | |
// | |
// For developpement with webpack and HTTPS add --https | |
// to webpack-dev-server command line in package.json : | |
// | |
// "dev": "webpack-dev-server --https --inline --progress --config build/webpack.dev.conf.js", | |
// | |
// | |
// ---------- | |
// For Angular 2+ with angular-cli : | |
// | |
// Create a file proxy.conf.json (see example) and | |
// use "--proxy-config proxy.conf.json" option with ng serve. | |
// More on proxy : https://github.com/angular/angular-cli/blob/master/docs/documentation/stories/proxy.md | |
// | |
// { | |
// "/api": { | |
// "target": "http://localhost:5000", | |
// "secure": false | |
// } | |
// } | |
// | |
// For HTTPS simply use --ssl option with ng server : ng serve --ssl | |
// | |
import ( | |
"log" | |
"net/http" | |
"os" | |
"path/filepath" | |
"github.com/go-chi/chi" | |
) | |
func main() { | |
r := chi.NewRouter() | |
r.Route("/api/v1", func(r chi.Router) { | |
r.Get("/hello", helloWorld) | |
}) | |
// Frontend (without webpack : prod, dev only on api, ...) | |
workDir, _ := os.Getwd() | |
//filesDir := filepath.Join(workDir, "public") | |
filesDir := filepath.Join(workDir, "ClientVue", "dist") | |
fileServer := http.FileServer(http.Dir(filesDir)) | |
r.Method("GET", "/*", fileServer) | |
log.Fatal(http.ListenAndServe(":5000", r)) | |
} | |
// API | |
func helloWorld(w http.ResponseWriter, r *http.Request) { | |
w.Header().Set("Content-Type", "application/json") | |
w.WriteHeader(http.StatusOK) | |
w.Write([]byte(`{ "message": "Hello World !" }`)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment