goploy/internal/server/server.go

39 lines
895 B
Go
Raw Normal View History

2022-11-23 16:49:55 +08:00
// Copyright 2022 The Goploy Authors. All rights reserved.
// Use of this source code is governed by a GPLv3-style
// license that can be found in the LICENSE file.
package server
import (
"github.com/vearutop/statigz"
"github.com/zhenorzz/goploy/config"
"github.com/zhenorzz/goploy/web"
"io/fs"
"log"
"net/http"
)
type Response interface {
Write(http.ResponseWriter, *http.Request) error
}
type Server struct {
http.Server
*Router
}
func (srv *Server) Spin() {
if config.Toml.Env == "production" {
subFS, err := fs.Sub(web.Dist, "dist")
if err != nil {
log.Fatal(err)
}
http.Handle("/assets/", statigz.FileServer(subFS.(fs.ReadDirFS)))
http.Handle("/favicon.ico", statigz.FileServer(subFS.(fs.ReadDirFS)))
}
http.Handle("/", srv.Router)
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatal("ListenAndServe: ", err.Error())
}
}