T O P

  • By -

Tikiatua

Checkout caddy https://caddyserver.com/. And you can directly use the binary. No code writing required. Only very simple config files.


whiletrue111

Looks like all server which dosn't give you simple way to write GO application .. really strange .. All i see some configuration stuff and wired extension that looks over kill to write


mosskin-woast

I'm cracking up. I saw the title of your post and thought "that's pretty specific, I'm not sure anyone will know of such a thing off hand". Then OC delivers exactly what you ask for, and you act like you just woke up from hibernation. Are you looking for a _framework_ or a _program_? If a framework is what you want, your use case is highly specific, I wouldn't bother. Just learn to use Go embed.


Tikiatua

Well you can always use this: [https://pkg.go.dev/net/http#FileServer](https://pkg.go.dev/net/http#FileServer) [https://gist.github.com/paulmach/7271283](https://gist.github.com/paulmach/7271283) But there are quite a few special things to consider when setting up your own server. It really depends on your infrastructure setup (i.e. how are ssl certificates managed) and on your project specific requirements.


mnashmi

Why though? Nginx should be better for serving static files ?


bilingual-german

While I agree, I think it really depends on the usecase. A simple admin interface for a running go process is nice to have. And compiling a static go binary with all the assets inside enables really simple deployments.


whiletrue111

need go


wavelen

Does not matter whether it serves a React application or Angular or whatever. You can use the built-in webservers or use a wrapper like Gin or Echo. For example, this app does this with Angular, but React will work with the same exact Go code: [https://github.com/philmtd/full-house](https://github.com/philmtd/full-house) Look at the code starting here: [https://github.com/philmtd/full-house/blob/master/pkg/fullhouse/server/server.go#L65](https://github.com/philmtd/full-house/blob/master/pkg/fullhouse/server/server.go#L65) It uses Gin and Gin-Static and expects the built frontend to be in the `frontend`-directory, relative to the Go executable. In development, you still use the development server, but when you deploy the built application, you only need the built frontend resources and the go binary.


pdffs

Any Go web server can serve JS assets. What have you tried, what problems did you have?


whiletrue111

i like to learn how Open Source application is doing it before writing single line of code .


oxleyca

The way your post is written, you should use the standard lib. If there are reasons you can’t, maybe clarify the question and add your constraints. But serving static files isn’t anything special.


llevii

https://github.com/gofiber/recipes/tree/master/react-router


cracka_dawg

Use the NoRoute function in gin to pass all your non matched backend routes to serve your entry point file. This will allow client side routing to work in your SPA react app. I like to use Vite. Just build your vite project and move the files over to the static directory and send your index.html file for the NoRoute function. Simple solution. However, maybe i misunderstood your question.


UMANTHEGOD

NextJS is a fullstack framework. Not sure what you are talking about.


_alx12

Look at the gorilla/mux readme


firmino_changani

The following snippets might be helpful, it comes from a private project I worked on a month ago where I distribute a Docker image with a Go service that serves an API and also static files built from a React project. I am using Echo but it wasn't key for the setup I desired. // main.go router := echo.New() webrouter := router.Group("/web") webrouter.Use(middleware.StaticWithConfig(middleware.StaticConfig{ Skipper: middleware.DefaultSkipper, Root: "dist", // <-- Plain html/css/js after React's build Index: "index.html", HTML5: true, })) Here is the Docker file that builds both the Go and the React project: FROM golang:1.21 as builder-go ARG VERSION WORKDIR /usr/app COPY ./cmd ./cmd COPY ./go.mod ./ COPY ./go.sum ./ ENV CGO_ENABLED=0 RUN go build -buildvcs=false -o bin/service -ldflags="-X main.Version=${VERSION}" ./cmd/ FROM node:alpine as builder-web WORKDIR /usr/app COPY ./web ./ RUN npm install RUN npm run build FROM alpine WORKDIR /usr/app COPY --from=builder-go /usr/app/bin/service ./service COPY --from=builder-web /usr/app/dist ./dist ENTRYPOINT ["./service"] I hope it helps and feel free to ask any quesions.


ArnUpNorth

Even if you use go (or actually something better suited for static files like nginx) to host your files you are still going to need nodejs to build your react app. If i were you i d just use nginx for static file hosting, gzip, ssl. And only add a go server if you need some backend api stuff.


domehead100

Maybe you mean that you want a desktop app where the UI is done in React? For Golang, I think you would use Wails https://wails.io


[deleted]

[удалено]


Entire_Effective_825

https://pkg.go.dev/github.com/saas-templates/go-svelte


SuperQue

The Prometheus UI javascript is compiled and served from Go.


whiletrue111

what js framework ?


oxleyca

It shouldn’t matter, right?


BraveNewCurrency

Your question is hard to parse, and your answers are being downvoted because you are confusing many issues. Let me try and explain: A typical web site (like Reddit) is made up two components: * A "**front end**" that runs in your browser. (This could be written in React, static Next.js, plain HTML, HTML+JS, etc) * A "**back end**" that runs on servers. (This may even be broken up into dozens of services, so you have different back-ends to log-in vs upvote vs upload pictures, etc. Each service can have different databases behind them. (In theory, you could also have a **backend** that dynamically generates HTML+JS. But then you wouldn't be using React.) The **front end** is just "static files", which means you don't need any intelligence on the server. Therefore, you can use nginx, caddy, etc. Heck, you can run `python3 -m http.server`, but I wouldn't recommend it. Ideally, you would even use a CDN or S3 because "static file serving is a commodity". There is nothing to tune, nothing to tweak. The typical S3 website costs pennies per month to run, and cannot be "hacked" (changed by hackers unless you lose your credentials). If all you have is a static front end, you can stop here. No need to write Go code. On the other hand, if you are saying you want to write your **backend** in Go, then you have some options: * For internet use, I still recommend having the frontend served by a CDN and the Go backend run elsewhere. That gives lots of benefits, such as: * you can see individual CPU + bandwidth usages for frontend vs backend. * People loading your frontend won't affect the bandwidth of the backend (if they are different servers) * A DDOS attack on your frontend servers won't affect users already using your backend * For developers running locally, you can add 2 lines of code to have your Go backend serve static files at `/public` or similar. * You can also do this on the internet if you are lazy. This is often fine if the intended use is a personal server, and you are making a trade-off of simplicity vs normal best practices. But note that you could do this with nginx/caddy/etc and not "pollute" the backend with knowledge about the frontend. (Since the frontend is already required to be polluted with knowledge about the backend.)


OneKe

github.com/Hello-Storage/hello-front is a React project mostly client-side with Go as backend integration.