Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add an env param "InSecure" to allow non-https deploy #9

Merged
merged 3 commits into from
Aug 2, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
feat: support hosting as a TLS server directly
0x2E committed Aug 1, 2024

Verified

This commit was signed with the committer’s verified signature.
0x2E Yuan
commit a26c8e0bf75ad8f28ce42469ae16748146e42919
10 changes: 8 additions & 2 deletions .env
Original file line number Diff line number Diff line change
@@ -8,5 +8,11 @@ PASSWORD=""
# Path to store sqlite DB file
DB="fusion.db"

# Allow non-https request
INSECURE=true
# Enable Secure Cookie
# It is automatically set to true when TLS_* is not empty.
SECURE_COOKIE=false

# Path to TLS cert and key files
# If you are using a reverse proxy like Nginx to handle HTTPS, please leave these empty.
TLS_CERT=""
TLS_KEY=""
11 changes: 10 additions & 1 deletion api/api.go
Original file line number Diff line number Diff line change
@@ -119,7 +119,16 @@ func Run() {
items.PATCH("/-/unread", itemAPIHandler.UpdateUnread)
items.DELETE("/:id", itemAPIHandler.Delete)

apiLogger.Fatalln(r.Start(fmt.Sprintf("%s:%d", conf.Conf.Host, conf.Conf.Port)))
var err error
addr := fmt.Sprintf("%s:%d", conf.Conf.Host, conf.Conf.Port)
if conf.Conf.TLSCert != "" {
err = r.StartTLS(addr, conf.Conf.TLSCert, conf.Conf.TLSKey)
} else {
err = r.Start(addr)
}
if err != nil {
apiLogger.Fatalln(err)
}
}

func errorHandler(err error, c echo.Context) {
3 changes: 1 addition & 2 deletions api/session.go
Original file line number Diff line number Diff line change
@@ -26,8 +26,7 @@ func (s Session) Create(c echo.Context) error {

sess, _ := session.Get("login", c)

//使用非https请求时,为保证Set-Cookie能正常生效,对Option进行特殊设置
if conf.Conf.InSecure {
if !conf.Conf.SecureCookie {
sess.Options.Secure = false
sess.Options.SameSite = http.SameSiteDefaultMode
}
13 changes: 12 additions & 1 deletion conf/conf.go
Original file line number Diff line number Diff line change
@@ -12,11 +12,14 @@ import (
const Debug = false

var Conf struct {
InSecure bool `mapstructure:"INSECURE"`
Host string `env:"HOST" envDefault:"0.0.0.0"`
Port int `env:"PORT" envDefault:"8080"`
Password string `env:"PASSWORD"`
DB string `env:"DB" envDefault:"fusion.db"`

SecureCookie bool `env:"SECURE_COOKIE" envDefault:"false"`
TLSCert string `env:"TLS_CERT"`
TLSKey string `env:"TLS_KEY"`
}

func Load() {
@@ -41,5 +44,13 @@ func validate() error {
if Conf.Password == "" {
return errors.New("password is required")
}

if (Conf.TLSCert == "") != (Conf.TLSKey == "") {
return errors.New("missing TLS cert or key file")
}
if Conf.TLSCert != "" {
Conf.SecureCookie = true
}

return nil
}