I followed creating a basic web app with go, I created the folder with name myApp. In myApp have main.go and public folder(in public have index.html), here is my content in main.go:
package main
import "net/http"
import "github.com/russross/blackfriday"
func main() {
http.HandleFunc("/markdown", GenerateMarkdown)
http.Handle("/", http.FileServer(http.Dir("public")))
http.ListenAndServe(":8080", nil)
}
func GenerateMarkdown(rw http.ResponseWriter, r *http.Request) {
markdown := blackfriday.MarkdownCommon([]byte(r.FormValue("body")))
rw.Write(markdown)
}
I started server and go to http://localhost:8080/ but it doesn't link to index.html. Can anyone explain why it doesn't render index.html file. I'm a newbie in Golang.
This was answered in the comments, so here is a summary for completeness or something.
The code presented in the question produced the expected results after the actual build->run->test->repeat process was clarified for OP. Points of clarification were as follows:
After using go build to compile the binary, the program isn't running yet. Be sure to actually execute the binary before trying to connect to the server.
If it's necessary to make changes, stop the server, run go build again, then run the new binary.
As a side note, gin is a tool that tries to automate this process by rebuilding and launching your program each time you save the file.
Related
I am using github.com/webview/webview to render a web view on a Go app. It works fine on one system where I am writing it. But the request does not make it to the http server on some clean installed systems. It appears the failure is within the webview dlls. But I am at a loss on how to debug it.
Currently using Go 1.17.1 on Windows 10 Pro x64 21H1
I am running very simple code, similar to the example... I am also using go to serve an http page. I am then opening a webview box to interact with it.
On the Windows development machine, everything works as expected.
On a separate clean install of Windows (all same versions), the box comes up blank - just white. Not crashing though. Just not displaying anything.
Hitting the same URL from a browser on the local machine works. So the page works.
Changing the URL to refer to an external site (e.g. http://google.com), it works on all machines
On some machines, it works. Others not.
I have the DLLs in the same folder as the EXEs.
Adding debugging statements, it's mostly executing everything as expected.
However, the request is not making it to the HTTP server
BTW, the code also works fine on Linux...
The only pattern I can see is that the Windows machines that work seem to be older ones that have existed and been updated for some time. Relatively fresh installs seem to fail. So there must be some kind of subtle dependency?
I'm not sure how to debug this. It seems to be failing within the Webview code.
Any suggestions would be most appreciated... Simple test code below...
main.go
package main
import (
"html/template"
"log"
"net/http"
"github.com/gorilla/mux"
"github.com/webview/webview"
)
var (
IPport = "127.0.0.1:8080"
URL = "http://" + IPport
)
func main() {
log.Print("Starting main")
go HTTPServer(IPport)
OpenUI(URL)
}
func OpenUI(url string) {
log.Print("Starting OpenUI")
w := webview.New(true)
defer w.Destroy()
w.SetTitle("Webview Window")
w.SetSize(800, 600, webview.HintFixed)
w.Navigate(url)
w.Run()
}
func HTTPServer(ipPort string) {
log.Print("Starting HTTPserver")
rtr := mux.NewRouter()
rtr.HandleFunc("/", Page)
log.Printf("Listening on %s\n", ipPort)
err := http.ListenAndServe(ipPort, rtr)
if err != nil {
log.Print(err)
}
}
func Page(w http.ResponseWriter, r *http.Request) {
log.Print("Starting Page handler")
var err error
tmpl := template.New("page")
if tmpl, err = tmpl.ParseFiles("page.gohtml"); err != nil {
log.Print(err)
}
if err = tmpl.ExecuteTemplate(w, "page", nil); err != nil {
log.Print(err)
}
}
page.gohtml
{{define "page"}}
<!doctype html>
<html lang="en">
<body>
<h1>Hello Page</h1>
</body>
</html>
{{end}}
Output on working system...
2021/09/21 11:51:00 Starting main
2021/09/21 11:51:00 Starting OpenUI
2021/09/21 11:51:00 Starting HTTPserver
2021/09/21 11:51:00 Listening on 127.0.0.1:8080
2021/09/21 11:51:02 Starting Page handler
Output on failing system... Again, this will work if you hit it with a web browser... e.g. Edge
2021/09/21 11:52:00 Starting main
2021/09/21 11:52:00 Starting OpenUI
2021/09/21 11:52:00 Starting HTTPserver
2021/09/21 11:52:00 Listening on 127.0.0.1:8080
After a couple of days of fiddling... I figure it out.
You need to actually install one of the Webview2 runtimes. It seems on my older Windows systems, the required dependencies were satisfied through some previous updates or other unknown means. I certainly never installed the runtime specifically. But on a fresh install, you need to install one of the runtime options. You still need the dlls in the same folder as the exe. Once installed, it works everywhere.
See:
https://learn.microsoft.com/en-us/microsoft-edge/webview2/concepts/distribution
Someone should update the README to mention the installation of the runtime... I used the Bootstrapper as it's smaller.
Thanks for listening...
How can I serve swagger-ui-dist in Cloud Function with Go?
Out of Cloud Functions environment I would do:
package main
import (
"fmt"
"net/http"
)
func main() {
fs := http.FileServer(http.Dir("./swagger-ui-dist"))
http.Handle("/swaggerui/", http.StripPrefix("/swaggerui/", fs))
http.ListenAndServe(":8080", nil)
}
But since Cloud functions use a normal function as handler using the Standard http.HanlderFunc interface I don't know how to make it work.
I have tried to simulate this scenario to do my tests with http.ServeFile, but didn't work. Seems like is not downloading all the contents of the folder.
package main
import (
"fmt"
"net/http"
)
func cloudFunctionHandler(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "./swagger-ui-dist")
}
func main() {
http.HanldeFunc("/swaggerui/", handler)
http.ListenAndServe(":8080", nil)
}
I tried it in several way and it worked locally with the same behavior as Cloud Function (same function signature), but it didn't work after deployment on Cloud Functions.
I understood why. In fact Buildpack (the tools develped by Google for building a container without a Dockerfile, and now in the CNCF sandbox) compiles the .go file and copy the binary to the final container layer. All the other files/directory are omitted. Thus, it can't work!
I recommend you to use Cloud Run (same underlying platform as Cloud Functions, and in some cases, cheaper. I wrote an article on this)
The other solution is to use a not compiled language such as NodeJS or Python.
It is actually possible in three ways (that I know):
Serve the static files from a CDN. E.g. from http://cdnjs.cloudflare.com/ajax/libs/swagger-ui/3.40.0/
Host the static files yourself on a different endpoint
Add the static files to the root folder of your cloud function. These will be packaged together with your code and therefore be available when deployed
(I went for option 3)
For options 2 and 3 you have to override the folder that swaggerui uses for its static assets. How you do this depends on how you configure the swagger ui.
In my case I was using the flask_restx Python package to generate the swagger.json and serve the swaggerui. So I monkeypatched like so:
from flask_restx.apidoc import apidoc
apidoc.static_folder = '<MY STATIC FOLDER PATH' # e.g. os.path.abspath('static/swaggerui')
Also, you need to copy the files from the swaggerui library to your function's root directory before running gcloud function deploy ....
I copy the static folder from flask_restx to my function's root directory during my CI pipeline.
Hope this helps anyone!
I am fairly new to the Go programming language and completely new to the Go SDK from AWS. I am trying to use a service but I have a strange problem where the types defined by the imported service are found, but the functions of the service are undefined.
This question is not about using the particular service, but just how to correctly import it. My code:
package auth
import (
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cognitoidentityprovider"
)
func SignUpTest() {
input := cognitoidentityprovider.SignUpInput{
Username: aws.String("example#mail.com"),
Password: aws.String("test1234"),
}
_, err := cognitoidentityprovider.SignUp(&input)
if err != nil {
log.Fatal(err)
}
}
I get the following error when running go build:
auth/signup.go:18:12: undefined: cognitoidentityprovider.SignUp
The autocomplete in my IDE also states that it can find the cognitoidentityprovider.SingUpInput struct, but it is unable to find the cognitoidentityprovider.SignUp function.
I use Go 1.10.1 on WSL Ubuntu. I use DEP 0.4.1 for package management. I verified that the AWS SDK is available in the vendor folder and that the cognitoidentityprovider package is available (the SignUp) function is also there.
What am I missing here?
The error says it all. cognitoidentityprovider.SignUp isn't defined, because there is no symbol SignUp exported by the cognitoidentityprovider package.
I'm not really sure what you want to do instead, since I'm not familiar with that SDK, but you're trying to call a function that doesn't exist. I suggest re-examining the documentation or example you're following. You've probably made a simple mistake.
You seem to be confused by the CognitoIdentityProvider.SignUp instance method. But as that's an instance method, and not an exported function, it requires an instance of a CognitoIdentityProvider first:
cip := cognitoidentityprovider.New( ... )
_, err := cip.SignUp(input)
Normally I use C# for everything, I can create a desktop application with C# and also place a lot of the same c# code in a dll on my shared hosted web server. This saves me a lot of coding time.
Is there any way to this with go?
e.g. place some kind of go dll on my hosted web server to generate HTML.
I am aware that go doesn't do dll's, I am also aware that creating a web server with go to listen to port 80 is straight forward. But this is not a solution for a shared web server.
This seems like a brilliant use for go and it surprises me that this might not be possible.
I should mention, it would be nice if the go code didn't have to restart with every http request.
This is how I do it with C#:
On the web server I add an aspx page like this:
<%# Page Language="C#" %>
<%# Register TagPrefix="MyDLL" Namespace="MyDLL" Assembly="MyDLL"%>
<MyDLL:Web Runat="Server"/>
It simply loads the C# dll which generates HTML based on the http request.
First you have to setup IIS to use fastcgi (instructions) then you can simply build a go web app like you normally would but instead of using http.ListenAndServe you use fcgi.Serve
Example:
func hello(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Hello, world of FCGI!")
}
func main() {
http.HandleFunc("/hello/", hello)
if err := fcgi.Serve(nil, nil); err != nil {
log.Fatal("fcgi.Serve: ", err)
}
}
But keep in mind that there can be multiple instances of the programming running / destroyed all the time, so any temp data (in-memory sessions, etc) should be stored on memcache or temp files on disk or a database.
Me and a few friends are writing a server using golang (revel) and had a database implemented just a few weeks ago. Then suddenly everything just stopped working and the go compiler can't find my import files. That includes EmptyPlugin as well as RegisterPlugin.
We are all pretty new so this is probably a pretty simple fix. I have checked my github.com/robfig/revel folder, and there is no file called EmptyPlugin. But that is probably a misunderstanding from my side :P.
By commenting the lines revel.EmptyPlugin and revel.RegisterPlugin(DbPlugin{}) everything works as it should.
package controllers
import (
"database/sql"
"fmt"
_ "github.com/bmizerany/pq"
"github.com/robfig/revel"
"log"
"time"
//"strconv"
)
type DbPlugin struct {
revel.EmptyPlugin
}
func init() {
revel.RegisterPlugin(DbPlugin{})
}
var db *sql.DB
var err error
You appear to compiling using the latest and recently updated version of Revel: https://github.com/robfig/revel.
However, it appears that your code was written for this older version of Revel: https://github.com/robfig/revel/tree/dev.
Update your code to the latest version of Revel.